【问题标题】:Flutter - DropdownButton onChanged returning nullFlutter - DropdownButton onChanged 返回 null
【发布时间】:2020-12-29 21:18:33
【问题描述】:

我正在创建一个下拉列表,但是每当我单击一个值并尝试在 onChanged 部分中打印它时,它都会打印 null,我做错了什么?谢谢。这是我的代码:

  List<UserFontModel> _fonts = [
    UserFontModel(fontFamily: 'Regular', fontWeight: FontWeight.w400),
    UserFontModel(fontFamily: 'Bold', fontWeight: FontWeight.w700),
    UserFontModel(fontFamily: 'Medium', fontWeight: FontWeight.w500),
    UserFontModel(fontFamily: 'Light', fontWeight: FontWeight.w300),
    UserFontModel(fontFamily: 'Thin', fontWeight: FontWeight.w100),
  ];

  String _selectedFontStyle;

    new DropdownButton<String>(
                      value: _selectedFontStyle,
                      hint: Text('Style'),
                      items: _fonts.map((fonts) => DropdownMenuItem<String> (
                        child: Container(
                            width: MediaQuery.of(context).size.width * 0.2,
                          child: Text(fonts.fontFamily, style: TextStyle(fontWeight: fonts.fontWeight),),
                        ),
                      )).toList(),
                      onChanged: (String _) {
                          print(_);
                      },
                    ),

当我选择一个项目时,我会显示:

flutter: null

有人可以帮忙吗?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    请试试这个。

    List<UserFontModel> _fonts = [
        UserFontModel(fontFamily: 'Regular', fontWeight: FontWeight.w400),
        UserFontModel(fontFamily: 'Bold', fontWeight: FontWeight.w700),
        UserFontModel(fontFamily: 'Medium', fontWeight: FontWeight.w500),
        UserFontModel(fontFamily: 'Light', fontWeight: FontWeight.w300),
        UserFontModel(fontFamily: 'Thin', fontWeight: FontWeight.w100),
      ];
    
      String _selectedFontStyle;
    
    DropdownButton<String>(
      hint: Text("Style"),
      value: _selectedFontStyle,
      onChanged: (String Value) {
        setState(() {
           _selectedFontStyle = Value;
          });
         },
      items: _fonts.map((fonts) {
        return  DropdownMenuItem<String>(
            value: fonts.fontFamily,
            child: new Container(
              width: MediaQuery.of(context).size.width * 0.2,
              child: Text(fonts.fontFamily, style: TextStyle(fontWeight: fonts.fontWeight),),
            )
        );
      }).toList(),
    ),
    

    【讨论】:

    • 不客气 :)
    【解决方案2】:

    这对我有用:

        new DropdownButton<UserFontModel>(
          value: _fonts[0],
          hint: Text('Style'),
          items: _fonts
              .map<DropdownMenuItem<UserFontModel>>((UserFontModel font) {
            return DropdownMenuItem<UserFontModel>(
              value: font,
              child: Text(
                font.fontFamily,
                style: TextStyle(fontWeight: font.fontWeight),
              ),
            );
          }).toList(),
          onChanged: (UserFontModel _) {
            print(_.fontFamily);
          },
        ),
    

    【讨论】:

      【解决方案3】:

      DropdownMenuItem 返回它的值字段。所以你需要给孩子增加价值。

      像这样试试。

      new DropdownButton<String>(
                              value: _selectedFontStyle,
                              hint: Text('Style'),
                              items: _fonts.map((fonts) => DropdownMenuItem<String> (
                                child: Container(
                                  width: MediaQuery.of(context).size.width * 0.2,
                                  child: Text(fonts.fontFamily, style: TextStyle(fontWeight: fonts.fontWeight),),
                                ),
                                value: fonts.fontFamily, // add this line
                              )).toList(),
                              onChanged: (String _) {
                                print(_);
                              },
                            ),
      

      【讨论】:

        猜你喜欢
        • 2021-08-23
        • 2020-08-23
        • 2021-09-28
        • 2020-11-10
        • 2020-07-12
        • 2021-06-14
        • 2021-04-19
        • 2021-06-29
        • 2019-11-22
        相关资源
        最近更新 更多