0

设想

在这个应用程序中有一个倒数计时器,每当我打开应用程序时,倒数计时器会自动从 5 开始并在 0 处停止。

问题

当计时器编号达到 0 并自动回到 5 并再次启动(循环)时,如何更新/更改计时器编号。以下是 dart 和 bloc 代码。

代码飞镖

class Ticker {
      Stream<int> tick({int ticks}) {
        return Stream.periodic(Duration(seconds: 1), (x) => ticks - x - 1)
            .take(ticks);
      }}

TimerBloc.dart

class TimerBloc extends Bloc<TimerEvent, TimerState> {
      final Ticker _ticker;
      final int _duration = 5;
      StreamSubscription<int> _tickerSubscription;
      TimerBloc({@required Ticker ticker})
          : assert(ticker != null),
            _ticker = ticker;
      @override
      TimerState get initialState => Ready(_duration);
      @override
      void onTransition(Transition<TimerEvent, TimerState> transition) {
        super.onTransition(transition);
        print(transition);
      }
      @override
      Stream<TimerState> mapEventToState(
          TimerEvent event,
          ) async* {
        if (event is Start) {
          yield* _mapStartToState(event);
        }else if (event is Tick) {
          yield* _mapTickToState(event);
        }
      }
      @override
      Future<void> close() {
        _tickerSubscription?.cancel();
        return super.close();
      }
      Stream<TimerState> _mapStartToState(Start start) async* {
        yield Running(start.duration);
        _tickerSubscription?.cancel();
        _tickerSubscription = _ticker
            .tick(ticks: start.duration)
            .listen((duration) => add(Tick(duration: duration)));
      }
      Stream<TimerState> _mapTickToState(Tick tick) async* {
        yield tick.duration > 0 ? Running(tick.duration) : Finished();
      }
    }

TimerEvent.dart

abstract class TimerEvent extends Equatable {
  const TimerEvent();
  @override
  List<Object> get props => [];
}
class Start extends TimerEvent {
  final int duration;
  const Start({@required this.duration});
  @override
  String toString() => "Start { duration: $duration }";
}
class Tick extends TimerEvent {
  final int duration;
  const Tick({@required this.duration});
  @override
  List<Object> get props => [duration];
  @override
  String toString() => "Tick { duration: $duration }";
}

TimerState.dart

abstract class TimerState extends Equatable {
  final int duration;
  const TimerState(this.duration);
  @override
  List<Object> get props => [duration];
}
class Ready extends TimerState {
  const Ready(int duration) : super(duration);
  @override
  String toString() => 'Ready { duration: $duration }';
}
class Running extends TimerState {
  const Running(int duration) : super(duration);
  @override
  String toString() => 'Running { duration: $duration }';
}
class Finished extends TimerState {
  const Finished() : super(0);
}

主要.dart

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Timer',
      home: BlocProvider(
        create: (context) => TimerBloc(ticker: Ticker()),
        child: Timer(),
      ),);}}
class Timer extends StatefulWidget {
  static const TextStyle timerTextStyle = TextStyle(
    fontSize: 60,
    fontWeight: FontWeight.bold,
    color: Colors.black
  );
  @override
  _TimerState createState() => _TimerState();
}
class _TimerState extends State<Timer> {
  TimerBloc timerBloc;
  @override
  void initState() {
    timerBloc = BlocProvider.of<TimerBloc>(context);
    timerBloc.add(Start(duration: 5));
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Flutter Timer')),
      body: Stack(
        children: [
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Padding(
                padding: EdgeInsets.symmetric(vertical: 100.0),
                child: Center(
                  child: BlocBuilder<TimerBloc, TimerState>(
                    builder: (context, state) {
                      final String minutesStr = ((state.duration / 60) % 60)
                          .floor()
                          .toString()
                          .padLeft(2, '0');
                      final String secondsStr = (state.duration % 60)
                          .floor()
                          .toString()
                          .padLeft(2, '0');
                      return Text(
                        '$minutesStr:$secondsStr',
                        style: Timer.timerTextStyle,
                      );},),),),
              BlocBuilder<TimerBloc, TimerState>(
                condition: (previousState, state) =>
                state.runtimeType != previousState.runtimeType,
                builder: (context, state) => Actions(),
              ),],),],),);}}
class Actions extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: _mapStateToActionButtons(
        timerBloc: BlocProvider.of<TimerBloc>(context),
      ),);}
List<Widget> _mapStateToActionButtons({
    TimerBloc timerBloc,
  }) {
    final TimerState currentState = timerBloc.state;
    if (currentState is Ready) {
 return [];}}
4

1 回答 1

4

您可以像这样创建一个从 5 到 0 永远计数的流:

const int ticks = 5;
Stream<int>.periodic(
        const Duration(seconds: 1), (x) => ticks - x % (ticks + 1))
    .listen((value) => print(value));

您应该能够将它集成到您​​的代码类中,如下所示:

class Ticker {
    Stream<int> tick({int ticks}) {
        return Stream<int>.periodic(
            const Duration(seconds: 1), (x) => ticks - x % (ticks + 1));
    }
}
于 2020-02-24T14:30:35.310 回答