【问题标题】:How to change text field text value after popping from bottom sheet从底部表单弹出后如何更改文本字段文本值
【发布时间】:2020-11-01 03:16:51
【问题描述】:

单击文本字段时,我的代码会显示底部工作表小部件。底部工作表有一些按钮,单击并保存弹出底部工作表。但是,弹出后它会获取值,但不会将文本字段文本更改为该值。

我的代码:

  Widget _additionInformation() {
    TextEditingController statusController = TextEditingController();
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 40),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          TextFormField(
            onTap: () {
              showModalBottomSheet(
                  context: context,
                  isScrollControlled: false,
                  isDismissible: false,
                  builder: (context) => BottomSheetSettingWidget(
                      ['None', 'Yes', 'No'])).then((value) {
                setState(() {
                  print(value);
                  statusController.text = value;
                });
              });
            },
            controller: statusController,
            decoration: InputDecoration(
              fillColor: Colors.white,
              filled: true,
              contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 10),
              enabledBorder: OutlineInputBorder(
                  borderSide: BorderSide(color: Colors.grey[400])),
              border: OutlineInputBorder(
                  borderSide: BorderSide(color: Colors.grey[400])),
            ),
          ),
          
        ],
      ),
    );
  }

【问题讨论】:

  • 在弹出 showModalBottomSheet 后放置 setState
  • 我认为这就是我所做的。当底部的工作表弹出时,它会设置状态
  • 你已经把它放在了BottomSheetSettingWidget之后
  • 我把它放在底部表之后,因为 .then 之后的最后一个括号是底部表的一部分
  • 我的错,之前没看清楚。尝试在 showModalBottomSheet 之前放置 await

标签: flutter dart text


【解决方案1】:
showModalBottomSheet(
    context: context,
    builder: (context) {
      return StatefulBuilder(
          builder: (BuildContext context, StateSetter setState /*You can rename this!*/) {
        return Container(
          height: heightOfModalBottomSheet,
          child: RaisedButton(onPressed: () {
            setState(() {
              heightOfModalBottomSheet += 10;
            });
          }),
        );
      });
});

【讨论】:

    【解决方案2】:

    将context 和controller 作为参数传递给您创建的_additionInformation` 小部件,我在下面添加了一个演示代码:

    Widget _additionInformation(BuildContext context) {
        return Padding(
          padding: EdgeInsets.symmetric(horizontal: 40),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              TextFormField(
                onTap: () {
                  showModalBottomSheet(
                      context: context,
                      isScrollControlled: false,
                      isDismissible: false,
                      builder: (context) => BottomSheetSettingWidget(
                          ['None', 'Yes', 'No'])).then((value) {
                      print(value);
                      statusController.text = value;
                  });
                },
                controller: statusController,
                decoration: InputDecoration(
                  fillColor: Colors.white,
                  filled: true,
                  contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 10),
                  enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.grey[400])),
                  border: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.grey[400])),
                ),
              ),
              
            ],
          ),
        );
      }
    

    之后,在您的ProfileEditPage 中创建一个TextEditingController。赞TextEditingController statusController = TextEditingController();

    瞧!!快乐编码:)

    【讨论】:

    • 将它作为参数传递也没有用,但它在我的构建中有效
    • 你能创建一个 github gist 让我检查代码吗? @iKreateCode。问题肯定来自您如何调用方法 additionInformation 。我会看一下要点并在这里更新我的答案。
    • 已修复。 @iKreateCode,我更新了我的答案。问题在于controller,将其作为参数传递并在您的ProfileEditPageState 中定义一个。
    • 那行得通。但是如果我在同一方法中包含更多底部小部件,参数会不会很长?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多