【问题标题】:Using bloc and dropdown menu in flutter在颤动中使用块和下拉菜单
【发布时间】:2019-09-30 17:40:24
【问题描述】:

您好,我是 Flutter 的新手,并且一直在通过 Flutter 的 udacity 课程构建他们的单位转换器应用程序以尝试了解该框架。我试图使用 bloc 构建应用程序,但我的下拉菜单遇到了问题。每次我更改下拉列表中的项目时,它都会在关注文本输入字段时重置回默认值。它看起来像我在关注文本字段时重建的小部件树。默认单位是重置,因为在我的 bloc 构造函数中,我有一个设置默认单位的方法。我不知道在哪里移动我的默认单位方法,以免它发生冲突。只有在设置了不同的类别并且首次构建时,我应该在我的集团中设置默认单位。

我尝试使用 _currentCatController.stream.distinct 方法仅在传递不同数据时更新流,但这似乎也不起作用。我试图将默认单位方法包装在各种条件语句中,但这些语句并没有给我想要的结果。

你可以在这里找到所有的来源https://github.com/Renzo-Olivares/Units_Flutter

class _ConverterScreenState extends State<ConverterScreen> {
  ///function that creates dropdown widget
  Widget _buildDropdown(
      bool selectionType, ValueChanged<dynamic> changeFunction) {
    print("build dropdown");
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 15.0),
      child: Container(
        decoration: BoxDecoration(
            border: Border.all(color: Colors.black, style: BorderStyle.solid),
            borderRadius: BorderRadius.circular(4.0)),
        child: DropdownButtonHideUnderline(
          child: ButtonTheme(
              alignedDropdown: true,
              child: StreamBuilder<Unit>(
                  stream: _conversionBloc.inputUnit,
                  initialData: widget._category.units[0],
                  builder: (context, snapshotIn) {
                    return StreamBuilder<Unit>(
                        stream: _conversionBloc.outputUnit,
                        initialData: widget._category.units[1],
                        builder: (context, snapshotOut) {
                          return StreamBuilder<Category>(
                              stream: _conversionBloc.currentCategory,
                              initialData: widget._category,
                              builder: (context, snapshotDropdown) {
                                return DropdownButton(
                                  items: snapshotDropdown.data.units
                                      .map(_buildDropdownItem)
                                      .toList(),
                                  value: selectionType
                                      ? snapshotIn.data.name
                                      : snapshotOut.data.name,
                                  onChanged: changeFunction,
                                  isExpanded: true,
                                  hint: Text("Select Units",
                                      style: TextStyle(
                                        color: Colors.black,
                                      )),
                                );
                              });
                        });
                  })),
        ),
      ),
    );
  }
}

class ConversionBloc {
  //input
  final _currentCatController = StreamController<Category>();
  Sink<Category> get currentCat => _currentCatController.sink;

  final _currentCatSubject = BehaviorSubject<Category>();
  Stream<Category> get currentCategory => _currentCatSubject.stream;

  ConversionBloc() {
    print("conversion bloc");
    //category
    _currentCatController.stream.listen((category) {
      print("setting category ${category.name}");
      _category = category;
      _currentCatSubject.sink.add(_category);

      //units default
      setDefaultUnits(_category);
    });
  }

  void setDefaultUnits(Category category) {
    print("setting default units for ${category.name}");
    _inputUnits = category.units[0];
    _outputUnits = category.units[1];
    _inputUnitSubject.sink.add(_inputUnits);
    _outputUnitSubject.add(_outputUnits);
  }
}

【问题讨论】:

    标签: dart flutter bloc rxdart


    【解决方案1】:

    这里的问题是 DropdownButton value 没有在 onChanged 上更新。您可以在这里做的是处理从 onChanged 传递的值并更新 DropdownButton 值。此外,关注屏幕上显示的 Widget 不应重建 Widget build()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 2021-09-17
      • 2020-02-06
      • 2023-04-08
      相关资源
      最近更新 更多