will_pop_scope.dart 2.69 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'framework.dart';
6
import 'navigator.dart';
7 8 9 10 11
import 'routes.dart';

/// Registers a callback to veto attempts by the user to dismiss the enclosing
/// [ModalRoute].
///
12
/// {@tool dartpad}
13 14 15 16
/// Whenever the back button is pressed, you will get a callback at [onWillPop],
/// which returns a [Future]. If the [Future] returns true, the screen is
/// popped.
///
17
/// ** See code in examples/api/lib/widgets/will_pop_scope/will_pop_scope.0.dart **
18 19
/// {@end-tool}
///
20 21
/// See also:
///
22
///  * [ModalRoute.addScopedWillPopCallback] and [ModalRoute.removeScopedWillPopCallback],
23
///    which this widget uses to register and unregister [onWillPop].
24 25 26
///  * [Form], which provides an `onWillPop` callback that enables the form
///    to veto a `pop` initiated by the app's back button.
///
27 28 29 30 31
class WillPopScope extends StatefulWidget {
  /// Creates a widget that registers a callback to veto attempts by the user to
  /// dismiss the enclosing [ModalRoute].
  ///
  /// The [child] argument must not be null.
32
  const WillPopScope({
33 34 35
    Key? key,
    required this.child,
    required this.onWillPop,
36 37
  }) : assert(child != null),
       super(key: key);
38 39

  /// The widget below this widget in the tree.
40
  ///
41
  /// {@macro flutter.widgets.ProxyWidget.child}
42 43 44 45 46 47
  final Widget child;

  /// Called to veto attempts by the user to dismiss the enclosing [ModalRoute].
  ///
  /// If the callback returns a Future that resolves to false, the enclosing
  /// route will not be popped.
48
  final WillPopCallback? onWillPop;
49 50

  @override
51
  State<WillPopScope> createState() => _WillPopScopeState();
52 53 54
}

class _WillPopScopeState extends State<WillPopScope> {
55
  ModalRoute<dynamic>? _route;
56 57

  @override
58 59
  void didChangeDependencies() {
    super.didChangeDependencies();
60
    if (widget.onWillPop != null)
61
      _route?.removeScopedWillPopCallback(widget.onWillPop!);
62
    _route = ModalRoute.of(context);
63
    if (widget.onWillPop != null)
64
      _route?.addScopedWillPopCallback(widget.onWillPop!);
65 66 67
  }

  @override
68
  void didUpdateWidget(WillPopScope oldWidget) {
69
    super.didUpdateWidget(oldWidget);
70 71
    if (widget.onWillPop != oldWidget.onWillPop && _route != null) {
      if (oldWidget.onWillPop != null)
72
        _route!.removeScopedWillPopCallback(oldWidget.onWillPop!);
73
      if (widget.onWillPop != null)
74
        _route!.addScopedWillPopCallback(widget.onWillPop!);
75 76 77 78 79
    }
  }

  @override
  void dispose() {
80
    if (widget.onWillPop != null)
81
      _route?.removeScopedWillPopCallback(widget.onWillPop!);
82 83 84 85
    super.dispose();
  }

  @override
86
  Widget build(BuildContext context) => widget.child;
87
}