如何动态更改块flutter中的字段

问题描述 投票:0回答:1

我正在使用 bloc 中的 Flutter Timer 示例(https://bloclibrary.dev/ja/tutorials/flutter-timer/),我只是想知道如何动态更改 bloc 中字段的初始值。应该通过事件,可以直接或者什么是在 TimerBloc 类中定义持续时间参数的正确方法

import 'dart:async';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:fasting_app/models/ticker.dart';

part 'timer_event.dart';
part 'timer_state.dart';

class TimerBloc extends Bloc<TimerEvent, TimerState> {
  final Ticker _ticker;

  // this is what I want to change below in some widget outside
  static var _duration = 60;

  StreamSubscription<int>? _tickerSubscription;

  TimerBloc({required Ticker ticker})
      : _ticker = ticker,
        super(TimerInitial(_duration)) {
          //como cambiar este estado inicial dinamico
    on<TimerStarted>(_onStarted);
    on<TimerTicked>(_onTicked);
    on<TimerReset>(_onReset);
  }

  @override
  Future<void> close() {
    _tickerSubscription?.cancel();
    return super.close();
  }

  void _onStarted(TimerStarted event, Emitter<TimerState> emit) {
    _tickerSubscription?.cancel();

    emit(TimerRunInProgress(event.duration));
  //como cambiar los ticks dinamicamente
      _tickerSubscription = _ticker
        .tick(ticks: _duration)
        .listen((duration) => add(TimerTicked(duration)));
  }

  void _onTicked(TimerTicked event, Emitter<TimerState> emit) {
    //if event== state 60==60 then stop
    emit(event.duration != state.duration
        ? TimerRunInProgress(event.duration)

        /// triggers the TimerRunInProgress state
        : TimerRunComplete(_duration));
  }

  void _onReset(TimerReset event, Emitter<TimerState> emit) {
    /// Timer counting finished, so we must cancel the subscription
    _tickerSubscription?.cancel();
    emit(TimerInitial(_duration));
  }
}

我试过这个

Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: SizedBox(
                    width: 100,
                    height: 50,
                    child: ElevatedButton(
                      onPressed: () {  
//I have tried this but does not access to _duration field at bloc just                       
                        context.read<TimerBloc>().add(const TimerStarted(startTime));
                      },
                      child: const Text('START'),
                    ),
                  ),
                ),
flutter dart timer bloc
1个回答
0
投票

添加新活动:

class TimerDurationChanged extends TimerEvent 
  final int duration;
  const TimerDurationChanged(this.duration);

在您的集团中处理此事件:

class TimerBloc extends Bloc<TimerEvent, TimerState> {
      final Ticker _ticker;
      int _duration = 60; // Make this non-static to modify per instance
    
      StreamSubscription<int>? _tickerSubscription;
    
      TimerBloc({required Ticker ticker})
          : _ticker = ticker,
            super(TimerInitial(60)) {
        on<TimerStarted>(_onStarted);
        on<TimerTicked>(_onTicked);
        on<TimerReset>(_onReset);
        on<TimerDurationChanged>(_onDurationChanged); //add this
      }
    
      void _onDurationChanged(TimerDurationChanged event, Emitter<TimerState> emit) {
        _duration = event.duration;
        emit(TimerInitial(_duration));
      }

然后:

 ElevatedButton(
  onPressed: () {  
    context.read<TimerBloc>().add(TimerDurationChanged(120));
  },
  child: const Text('CHANGE DURATION'),
),
ElevatedButton(
  onPressed: () {  
    context.read<TimerBloc>().add(TimerStarted(context.read<TimerBloc>().state.duration));
  },
  child: const Text('START'),
),
© www.soinside.com 2019 - 2024. All rights reserved.