android - 有没有办法在 Android 上的 Flutter 应用程序中拦截 'Back' keydown?

标签 android dart flutter

我需要在用户通过按 Android 设备上的 Back 按钮离开当前路线之前显示一个警告对话框。我试图通过实现 WidgetsBindingObserver 来拦截后退按钮的行为处于小部件状态。有一个封闭的issue在 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 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: () {},
      ),
    );
  }
}

最佳答案

我发现解决方案是使用 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: () {},
        ),
      ),
    );
  }
}

关于android - 有没有办法在 Android 上的 Flutter 应用程序中拦截 'Back' keydown?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44635450/

相关文章:

flutter - 未定义命名参数 'options'

dart - flutter 实现粘性标题和对齐项目效果

flutter - 未解析的引用 : firebasemessaging

android - 如何在 CachedNetworkImage flutter 中清除缓存

java - Android 中事件监听器的超时检测

dart - 很难用 eclipse 插件建立一个 dart 项目

flutter - 有什么方法可以在flf中的zefyrEditor中使用验证属性和onChanged属性?

android - public open fun navigateUp() 的争论太多

java - 驱动程序无法使用安全套接字层 (SSL) 加密与 SQL Server 建立安全连接

android - 不允许从此设备付款 - Android Paypal