代码之家  ›  专栏  ›  技术社区  ›  gill

如何通过在Flutter中双击后退按钮来关闭应用程序?

  •  0
  • gill  · 技术社区  · 2 年前

    当您按下后退按钮时,您可以转到网络视图中的上一页。此外,我们还想补充一点,当您双击后退时,应用程序将关闭。

    https://pub.dev/packages/double_back_to_close_app

    我想我可以使用它,但我不知道如何同时应用这两个函数。

    这是我创建的代码。

    import 'package:flutter/material.dart';
    import 'package:webview_flutter/webview_flutter.dart';
    
    
    final uri = Uri.parse('https://google.com');
    
    class HomeScreen extends StatelessWidget {
    
      WebViewController controller = WebViewController()
        ..setJavaScriptMode(JavaScriptMode.unrestricted)
        ..setNavigationDelegate(NavigationDelegate(
          onPageFinished: (String url){
            print(url);
          }
        ))
        ..loadRequest(uri);
    
      HomeScreen({Key? key}) : super(key: key);
    
    
      @override
      Widget build(BuildContext context) {
    
        return PopScope(
          canPop: /* BOOL VALUE THAT CHECK IF ITS OK TO CLOSE APP OR NOT , YOU MAY PASS FALSE:)*/ false ,
          onPopInvoked: (didPop) {
            //FUNCTION THAT CALL WHEN EVER THE BACK CLICKED :)
            controller?.goBack();
          },
          child: Scaffold(
    
            floatingActionButton: Container(
              height: 40.0,
              width: 40.0,
              child: FittedBox(
                child: FloatingActionButton(
                  child: Icon(Icons.arrow_back),
                  onPressed: () => controller?.goBack(),
                  backgroundColor:  Colors.grey.shade200,
                ),
              ),
            ),
    
            body: SafeArea(
              child : WebViewWidget(
                controller: controller,
              ),
            ),
          ),
        );
    
      }
    }
    
    2 回复  |  直到 2 年前
        1
  •  1
  •   Munsif Ali    2 年前

    定义 currentPress 构建方法上方的变量,如下所示:

    DateTime? currentPress;
    

    和使用 PopScope 处理背面压力

    PopScope(
          canPop: false,
          onPopInvoked: (didPop) async {
            final now = DateTime.now();
            if (currentPress == null ||
                now.difference(currentPress!) > const Duration(seconds: 2)) {
              currentPress = now;
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(
                  content: Text('Press back again to exit'),
                  duration: Duration(seconds: 1),
                ),
              );
              return;
            } else {
              controller?.goBack();
              SystemNavigator.pop();
            }
          },
          child:Scaffold(
    
            floatingActionButton: Container(
              height: 40.0,
              width: 40.0,
              child: FittedBox(
                child: FloatingActionButton(
                  child: Icon(Icons.arrow_back),
                  onPressed: () => controller?.goBack(),
                  backgroundColor:  Colors.grey.shade200,
                ),
              ),
            ),
    
            body: SafeArea(
              child : WebViewWidget(
                controller: controller,
              ),
            ),
          ),
        );
    
        2
  •  0
  •   Sukant    2 年前

    对onPressed call使用if检查。检查是否有可用路线,然后拨打 Controller?.goBack(): SystemNavigator.pop();

    使用三进制检查将节省空间,而且很容易。但是,您必须实现手动逻辑来检查可用的路由。

    我希望它有效。