Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some adjustments to argument passing logic #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions simple_route_parameters_implementation/lib/routes/one_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> json) {
return OneRequestArgs(
name: json['name'],
);
}

@override
String toString() {
return 'OneRequestArgs(name: $name)';
}
}

// @NuRouteParser()
class OneRoute extends NuRoute<NuRouter, OneRequestArgs, void> {
// 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<NuRouter, OneRequestArgs, String> {
@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<OneRequestArgs> get paramsParser => OneRequestArgs.fromJson;

@override
Widget build(BuildContext context, NuRouteSettings<OneRequestArgs> settings) {
// print(settings);
/// Map<String, dynamic> 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'),
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: <Widget>[
TextButton(
child: const Text('Close'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
},
),
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class OneScreen extends StatelessWidget {

@override
Widget build(BuildContext context) {
print(params);
return Scaffold(
appBar: AppBar(
title: Text("Screen one"),
Expand Down Expand Up @@ -47,4 +46,4 @@ class OneScreen extends StatelessWidget {
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
}