0

我有一个简单的应用程序,它显示了一个模态底页。这是代码

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

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

现在这是我的两个问题:

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

谢谢

4

0 回答 0