Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 有没有办法拦截';背面';安卓上颤振应用程序的按键功能?_Android_Dart_Flutter - Fatal编程技术网

Android 有没有办法拦截';背面';安卓上颤振应用程序的按键功能?

Android 有没有办法拦截';背面';安卓上颤振应用程序的按键功能?,android,dart,flutter,Android,Dart,Flutter,在Android设备上,我需要在用户通过按下Back按钮离开当前路线之前显示一个警报对话框。我试图通过在小部件状态下实现来拦截后退按钮行为。关于同一主题,GitHub上有一个封闭的主题。但是,我的代码不起作用,因为从未调用过该方法。下面是我的代码: 导入'dart:async'; 进口“包装:颤振/材料.省道”; 类NewEntry扩展StatefulWidget{ NewEntry({Key-Key,this.title}):super(Key:Key); 最后的字符串标题; @凌驾 Sta

在Android设备上,我需要在用户通过按下
Back
按钮离开当前路线之前显示一个警报对话框。我试图通过在小部件状态下实现来拦截后退按钮行为。关于同一主题,GitHub上有一个封闭的主题。但是,我的代码不起作用,因为从未调用过该方法。下面是我的代码:

导入'dart:async';
进口“包装:颤振/材料.省道”;
类NewEntry扩展StatefulWidget{
NewEntry({Key-Key,this.title}):super(Key:Key);
最后的字符串标题;
@凌驾
State createState()=>new_NewEntryState();
}
类_NewEntryState使用WidgetsBindingObserver扩展状态{
@凌驾
void initState(){
super.initState();
WidgetsBinding.instance.addObserver(这个);
}
@凌驾
无效处置(){
WidgetsBinding.instance.removeObserver(此);
super.dispose();
}
@凌驾
未来路线(){
返回显示对话框(
上下文:上下文,
子:新建警报对话框(
标题:新文本(“你确定吗?”),
内容:新文本('未保存的数据将丢失'),
行动:[
新扁平按钮(
onPressed:()=>Navigator.of(context.pop)(true),
子项:新文本(“否”),
),
新扁平按钮(
onPressed:()=>Navigator.of(context.pop)(false),
子项:新文本(“是”),
),
],
),
);
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(widget.title),
),
floatingActionButton:新的floatingActionButton(
子项:新图标(Icons.edit),
按下:(){},
),
);
}
}

我发现解决方案是使用widget。以下是最终代码:

导入'dart:async';
进口“包装:颤振/材料.省道”;
类NewEntry扩展StatefulWidget{
NewEntry({Key-Key,this.title}):super(Key:Key);
最后的字符串标题;
@凌驾
State createState()=>new_NewEntryState();
}
类_NewEntryState扩展状态{
未来(willpop){
返回显示对话框(
上下文:上下文,
子:新建警报对话框(
标题:新文本(“你确定吗?”),
内容:新文本('未保存的数据将丢失'),
行动:[
新扁平按钮(
onPressed:()=>Navigator.of(context.pop)(false),
子项:新文本(“否”),
),
新扁平按钮(
onPressed:()=>Navigator.of(context.pop)(true),
子项:新文本(“是”),
),
],
),
)??假;
}
@凌驾
小部件构建(构建上下文){
返回新的Willposcope(
onWillPop:_onWillPop,
儿童:新脚手架(
appBar:新的appBar(
标题:新文本(widget.title),
),
floatingActionButton:新的floatingActionButton(
子项:新图标(Icons.edit),
按下:(){},
),
),
);
}
}

使用
后退按钮\u拦截器
软件包可以简化此过程,在更复杂的场景中尤其有用

用法示例:

@覆盖
void initState(){
super.initState();
BackButtonInterceptor.add(myInterceptor);
}
@凌驾
无效处置(){
BackButtonInterceptor.remove(myInterceptor);
super.dispose();
}
布尔myInterceptor(布尔stopDefaultButtonEvent){
打印(“后退按钮!”;//做一些事情。
返回true;
}

如果您正在使用GetX软件包,并且实现了
GetMaterialApp
方法来初始化应用程序,则不会调用
WidgetsBindingObserver
中的
didPopRoute
didPushRoute
方法。请改用
路由回调
,下面是一个示例,有关更多信息,请查看:


嗨,Mal,我试过上面的onWillPop方法,当我在willPop上运行release build时,它只起到调试apk的作用??false应删除我的应用程序使用Navigator.PushReplacementNamed在其流中移动。我学会了总是从OnWillPop()返回Future.value(false),以防止颤振弹出导航堆栈(不存在)。在我开始执行此策略之前,我的结果非常不稳定。
onWillPop:\u onWillPop
也可以是
onWillPop:()=>\u onWillPop()
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 FlatButton(
            onPressed: () => Navigator.of(context).pop(true),
            child: new Text('No'),
          ),
          new FlatButton(
            onPressed: () => Navigator.of(context).pop(false),
            child: new Text('Yes'),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.edit),
        onPressed: () {},
      ),
    );
  }
}
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: () {},
        ),
      ),
    );
  }
}
GetMaterialApp(
  routingCallback: (routing) {
    routing.isBack ? didPopRoute() : didPushRoute(routing.current);
  }
)