我需要在用户通过按BackAndroid设备上的按钮导航离开当前路线之前显示警告对话框.我试图通过WidgetsBindingObserver在小部件状态中实现来拦截后退按钮行为.关于同一主题,GitHub上有一个封闭的问题.但是我的代码没有工作,因为方法didPopRoute()从未被调用过.这是我的代码如下:
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> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
Future<bool> didPopRoute() {
return showDialog(
context: context,
child: new AlertDialog(
title: new Text('Are you sure?'),
content: new Text('Unsaved data will be lost.'),
actions: <Widget>[
new …Run Code Online (Sandbox Code Playgroud) 我正在创建一个应用程序,我可以完全手动控制每次后退按钮的按下。
我基本上在我的应用程序中更改了小部件/视图,而不是导航器推送和弹出新屏幕。
我创建了一个功能,可以显示视图 1,2 和 3。并使用 Android 上的后退按钮返回。然而,在 iOS 上,后退手势不起作用,我无法更改视图。
有什么解决方法吗?如有帮助,将不胜感激
Future<bool> goBackward(){
if(_isButtonPressed) {
if(currentView == 0)
{
return Future<bool>.value(true);
}
else if(currentView == 1)
{
currentView = 0;
notifyListeners();
return Future<bool>.value(false);
}
else
{
currentView = 2;
notifyListeners();
return Future<bool>.value(false);
}
}
}
Run Code Online (Sandbox Code Playgroud)
以上是代码的最小版本。当我返回 true 时,这意味着我已准备好在后按时弹出屏幕
I want to get a callback when the screen is changed, so I can stop my recurring request that runs on that specific screen. “dispose” is only called when Navigator.pop is used, but not when Navigator.push is called. Is there a way to detect that the screen is changed and not being shown at the moment?