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

前端 未结 2 2011
梦毁少年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<String, dynamic> mp;
      @override
      _MessageUSScreenFilterBodyState createState() =>
          _MessageUSScreenFilterBodyState();
    }
    
    class _MessageUSScreenFilterBodyState extends State<MessageUSScreenFilterBody> {
      int _selectedId;
    
      @override
      Widget build(BuildContext context) {
        return DropdownButton<int>(
          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;
            });
          },
        );
      }
    }
    
    0 讨论(0)
  • 2021-02-07 11:59

    The error is because you are declaring a method variable newValue you must declare that variable as global inside your StatefulWidget.

       String newValue;
    
      Widget buildDropdownButton() {
      return new Padding(
        padding: const EdgeInsets.all(24.0),
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            new ListTile(
              title: const Text('Frosting'),
              trailing: new DropdownButton<String>(
                  hint: Text('Choose'),
                  onChanged: (String changedValue) {
                    newValue=changedValue;
                    setState(() {
                      newValue;
                      print(newValue);
                    });
                  },
                  value: newValue,
                  items: <String>['None', 'Chocolate', 'Vanilla', 'ButterCream']
                      .map((String value) {
                    return new DropdownMenuItem<String>(
                      value: value,
                      child: new Text(value),
                    );
                  }).toList()),
            ),
          ],
        ),
      );
      }
    
    0 讨论(0)
提交回复
热议问题