【问题标题】:Flutter - How to implement a Dropdown List using BloC?Flutter - 如何使用 BloC 实现下拉列表?
【发布时间】:2020-04-26 12:30:45
【问题描述】:

我想在我的应用程序中添加一个包含“名称”的下拉列表,而不是像下面的代码中那样添加一个 TextField。 我如何使用 BloC 做到这一点?

class PersonPage extends StatefulWidget {

  PersonPage(this.person);
  final Person person;

  @override
  _PersonPageState createState() => _PersonPageState();
}

class Names{
  const Item(this.name);
  final String name;
}

class _PersonPageState extends State<PersonPage> {
  Item selectedUser;
  List<Item> names = <Names>[
   const Item('Thomas'),
   const Item('John'),
   const Item('Mary'),
   const Item('Lukas'),
  ];
 TextEditingController _nameController;

 final _bloc = PersonBloc();

 @override
 void initState() {

  _bloc.setPerson(widget.person);
  _nameController = TextEditingController(text: widget.person.name);

  super.initState();
 }
@override
void dispose() {
 _nameController.dispose();
 super.dispose();
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text("MyApp"),
    ),
    body: Container(
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: ListView(
          children: <Widget>[             
            Container(
              child: TextField(
                decoration: InputDecoration(labelText: "Name"),
                controller: _nameController,
                onChanged: _bloc.setName,
              ),
            ),
            Container(
              height: 20,
            ),              
            RaisedButton(
              child: Text("Save"),
              onPressed: () {
                if (_bloc.insertOrUpdate()) {
                  Navigator.pop(context);
                }
              },
            )
          ],
        ),
      ),
    ),
  );
}

谢谢。

【问题讨论】:

    标签: flutter dart bloc


    【解决方案1】:

    我会给你一个例子,你尝试在你的代码中应用

    class _PersonPageState extends State<PersonPage> {
      final _bloc = PersonBloc();
    
      @override
      void initState() {
    
        _bloc.setPerson(widget.person);
        _nameController = TextEditingController(text: widget.person.name);
    
       super.initState();
      }
    
      @override
      void dispose() {
        _nameController.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text("MyApp"),
        ),
        body: Container(
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: ListView(
              children: <Widget>[             
                StreamBuilder(
                  stream: bloc.outName,
                  builder: (context, snapshot) {
                    if (!snapshot.hasData)
                      return Container();
    
                    return DropdownButton(
                      hint: Text("Names"),
                      value: snapshot.data,
                      items: bloc.names.map((item) {
                        return DropdownMenuItem(
                          value: item,
                          child: Row(children: <Widget>[Text(item),]),
                        );
                      }).toList(),
                      onChanged: bloc.inName,
                    );
                  },
                ),
                Container(
                  height: 20,
                ),              
                RaisedButton(
                  child: Text("Save"),
                  onPressed: () {
                    if (_bloc.insertOrUpdate()) {
                      Navigator.pop(context);
                    }
                  },
                )
              ],
            ),
          ),
        ),
      );
    }
    

    将您的姓名列表更改为您的 BLoC,并创建名称 BehaviorSubject

    PersonBloc{
    
      List<String> names = ['Thomas', 'John', 'Mary', 'Lukas'];
    
      final _name = BehaviorSubject<String>.seeded("");
      Stream<String> get outName  => _name.stream;
      Function(String) get inName => _name.sink.add;
      //TODO - Don't forget to dispose this _name
    
    }
    

    现在您的_name 具有选定的名称,要获取名称,您只需要这样做

    Person.name = _name.stream.value;
    

    这个例子可以改进,只是草稿

    【讨论】:

    • 嗨@Mateus,感谢您的反馈。我一直在测试您的建议,不幸的是它不起作用。它在屏幕上没有显示任何内容,我没有错误,但显示屏上没有显示任何内容。对此有何建议?
    • @Michael 我已经改变了我的例子,请再看看它是否有效。如果仍然不行,尝试像这样实例化seeded("Thomas")
    • 它现在可以工作了,但为此我不得不删除它:if (!snapshot.hasData) return Container();
    • 非常感谢...这正是我想要的。祝你一切顺利。 :)
    猜你喜欢
    • 2019-08-29
    • 2020-07-21
    • 2021-05-20
    • 1970-01-01
    • 2019-08-22
    • 2021-08-15
    • 2020-08-16
    • 1970-01-01
    • 2018-05-04
    相关资源
    最近更新 更多