diff --git a/simple_route_parameters_implementation/lib/routes/one_route.dart b/simple_route_parameters_implementation/lib/routes/one_route.dart index bd9f864..09a42e4 100644 --- a/simple_route_parameters_implementation/lib/routes/one_route.dart +++ b/simple_route_parameters_implementation/lib/routes/one_route.dart @@ -3,25 +3,50 @@ import 'package:flutter/material.dart'; import 'package:simple_route_parameters_implementation/screens/one_screen.dart'; class OneRequestArgs { - String name; + OneRequestArgs({this.name}); + final String name; + + static OneRequestArgs fromJson(Map json) { + return OneRequestArgs( + name: json['name'], + ); + } + + @override + String toString() { + return 'OneRequestArgs(name: $name)'; + } } -// @NuRouteParser() -class OneRoute extends NuRoute { +// Arguments of this Route specifies the following: +// 1. be installed on any NuRouter +// 2. to receive arguments that can be parsed into a OneRequestArgs class +// 3. when popped will return a String +class OneRoute extends NuRoute { @override String get path => 'one'; @override ScreenType get screenType => materialScreenType; + /// Flutter does not have support for reflection, we need to explicitly provide a function to parse + /// the [NuRouteSettings.rawParameters] into the specified class that will be made available in [NuRouteSettings.args] + @override + ParamsParser get paramsParser => OneRequestArgs.fromJson; + @override Widget build(BuildContext context, NuRouteSettings settings) { - // print(settings); + /// Map with the raw arguments passed to the route + print(settings.rawParameters); + + /// OneRequestArgs instance with the parsed arguments + print(settings.arguments); return OneScreen( params: settings.args?.name, onScreenTwoClick: () => nuvigator.open('two'), onScreenThreeClick: () => nuvigator.open('three'), - onClose: () => nuvigator.pop(), + // The same work for nested flows when calling `closeFlow` to return a flow result + onClose: () => nuvigator.pop('closed'), ); } -} \ No newline at end of file +} diff --git a/simple_route_parameters_implementation/lib/screens/home_screen.dart b/simple_route_parameters_implementation/lib/screens/home_screen.dart index 1834084..4b4cde3 100644 --- a/simple_route_parameters_implementation/lib/screens/home_screen.dart +++ b/simple_route_parameters_implementation/lib/screens/home_screen.dart @@ -24,16 +24,38 @@ class HomeScreen extends StatelessWidget { Text( 'What\'s your name?', ), - SizedBox(height: 20,), + SizedBox( + height: 20, + ), TextField( controller: _nameController, ), - SizedBox(height: 20,), + SizedBox( + height: 20, + ), ElevatedButton( child: Text('Next'), - - // Descobrir o motivo de a rota não receber esse parâmetro passado - onPressed: () => nuvigator.open('exapp://one?name=${_nameController.text}'), + onPressed: () async { + final result = await nuvigator + .open('myapp://one?name=${_nameController.text}'); + showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: const Text('Route Closed'), + content: Text('Value Returned: $result'), + actions: [ + TextButton( + child: const Text('Close'), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + ], + ); + }, + ); + }, ), ], ), diff --git a/simple_route_parameters_implementation/lib/screens/one_screen.dart b/simple_route_parameters_implementation/lib/screens/one_screen.dart index c4aab69..bc2b7ff 100644 --- a/simple_route_parameters_implementation/lib/screens/one_screen.dart +++ b/simple_route_parameters_implementation/lib/screens/one_screen.dart @@ -15,7 +15,6 @@ class OneScreen extends StatelessWidget { @override Widget build(BuildContext context) { - print(params); return Scaffold( appBar: AppBar( title: Text("Screen one"), @@ -47,4 +46,4 @@ class OneScreen extends StatelessWidget { ), // This trailing comma makes auto-formatting nicer for build methods. ); } -} \ No newline at end of file +}