Flutter 展示模型底板后CupertinoPicker的颤振设置变量

Flutter 展示模型底板后CupertinoPicker的颤振设置变量,flutter,dart,Flutter,Dart,我指的是这个答案: 尝试在bottommodal用WhenComplete关闭时获取selectedValue WhenComplete()中的函数被调用,但当我打印selectedValue时,它只作为默认值0响应。我已经检查并选择了旋转时值确实会改变,但是当我关闭模式时没有检索到正确的值,我假设它与异步或范围有关。请分享任何建议 我的班级如下: class PopupButton extends StatefulWidget { final String initialValue;

我指的是这个答案:

尝试在bottommodal用WhenComplete关闭时获取selectedValue

WhenComplete()中的函数被调用,但当我打印selectedValue时,它只作为默认值0响应。我已经检查并选择了旋转时值确实会改变,但是当我关闭模式时没有检索到正确的值,我假设它与异步或范围有关。请分享任何建议

我的班级如下:


class PopupButton extends StatefulWidget {
  final String initialValue;
  final List<dynamic> options;
  final Function callback;
  int initialIndex;
  PopupButton(
      {@required this.initialValue,
      @required this.options,
      this.callback,
      this.initialIndex = 0});

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

class _PopupButtonState extends State<PopupButton> {
  int selectedValue;
  String text = "";
  void showPicker(
    BuildContext context,
  ) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return Container(
            height: 200,
            child: CupertinoPicker(
                itemExtent: 32,
                onSelectedItemChanged: (value) {
                  if (mounted) {
                    setState(() {
                      selectedValue = value;
                    });
                  }
                },
                children: (widget.options.map((option) => Text(
                      option,
                      style: TextStyles.headerLight,
                    ))).toList()),
          );
        }).whenComplete(() {
// THIS ALWAYS PRINTS 0
      print("Selected: $selectedValue");
      if (widget.callback != null) {
        widget.callback(selectedValue);
      }
      TextEditingController();
      return;
    });
  }

  @override
  Widget build(BuildContext context) {
    selectedValue = widget.initialIndex;
    text = widget.initialValue;
    return GestureDetector(
      child: Text(text),
      onTap: () {
        showPicker(context);
      },
    );
  }
}

类PopupButton扩展StatefulWidget{
最终字符串初始值;
最后清单选择;
最终函数回调;
int初始索引;
弹出按钮(
{@required this.initialValue,
@需要此选项,
这个.回调,,
this.initialIndex=0});
@凌驾
_PopupButtonState createState()=>\u PopupButtonState();
}
类PopupButtonState扩展状态{
int选择值;
字符串文本=”;
无效显示选择器(
构建上下文上下文,
) {
showModalBottomSheet(
上下文:上下文,
生成器:(BuildContext上下文){
返回容器(
身高:200,
孩子:CupertinoPicker(
项目范围:32,
onSelectedItemChanged:(值){
如果(已安装){
设置状态(){
selectedValue=值;
});
}
},
子项:(widget.options.map((option)=>Text(
选项
样式:TextStyles.headerLight,
))).toList()),
);
}).完成时(){
//这总是打印0
打印(“选定:$selectedValue”);
if(widget.callback!=null){
widget.callback(selectedValue);
}
TextEditingController();
返回;
});
}
@凌驾
小部件构建(构建上下文){
selectedValue=widget.initialIndex;
text=widget.initialValue;
返回手势检测器(
子:文本(Text),
onTap:(){
showPicker(上下文);
},
);
}
}

您的错误是在生成方法中初始化
selectedValue
。 每次你打电话

setState(() {
  selectedValue = value;
});
selectedValue
widget.initialIndex
启动

请移动
selectedValue=widget.initialIndex
initState
方法

@override
void initState() {
  super.initState();
  selectedValue = widget.initialIndex;
  text = widget.initialValue;
}

@override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Text(text),
      onTap: () {
        showPicker(context);
      },
    );
  }