Drop down button in flutter not switching values to the selected value

前端 未结 2 2010
梦毁少年i
梦毁少年i 2021-02-07 11:20

I\'ve recently started programming using dart and flutter and everything has been going smoothly for my app, although recently i wanted to add drop down menu to provide the user

2条回答
  •  既然无缘
    2021-02-07 11:51

    I had this problem although I was already using the solution above.

    for anyone who has this problem and the above solution does not work, try separating FutureBuilder from the dropdown. this is how your final code should look like:

    class TheFuture extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return FutureBuilder(
          future: myFuture(),
          builder: (ctx, snp) {
            if (!snp.hasData) return LoadingLine();
            return TheBuilder(snp.data);
          },
        );
      }
    }
    
    class TheBuilder extends StatefulWidget {
      const TheBuilder(this.mp);
      final Map mp;
      @override
      _MessageUSScreenFilterBodyState createState() =>
          _MessageUSScreenFilterBodyState();
    }
    
    class _MessageUSScreenFilterBodyState extends State {
      int _selectedId;
    
      @override
      Widget build(BuildContext context) {
        return DropdownButton(
          selectedItemBuilder: (context) =>
              widget.mp['myData'].map((e) => Text(e.type)).toList(),
          items: widget.mp['myData']
              .map(
                (e) => DropdownMenuItem(
                  child: Text(e.type),
                  value: e.id,
                ),
              )
              .toList(),
          value: _selectedId,
          onChanged: (int _id) {
            setState(() {
              _selectedId = _id;
            });
          },
        );
      }
    }
    

提交回复
热议问题