Flutter覆盖多个WillPops的后退按钮

时间:2019-07-13 06:45:32

标签: flutter

简单地说,您假设我有3个对话框:

return showGetnameDialog(
      context: context,
      builder: (context) => new AlertDialog(
        ...
      ),
    ) ?? false;

return showGetFamilyDialog(
      context: context,
      builder: (context) => new AlertDialog(
        ...
      ),
    ) ?? false;

return showGetAgeDialog(
      context: context,
      builder: (context) => new AlertDialog(
        ...
      ),
    ) ?? false;

例如,现在WillPopScope仅获得一个onWillPop参数

_onWillPop,: _onWillPop,

我如何管理多个willPops以覆盖后退按钮?例如:

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

  final String title;

  @override
  State<StatefulWidget> createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage> {

    Future<bool> _onNameWillPop() {
        return showGetnameDialog(
          context: context,
          builder: (context) => new AlertDialog(
            ...
          ),
        ) ?? false;
    }

    Future<bool> _onFamilyWillPop() {
        return showGetFamilyDialog(
          context: context,
          builder: (context) => new AlertDialog(
            ...
          ),
        ) ?? false;
    }

    Future<bool> _onAgeWillPop() {
        return showGetAgeDialog(
          context: context,
          builder: (context) => new AlertDialog(
            ...
          ),
        ) ?? false;
    }

  @override
  Widget build(BuildContext context) {
    return new WillPopScope(
      onWillPop: _onNameWillPop(), /*_onFamilyWillPop(), _onAgeWillPop*/
      child: new Scaffold(
        appBar: new AppBar(
          title: new Text("Home Page"),
        ),
        body: new Center(
          child: new Text("Home Page"),
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

尝试一下:

@override
  Widget build(BuildContext context) {
    return new WillPopScope(
      onWillPop: () {
       Navigator.pop(context, false);
      }
      child: new Scaffold(
        appBar: new AppBar(
          title: new Text("Home Page"),
        ),
        body: new Center(
          child: new Text("Home Page"),
        ),
      ),
    );
  }