在 showModalBottomSheet 中处理 TextEditingController

问题描述

我有一个简单的应用程序,它显示一个模态底部工作表。这是代码

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key,this.title}) : super(key: key);
  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.black,title: Text(widget.title),actions: <Widget>[
            IconButton(
              icon: Icon(
                Icons.settings,color: Colors.white,),onpressed: () {
                _settingNotesModalBottomSheet(context);
              },)
          ],backgroundColor: Colors.black45,body: MyWidget()
        );
  }
}


void _settingNotesModalBottomSheet(context) {
  TextEditingController userName = TextEditingController();

  showModalBottomSheet(
    useRootNavigator: true,isdismissible: true,isScrollControlled: true,backgroundColor: Colors.white,context: context,builder: (BuildContext context) {
      return StatefulBuilder(builder: (BuildContext context,StateSetter setModalState) {
        return Container(
          padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),child: SingleChildScrollView(
            child: SafeArea(
              child: Wrap(
                children: <Widget>[
                  
                  Padding(
                    padding: const EdgeInsets.only(top: 20.0),child: Center(
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                            primary: Colors.purple,padding: EdgeInsets.symmetric(horizontal: 25,vertical: 10),textStyle: TextStyle(fontSize: 24,fontWeight: FontWeight.bold)),child: Text('Update'),onpressed: () async {
                          userName.dispose();
                          Navigator.pop(context);
                        },],);
      });
    },);
}

现在,当我运行此代码并按下按钮时,出现此错误在处理后使用了 TextEditingController。

现在是我的两个问题:

  • 我是否需要处理 textEditingController ?不确定当我运行 Navigator.pop() 时控制器是否被破坏
  • 如果我需要处理它,我该怎么做?

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)