Skip to content

Instantly share code, notes, and snippets.

@mono0926
Last active June 26, 2023 12:39
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mono0926/404aed55890ef8628f9d6288bd31fff8 to your computer and use it in GitHub Desktop.
Save mono0926/404aed55890ef8628f9d6288bd31fff8 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() {
final navigatorKey = GlobalKey<NavigatorState>();
runApp(
MyApp(
navigatorKey: navigatorKey,
),
);
}
class MyApp extends StatelessWidget {
final GlobalKey<NavigatorState> navigatorKey;
const MyApp({
Key key,
@required this.navigatorKey,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.navigate_next),
onPressed: () {
// ここだと `Navigator.of(context)` 使えるので意味が無いが
// 実際にはnavigatorKeyを取り回すことでどこでも呼べる
navigatorKey.currentState.pushNamed("foo");
},
),
),
navigatorKey: navigatorKey,
onGenerateRoute: _handleRoutes,
);
}
Route _handleRoutes(RouteSettings settings) {
final name = settings.name;
return MaterialPageRoute(
builder: (context) => Scaffold(
appBar: AppBar(
title: Text(name),
),
),
);
}
}
@mono0926
Copy link
Author

1 2
simulator screen shot - iphone xs - 2018-12-18 at 09 28 19 simulator screen shot - iphone xs - 2018-12-18 at 09 28 21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment