Line data Source code
1 : import 'package:flutter/material.dart'; 2 : 3 : import 'package:beamer/beamer.dart'; 4 : import 'package:beamer/src/utils.dart'; 5 : 6 : /// A convenience typdef for [BeamerDelegate.locationBuilder]. 7 : typedef LocationBuilder = BeamLocation Function( 8 : RouteInformation, 9 : BeamParameters?, 10 : ); 11 : 12 : /// A pre-made builder to be used for [locationBuilder]. 13 : /// 14 : /// Determines the appropriate [BeamLocation] from the list 15 : /// and populates it with configured state. 16 : class BeamerLocationBuilder { 17 : /// Creates a [BeamerLocationBuilder] with specified property. 18 : /// 19 : /// [beamLocations] is a required list of [BeamLocation]s. 20 2 : BeamerLocationBuilder({required this.beamLocations}); 21 : 22 : /// List of all [BeamLocation]s that this builder handles. 23 : final List<BeamLocation> beamLocations; 24 : 25 : /// Makes this callable as [LocationBuilder]. 26 : /// 27 : /// Returns [Utils.chooseBeamLocation]. 28 2 : BeamLocation call( 29 : RouteInformation routeInformation, 30 : BeamParameters? beamParameters, 31 : ) { 32 2 : return Utils.chooseBeamLocation( 33 4 : Uri.parse(routeInformation.location ?? '/'), 34 2 : beamLocations, 35 2 : routeState: routeInformation.state, 36 : ); 37 : } 38 : } 39 : 40 : /// A pre-made builder to be used for [BeamerDelegate.locationBuilder]. 41 : /// 42 : /// Creates a single [BeamLocation]; [RoutesBeamLocation] 43 : /// and configures its [BeamLocation.buildPages] from specified [routes]. 44 : class RoutesLocationBuilder { 45 : /// Creates a [RoutesLocationBuilder] with specified properties. 46 : /// 47 : /// [routes] are required to build pages from. 48 7 : RoutesLocationBuilder({required this.routes, this.builder}); 49 : 50 : /// List of all routes this builder handles. 51 : final Map<Pattern, dynamic Function(BuildContext, BeamState, Object?)> routes; 52 : 53 : /// Used as a [BeamLocation.builder]. 54 : Widget Function(BuildContext context, Widget navigator)? builder; 55 : 56 : /// Makes this callable as [LocationBuilder]. 57 : /// 58 : /// Returns [RoutesBeamLocation] configured with chosen routes from [routes] or [NotFound]. 59 6 : BeamLocation call( 60 : RouteInformation routeInformation, 61 : BeamParameters? beamParameters, 62 : ) { 63 : final matched = 64 18 : RoutesBeamLocation.chooseRoutes(routeInformation, routes.keys); 65 6 : if (matched.isNotEmpty) { 66 6 : return RoutesBeamLocation( 67 : routeInformation: routeInformation, 68 6 : routes: routes, 69 6 : navBuilder: builder, 70 : ); 71 : } else { 72 6 : return NotFound(path: routeInformation.location ?? '/'); 73 : } 74 : } 75 : }