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