Метод setNewRoutePath
Метод setNewRoutePath класса RouterDelegate его вызывает Router после parseRouteInformation. После обновления delegate перестраивает Navigator через build.
Синтаксис
@override
Future<void> setNewRoutePath(AppConfig configuration) async {
_config = configuration;
notifyListeners();
}
Параметры
| Параметр | Описание |
|---|---|
configuration |
Новая конфигурация маршрута от парсера. |
Пример
Давайте сохраним path в setNewRoutePath:
import 'package:flutter/material.dart';
class PathConfig
{
PathConfig(this.path);
final String path;
}
class PathDelegate extends RouterDelegate<PathConfig>
with ChangeNotifier
{
PathConfig _config = PathConfig('/');
@override
Widget build(BuildContext context) {
return Navigator(
pages: [
MaterialPage<void>(
child: Scaffold(
body: Center(
child: Text(_config.path),
),
),
),
],
onPopPage: (route, result) => route.didPop(result),
);
}
@override
Future<void> setNewRoutePath(PathConfig configuration) async {
_config = configuration;
notifyListeners();
}
@override
PathConfig get currentConfiguration => _config;
}
class PathParser extends RouteInformationParser<PathConfig>
{
@override
Future<PathConfig> parseRouteInformation(
RouteInformation routeInformation,
) async {
return PathConfig(routeInformation.uri.path);
}
@override
RouteInformation restoreRouteInformation(PathConfig configuration) {
return RouteInformation(uri: Uri.parse(configuration.path));
}
}
void main()
{
runApp(
MaterialApp.router(
routerDelegate: PathDelegate(),
routeInformationParser: PathParser(),
),
);
}
На экране текст покажет path из setNewRoutePath, например '/' при старте.
Пример
Давайте вызовем setNewRoutePath вручную и обновим экран:
import 'package:flutter/material.dart';
class PathConfig
{
PathConfig(this.path);
final String path;
}
class PathDelegate extends RouterDelegate<PathConfig>
with ChangeNotifier
{
PathConfig _config = PathConfig('/');
@override
Widget build(BuildContext context) {
return Navigator(
pages: [
MaterialPage<void>(
child: Scaffold(
body: Center(
child: Text(_config.path),
),
),
),
],
onPopPage: (route, result) => route.didPop(result),
);
}
@override
Future<void> setNewRoutePath(PathConfig configuration) async {
_config = configuration;
notifyListeners();
}
@override
PathConfig get currentConfiguration => _config;
}
class PathParser extends RouteInformationParser<PathConfig>
{
@override
Future<PathConfig> parseRouteInformation(
RouteInformation routeInformation,
) async {
return PathConfig(routeInformation.uri.path);
}
@override
RouteInformation restoreRouteInformation(PathConfig configuration) {
return RouteInformation(uri: Uri.parse(configuration.path));
}
}
void main()
{
var delegate = PathDelegate();
delegate.setNewRoutePath(PathConfig('/detail'));
runApp(
MaterialApp.router(
routerDelegate: delegate,
routeInformationParser: PathParser(),
),
);
}
На экране по центру появится '/detail' после setNewRoutePath до runApp.
Смотрите также
-
метод
build,
перестраивает UI послеsetNewRoutePath -
метод
parseRouteInformation,
даёт configuration -
класс
RouteInformation,
источник URI для парсера -
виджет
Router,
оркестрируетsetNewRoutePath