在项目中经常需要返回特定数据,或者传递参数给上一页
返回1
onPressed: () { Map infos = {}; infos["nickname"] = nicknamelast; infos["avatar"] = avatar; Navigator.of(context).pop(infos); } 复制代码
返回2
Flutter中可以通过WillPopScope来实现返回按钮拦截
onWillPop是一个WillPopScope的回调函数,当用户点击返回按钮时被调用
WillPopScope必须包含child,否则会报错
WillPopScope( child:..., onWillPop: () { Map infos = {}; infos["nickname"] = nicknamelast; infos["avatar"] = avatar; Navigator.of(context).pop(infos); }, 复制代码
源码:
// Copyright 2014 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // @dart = 2.8 import 'framework.dart'; import 'navigator.dart'; import 'routes.dart'; ///为用户否决尝试注册一个回调,以取消封闭 ///[ModalRoute]。 /// ///也可以看看: /// ///*[ModalRoute.addScopedWillPopCallback]和[ModalRoute.removeScopedWillPopCallback], ///此小部件用来注册和注销[onWillPop]的代码。 class WillPopScope extends StatefulWidget { ///创建一个小部件,该小部件注册用户对否决尝试的回调 ///关闭封闭的[ModalRoute]。 /// ///[child]参数不能为null。 const WillPopScope({ Key key, @required this.child, @required this.onWillPop, }) : assert(child != null), super(key: key); /// The widget below this widget in the tree. /// /// {@macro flutter.widgets.child} final Widget child; ///由用户调用否决尝试以消除封闭的[ModalRoute]。 /// ///如果回调返回的Future解析为false,则包含 ///不会弹出路线。 final WillPopCallback onWillPop; @override _WillPopScopeState createState() => _WillPopScopeState(); } class _WillPopScopeState extends State<WillPopScope> { ModalRoute<dynamic> _route; @override void didChangeDependencies() { super.didChangeDependencies(); if (widget.onWillPop != null) _route?.removeScopedWillPopCallback(widget.onWillPop); _route = ModalRoute.of(context); if (widget.onWillPop != null) _route?.addScopedWillPopCallback(widget.onWillPop); } @override void didUpdateWidget(WillPopScope oldWidget) { super.didUpdateWidget(oldWidget); assert(_route == ModalRoute.of(context)); if (widget.onWillPop != oldWidget.onWillPop && _route != null) { if (oldWidget.onWillPop != null) _route.removeScopedWillPopCallback(oldWidget.onWillPop); if (widget.onWillPop != null) _route.addScopedWillPopCallback(widget.onWillPop); } } @override void dispose() { if (widget.onWillPop != null) _route?.removeScopedWillPopCallback(widget.onWillPop); super.dispose(); } @override Widget build(BuildContext context) => widget.child; } 复制代码
接受
onPressed: () { Navigator.pushNamed(context, route.OpenPersonInfoPage) .then((value) { setState(() { print(value.toString()); Map agrs = value; Config.UInfo.nickname = agrs["nickname"]; Config.UInfo.avatar = agrs["avatar"]; }); 复制代码
这样的话,就可以在按返回按钮的时候返回数据,
在这儿还可以向大家介绍如何双击退出程序
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:fluttertoast/fluttertoast.dart'; void main() { runApp(MyApp()); } class MyApp extends StatefulWidget { @override State<StatefulWidget> createState() { return WillPopTest(); } } class WillPopTest extends State { DateTime _lastTime; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( backgroundColor: Colors.blue, centerTitle: true, title: Text( '实现二次点击退出', style: TextStyle(fontSize: 18, color: Colors.white), ), ), body: WillPopScope( onWillPop: ()async{ if(_lastTime==null||DateTime.now().difference(_lastTime)>Duration(seconds: 1)){ _lastTime=DateTime.now(); Fluttertoast.showToast(msg: "再点击一次退出app", fontSize: 16, backgroundColor: Colors.blue, textColor: Colors.white ); return false; } return true; }, child: Container( alignment: Alignment.center, child: Text( "1秒内连续按两次返回键退出", style: TextStyle(fontSize: 30), ), ), ), ), ); } }