有什么办法可以在Android的Flutter应用中拦截“返回”键按下吗?

有什么办法可以在Android的Flutter应用中拦截“返回”键按下吗?,第1张

有什么办法可以在Android的Flutter应用中拦截“返回”键按下吗?

我发现解决方案是使用

WillPopScope
部件。这是下面的最终代码:

import 'dart:async';import 'package:flutter/material.dart';class NewEntry extends StatefulWidget {  NewEntry({Key key, this.title}) :super(key: key);  final String title;  @override  State<StatefulWidget> createState() => new _NewEntryState();}class _NewEntryState extends State<NewEntry> {  Future<bool> _onWillPop() {    return showDialog(      context: context,      child: new alertDialog(        title: new Text('Are you sure?'),        content: new Text('Unsaved data will be lost.'),        actions: <Widget>[          new FlatButton( onPressed: () => Navigator.of(context).pop(false), child: new Text('No'),          ),          new FlatButton( onPressed: () => Navigator.of(context).pop(true), child: new Text('Yes'),          ),        ],      ),    ) ?? false;  }  @override  Widget build(BuildContext context) {    return new WillPopScope(      onWillPop: _onWillPop,      child: new Scaffold(        appBar: new AppBar(          title: new Text(widget.title),        ),        floatingActionButton: new FloatingActionButton(          child: new Icon(Icons.edit),          onPressed: () {},        ),      ),    );  }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4914641.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-13
下一篇2022-11-12

发表评论

登录后才能评论

评论列表(0条)

    保存