LCOV - code coverage report
Current view: top level - src - beamer_back_button_dispatcher.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 21 22 95.5 %
Date: 2021-12-03 10:03:44 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:flutter/material.dart';
       2             : 
       3             : import 'beamer_delegate.dart';
       4             : 
       5             : /// Overrides default back button behavior in [RootBackButtonDispatcher]
       6             : /// to do custom [onBack] or [BeamerDelegate.beamBack].
       7             : class BeamerBackButtonDispatcher extends RootBackButtonDispatcher {
       8             :   /// Creates a [BeamerBackButtonDispatcher] with specified properties.
       9             :   ///
      10             :   /// [delegate] is required as this needs to communicated with [BeamerDelegate].
      11           1 :   BeamerBackButtonDispatcher({
      12             :     required this.delegate,
      13             :     this.onBack,
      14             :     this.alwaysBeamBack = false,
      15             :     this.fallbackToBeamBack = true,
      16             :   });
      17             : 
      18             :   /// A [BeamerDelegate] that belongs to the same [Router]/[Beamer] as this.
      19             :   final BeamerDelegate delegate;
      20             : 
      21             :   /// A custom closure that has precedence over other behaviors.
      22             :   ///
      23             :   /// Return `true` if back action can be handled and `false` otherwise.
      24             :   final Future<bool> Function(BeamerDelegate delegate)? onBack;
      25             : 
      26             :   /// Whether to always do [BeamerDelegate.beamBack] when Android back button
      27             :   /// is pressed, i.e. always go to previous route in navigation history
      28             :   /// instead of trying to pop first.
      29             :   final bool alwaysBeamBack;
      30             : 
      31             :   /// Whether to try to use `beamBack()` when pop cannot be done.
      32             :   final bool fallbackToBeamBack;
      33             : 
      34             :   @override
      35           1 :   Future<bool> invokeCallback(Future<bool> defaultValue) async {
      36           1 :     if (onBack != null) {
      37           3 :       return (await onBack!(delegate));
      38             :     }
      39             : 
      40           1 :     if (alwaysBeamBack) {
      41           2 :       return delegate.beamBack();
      42             :     }
      43             : 
      44           2 :     bool didPopRoute = await super.invokeCallback(defaultValue);
      45             :     if (didPopRoute) {
      46             :       return didPopRoute;
      47             :     }
      48             : 
      49           1 :     if (fallbackToBeamBack) {
      50           2 :       return delegate.beamBack();
      51             :     } else {
      52             :       return false;
      53             :     }
      54             :   }
      55             : }
      56             : 
      57             : /// Overrides default back button behavior in [ChildBackButtonDispatcher]
      58             : /// to do custom [onBack] or [BeamerDelegate.beamBack].
      59             : class BeamerChildBackButtonDispatcher extends ChildBackButtonDispatcher {
      60             :   /// Creates a [BeamerChildBackButtonDispatcher] with specified properties.
      61             :   ///
      62             :   /// [parent] is required as this needs to communicate with its parent [BeamerBackButtonDispatcher].
      63             :   /// [delegate] is required as this needs to communicated with [BeamerDelegate].
      64           1 :   BeamerChildBackButtonDispatcher({
      65             :     required BeamerBackButtonDispatcher parent,
      66             :     required this.delegate,
      67             :     this.onBack,
      68           1 :   })  : alwaysBeamBack = parent.alwaysBeamBack,
      69           1 :         fallbackToBeamBack = parent.fallbackToBeamBack,
      70           1 :         super(parent);
      71             : 
      72             :   /// A [BeamerDelegate] that belongs to the same [Router]/[Beamer] as this.
      73             :   final BeamerDelegate delegate;
      74             : 
      75             :   /// A custom closure that has precedence over other behaviors.
      76             :   ///
      77             :   /// Return `true` if back action can be handled and `false` otherwise.
      78             :   final Future<bool> Function(BeamerDelegate delegate)? onBack;
      79             : 
      80             :   /// Whether to always do [BeamerDelegate.beamBack] when Android back button
      81             :   /// is pressed, i.e. always go to previous route in navigation history
      82             :   /// instead of trying to pop first.
      83             :   final bool alwaysBeamBack;
      84             : 
      85             :   /// Whether to try to use `beamBack()` when pop cannot be done.
      86             :   final bool fallbackToBeamBack;
      87             : 
      88             :   @override
      89           1 :   Future<bool> invokeCallback(Future<bool> defaultValue) async {
      90           2 :     if (!delegate.active) {
      91             :       return false;
      92             :     }
      93             : 
      94           1 :     if (onBack != null) {
      95           3 :       return (await onBack!(delegate));
      96             :     }
      97             : 
      98           1 :     if (alwaysBeamBack) {
      99           0 :       return delegate.beamBack();
     100             :     }
     101             : 
     102           2 :     bool didPopRoute = await super.invokeCallback(defaultValue);
     103             :     if (didPopRoute) {
     104             :       return didPopRoute;
     105             :     }
     106             : 
     107           1 :     if (fallbackToBeamBack) {
     108           2 :       return delegate.beamBack();
     109             :     } else {
     110             :       return false;
     111             :     }
     112             :   }
     113             : }

Generated by: LCOV version 1.14