小编Ree*_*Mal的帖子

在Android上的Flutter应用程序中是否有任何方法拦截'Back'keydown?

我需要在用户通过按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)

android dart flutter

10
推荐指数
1
解决办法
5831
查看次数

标签 统计

android ×1

dart ×1

flutter ×1