Line data Source code
1 : import 'package:flutter/material.dart'; 2 : import '../../routemaster.dart'; 3 : 4 : typedef ValidateCallback = bool Function( 5 : RouteInfo info, 6 : BuildContext context, 7 : ); 8 : 9 : typedef ValidationFailedCallback = Page Function( 10 : RouteInfo info, 11 : BuildContext context, 12 : ); 13 : 14 : mixin GuardedPage<T> on ProxyPage<T> { 15 : /// Callback to check if the route is valid. If this returns false, 16 : /// [onValidationFailed] is called. 17 : /// 18 : /// By default this redirects to the default path. 19 : ValidateCallback get validate; 20 : 21 : /// Callback, called when the [validate] returns false. 22 : /// 23 : /// By default this redirects to the default path. 24 : ValidationFailedCallback? get onValidationFailed; 25 : } 26 : 27 : /// A page results which tells the router to redirect to another page. 28 : class Redirect extends Page<void> { 29 : final String path; 30 : final Map<String, String>? queryParameters; 31 4 : String get absolutePath => Uri( 32 2 : path: path, 33 2 : queryParameters: queryParameters, 34 2 : ).toString(); 35 : 36 4 : Redirect(this.path, {this.queryParameters}); 37 : 38 1 : @override 39 : Route createRoute(BuildContext context) { 40 1 : throw UnimplementedError('Redirect does not support building a route'); 41 : } 42 : } 43 : 44 : class Guard extends ProxyPage<void> with GuardedPage<void> { 45 : @override 46 : final ValidateCallback validate; 47 : 48 : @override 49 : final ValidationFailedCallback? onValidationFailed; 50 : 51 2 : Guard({ 52 : required Page child, 53 : required this.validate, 54 : this.onValidationFailed, 55 2 : }) : super(child: child); 56 : }