【问题标题】:Even though the backbutton close is implemented, it does not work即使实现了后退按钮关闭,它也不起作用
【发布时间】:2020-04-05 22:38:12
【问题描述】:

即使我实现了后退按钮应用程序关闭。如果您按下返回按钮,则不会出现任何消​​息,您将返回登录屏幕。

主页正在主屏幕中运行。 为了找出任何可能的错误,我在两个地方都放了后退按钮关闭代码。

我不知道出了什么问题。

我需要帮助。

主屏幕

class HomeScreen extends StatelessWidget {
  DateTime currentBackPressTime;

  final scaffoldKey = GlobalKey<ScaffoldState>();

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

  @override
  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(),
      ),
    );
  }

  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;
  }

}

  class MyHomePage extends StatelessWidget {

    @override
    Widget build(BuildContext context) {
      return MaterialApp(
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
              scaffoldBackgroundColor: Colors.white,
              primaryColor: Colors.white
          ),
          home: SideBarLayout(),
      );
    }
  }

主屏幕

import 'package:flutter/material.dart';
import 'package:aciel_pro/navigation_bloc/navigation_bloc.dart';


class Home extends StatelessWidget {
  DateTime currentBackPressTime;

  final scaffoldKey = GlobalKey<ScaffoldState>();


  @override
  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: HomePage(),
      ),
    );
  }

  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;
  }

}



class HomePage extends StatelessWidget with NavigationStates {
  BuildContext ctx;


  @override
  Widget build(BuildContext context) {
    ctx = context;

    return Scaffold(
        body: Center(
          child: Column(
          children: <Widget>[
            FlatButton(
              child: Text('예약하기',style: TextStyle(fontSize: 40)),
              onPressed: () =>showMessage('예약하기'),
              color:Colors.green,
              textColor: Colors.white,
            )
          ],
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        ),
        ),
    );
  }

  void showMessage(String msg) {
    final snackbar = SnackBar(content: Text(msg));

    Scaffold.of(ctx)
      ..removeCurrentSnackBar()
      ..showSnackBar(snackbar);
  }
}

【问题讨论】:

    标签: android flutter


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    第 1 步:将final scaffoldKey = GlobalKey&lt;ScaffoldState&gt;(); 移出Home
    第二步:添加Scaffold key

    return Scaffold(
          key: scaffoldKey,
    

    第 3 步:showMessage() 使用 scaffoldKey

    scaffoldKey.currentState
      ..removeCurrentSnackBar()
      ..showSnackBar(snackbar);
    

    工作演示

    完整代码

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(Home());
    }
    
    final scaffoldKey = GlobalKey<ScaffoldState>();
    
    class Home extends StatelessWidget {
      DateTime currentBackPressTime;
    
      @override
      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: HomePage(),
          ),
        );
      }
    
      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;
      }
    }
    
    class HomePage extends StatelessWidget {
      BuildContext ctx;
    
      @override
      Widget build(BuildContext context) {
        ctx = context;
    
        return Scaffold(
          key: scaffoldKey,
          body: Center(
            child: Column(
              children: <Widget>[
                FlatButton(
                  child: Text('예약하기', style: TextStyle(fontSize: 40)),
                  onPressed: () => showMessage('예약하기'),
                  color: Colors.green,
                  textColor: Colors.white,
                )
              ],
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            ),
          ),
        );
      }
    
      void showMessage(String msg) {
        final snackbar = SnackBar(content: Text(msg));
    
        scaffoldKey.currentState
          ..removeCurrentSnackBar()
          ..showSnackBar(snackbar);
      }
    }
    

    【讨论】:

    • 感谢您提供有用的信息。但是如果登录页面有问题,它会继续出现。想必大家的答案都差不多,所以里面有问题,另外上下全局键在两边都崩溃了(后退键、导航)。我解决这个问题可以吗?
    • 在我的示例中,所有测试都可以。需要使用更多代码重现您的问题。
    • 好的,非常感谢 :) 我会寻找更多信息并寻求帮助。谢谢你的好意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    • 2014-12-27
    • 2014-05-06
    相关资源
    最近更新 更多