栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

有什么办法可以在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: () {},        ),      ),    );  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373839.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号