Created
August 2, 2020 03:26
-
-
Save hachibeeDI/e722500f432fb79d6465e2e0b2f1af0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:meta/meta.dart'; | |
import 'package:flutter/material.dart'; | |
abstract class RouteSetting { | |
List<Route> toInstance(); | |
} | |
class Route<Arguments> { | |
final String name; | |
final bool initial; | |
final Widget Function(BuildContext context, Arguments args) builder; | |
const Route({ | |
@required this.name, | |
@required this.builder, | |
this.initial, | |
}); | |
} | |
class Router<T extends RouteSetting> { | |
final T setting; | |
final List<Route> routes; | |
Router(this.setting) : routes = setting.toInstance(); | |
Route get initialRoute => routes.firstWhere((r) => r.initial == true); | |
Map<String, WidgetBuilder> toMaterialRoutes() => Map<String, WidgetBuilder>.fromIterable( | |
routes, | |
key: (dynamic route) => route.name as String, | |
value: (dynamic route) => (BuildContext context) { | |
// TODO: Builder widget? | |
final dynamic arg = ModalRoute.of(context).settings.arguments; | |
return route.builder(context, arg) as Widget; | |
}, | |
); | |
/// Be aware!! You can pass a route which require argument without errors like below: | |
/// ``` | |
/// ↓ Runtime error! because knowledge id is required | |
/// soeasyRoutes.pushWithoutArgument(context, (r) => r.comments); | |
/// ``` | |
/// That's because [Route<void>] is able to override any kinda definition. Dartはクソ | |
Future<void> pushWithoutArgument<Ro extends Route<void>>( | |
BuildContext ctx, | |
Ro Function(T setting) fromSettingToName, | |
) { | |
final r = fromSettingToName(setting); | |
return Navigator.pushNamed(ctx, r.name); | |
} | |
Future<void> push<Arguments, Ro extends Route<Arguments>>( | |
BuildContext ctx, | |
Ro Function(T setting) fromSettingToName, { | |
@required Arguments arguments, | |
}) { | |
final r = fromSettingToName(setting); | |
return Navigator.pushNamed(ctx, r.name, arguments: arguments); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is there example for its usage?