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