我最近开始使用dart 和Flutter 翼编程,我的应用程序一切都很顺利,尽管最近我想增加下拉菜单,为用户提供多种 Select .但是,当我从列表中 Select 一个值时,它不会改变框中的值,它会返回到提示或空框.如有任何帮助,我们将不胜感激!

下面是我的下拉按钮代码:

Widget buildDropdownButton() {
String newValue;

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()),
      ),
    ],
  ),
);
}

推荐答案

错误是因为您声明了一个方法变量newValue,您必须在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()),
        ),
      ],
    ),
  );
  }

Flutter相关问答推荐

ListTile未正确显示在flutter中

带有可滚动页面(不包括分页区)的Flutter 页面视图

摆动如何更改弹出菜单项高亮显示 colored颜色 半径

如何正确处理Flutter 翼中使用火基时发生的碰撞?

'Flutter的无效常数值错误

Ffmpeg_kit_fltter:^6.0.3版权问题

flutter Listview构建器溢出

如何在 Flutter 中创建厚度沿着容器逐渐减小的边框

Flutter CarouselSlider中如何防止右图与放大的中心图重叠?

Flutter开发中,如何通过'nfc_manager'插件在Android设备上避免默认的New Tag Scanned弹出窗口?

是否有可能减少代码的编写,例如:ref.read(countProvider.notifier) - 在构建方法中?

如何让小部件根据小部件的大小构建不同的布局?

Flutterfire Configure - 未处理的异常和没有 firebase 初始化

当项目较少时收缩列表视图

Flutter Serverpod 在示例项目上返回 400 错误

SfCircularChart 周围的额外间距

有任何方法可以在应用程序移动背景恢复时显示 appopenAd

使用 Firebase/Flutter 进行应用判断 - 100% 无效请求

小部件库捕获的 Flutter 异常

在 Flutter 中更新对象实例属性的最佳实践是什么?该实例嵌套在提供程序类的映射中,如下所示