首页 > 解决方案 > WillPopScope 不适用于其他小部件

问题描述

我的颤振应用程序中有多个小部件。如果我单击另一个小部件,则在顶部定义的 onWillPopScope 方法Widget build(BuildContext context)不起作用。你能给我建议如何解决它吗?谢谢

见下面的代码:

 ExpansionTile(
                                  initiallyExpanded: false,
                                  title:
                                  Text('Solution', style: basicAlertBlack),
                                  children: <Widget>[
                                    Container(
                                      color: Colors.amber.shade50,
                                      height:
                                      MediaQuery
                                          .of(context)
                                          .size
                                          .height /
                                          7,
                                      child: _solutionTextField(),
                                    )
                                  ],
                                ),

第二个小部件的代码:

 Widget _solutionTextField() {
 return TextField(

    focusNode: myFocusNode,
    keyboardType: TextInputType.multiline,
    maxLines: null,
    style: solution,
    controller: solutionController,
    decoration: new InputDecoration(
        contentPadding: EdgeInsets.all(8),
        focusedBorder: InputBorder.none,
        enabledBorder: InputBorder.none,
        errorBorder: InputBorder.none,
        disabledBorder: InputBorder.none,
        hintText: 'Enter a solution'));

}

标签: flutterdartwidgettextfieldback-button-control

解决方案


你能提供更多的上下文吗?

正如文件所说:

WillPopScope 类

注册回调以否决用户尝试关闭封闭的 ModalRoute。

这意味着它将被调用pop。可以通过单击返回箭头从 android 导航中调用它,或者通过从边缘左侧向右滑动来调用 iOS。pop以及您通过调用的所有其他方法Navigator。所以它与点击 Widget 没有关系。


推荐阅读