【问题标题】:I want to exit back button app using willpopscope code with my code我想使用 willpopscope 代码和我的代码退出后退按钮应用程序
【发布时间】:2020-07-11 19:02:29
【问题描述】:

我想使用 willpopscope 代码和我的代码退出后退按钮应用程序。 就不能这样写代码吗?

当使用另一个返回按钮应用退出代码 xxxx: xxxx 错误。

而且下面的代码也报错了。

我正在寻找方法

请指教

  class HomeScreen extends StatelessWidget {
  DateTime currentBackPressTime;

  final scaffoldKey = GlobalKey<ScaffoldState>();

  final FirebaseUser user;
  HomeScreen({this.user});

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async{
        bool result = onPressBackButton();
        return await Future.value(result);
        },
      child: MaterialApp(
              title: 'Custom Navigation Drawer Demo',
              theme: ThemeData(
                primarySwatch: Colors.blue,
              ),
              debugShowCheckedModeBanner: false,
              home: MyHomePage(
              ),
      ),
    );
  }

  bool onPressBackButton() {
    DateTime now = DateTime.now();
    if (currentBackPressTime == null ||
        now.difference(currentBackPressTime) > Duration(seconds: 2)) {
      currentBackPressTime = now;
      scaffoldKey.currentState
        ..hideCurrentSnackBar()
        ..showSnackBar(SnackBar(
          content: Text("Tap back again to leave."),
        ));
      return false;
    }
    return true;
  }

}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您的 WillPopScope 小部件放错了位置。

    您必须将它放在具有导航器的 MaterialApp 小部件中才能使其工作。

    Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Custom Navigation Drawer Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      debugShowCheckedModeBanner: false,
      home: WillPopScope(
        onWillPop: () async {
          bool result = onPressBackButton();
          return await Future.value(result);
        },
        child: MyHomePage(),
      ),
    );
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 2022-12-19
      • 1970-01-01
      相关资源
      最近更新 更多