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(RouteInformation); 7 : 8 : /// A pre-made builder to be used for [locationBuilder]. 9 : /// 10 : /// Determines the appropriate [BeamLocation] from the list 11 : /// and populates it with configured state. 12 : class BeamerLocationBuilder { 13 2 : BeamerLocationBuilder({required this.beamLocations}); 14 : 15 : /// List of all [BeamLocation]s that this builder handles. 16 : final List<BeamLocation> beamLocations; 17 : 18 2 : BeamLocation call(RouteInformation routeInformation) { 19 2 : return Utils.chooseBeamLocation( 20 4 : Uri.parse(routeInformation.location ?? '/'), 21 2 : beamLocations, 22 4 : data: {'state': routeInformation.state}, 23 : ); 24 : } 25 : } 26 : 27 : /// A pre-made builder to be used for [locationBuilder]. 28 : /// 29 : /// Creates a single [BeamLocation]; [SimpleBeamLocation] 30 : /// and configures its [BeamLocation.buildPages] with appropriate [routes]. 31 : class SimpleLocationBuilder { 32 8 : SimpleLocationBuilder({required this.routes, this.builder}); 33 : 34 : /// List of all routes this builder handles. 35 : final Map<Pattern, dynamic Function(BuildContext, BeamState)> routes; 36 : 37 : /// Used as a [BeamLocation.builder]. 38 : Widget Function(BuildContext context, Widget navigator)? builder; 39 : 40 7 : BeamLocation call(RouteInformation routeInformation) { 41 : final matched = 42 21 : SimpleBeamLocation.chooseRoutes(routeInformation, routes.keys); 43 7 : if (matched.isNotEmpty) { 44 7 : return SimpleBeamLocation( 45 : routeInformation: routeInformation, 46 7 : routes: Map.fromEntries( 47 42 : routes.entries.where((e) => matched.containsKey(e.key))), 48 7 : navBuilder: builder, 49 : ); 50 : } else { 51 6 : return NotFound(path: routeInformation.location ?? '/'); 52 : } 53 : } 54 : }