mirror of
https://github.com/flutter/samples.git
synced 2025-11-09 14:28:51 +00:00
Rename Navigator 2.0 to Router (#712)
* Rename Navigator 2.0 to Router This aligns better with the documentation for this new API, and doesn't imply that apps need to be upgraded. * introduction -> navigator
This commit is contained in:
98
navigation_and_routing/lib/navigator/on_generate_route.dart
Normal file
98
navigation_and_routing/lib/navigator/on_generate_route.dart
Normal file
@@ -0,0 +1,98 @@
|
||||
// Copyright 2020, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
/// Shows how to handle arbitrary named routes using the `onGenerateRoute`
|
||||
/// callback defined in the `MaterialApp` constructor.
|
||||
library on_generate_route;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() {
|
||||
runApp(Nav2App());
|
||||
}
|
||||
|
||||
class Nav2App extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
onGenerateRoute: (settings) {
|
||||
// Handle '/'
|
||||
if (settings.name == '/') {
|
||||
return MaterialPageRoute(builder: (context) => HomeScreen());
|
||||
}
|
||||
|
||||
// Handle '/details/:id'
|
||||
var uri = Uri.parse(settings.name);
|
||||
if (uri.pathSegments.length == 2 &&
|
||||
uri.pathSegments.first == 'details') {
|
||||
var id = uri.pathSegments[1];
|
||||
return MaterialPageRoute(builder: (context) => DetailScreen(id: id));
|
||||
}
|
||||
|
||||
return MaterialPageRoute(builder: (context) => UnknownScreen());
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class HomeScreen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Center(
|
||||
child: TextButton(
|
||||
child: Text('View Details'),
|
||||
onPressed: () {
|
||||
Navigator.pushNamed(
|
||||
context,
|
||||
'/details/1',
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class DetailScreen extends StatelessWidget {
|
||||
final String id;
|
||||
|
||||
DetailScreen({
|
||||
this.id,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text('Viewing details for item $id'),
|
||||
TextButton(
|
||||
child: Text('Pop!'),
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class UnknownScreen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Center(
|
||||
child: Text('404!'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user