{"id":44,"date":"2026-04-19T09:46:57","date_gmt":"2026-04-19T00:46:57","guid":{"rendered":"https:\/\/tate.stars.ne.jp\/wpb\/?p=44"},"modified":"2026-04-25T08:30:39","modified_gmt":"2026-04-24T23:30:39","slug":"go_router","status":"publish","type":"post","link":"https:\/\/tate.stars.ne.jp\/wpb\/go_router.html","title":{"rendered":"Go_router"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\nimport 'package:flutter\/material.dart';\nimport 'package:go_router\/go_router.dart';\nimport 'package:go_transitions\/go_transitions.dart';\n\nvoid main() {\n  runApp(const MyApp());\n}\n\n\/\/\/ 1. GoRouter\u306e\u5b9a\u7fa9\n\/\/\/ \u30d1\u30b9\u3068\u753b\u9762\uff08Widget\uff09\u306e\u7d44\u307f\u5408\u308f\u305b\u3092\u767b\u9332\u3059\u308b\nfinal GoRouter router = GoRouter(\n  initialLocation: '\/',\n  routes: &lt;RouteBase>&#91;\n  GoRoute(\n    name: 'home',\n    path: '\/',\n    builder: (context, state) => const HomeScreen(),\n  pageBuilder: GoTransitions.slide.toLeft.withFade,\n\n  routes: &lt;RouteBase>&#91;\n   \/\/ \u8a73\u7d30\u753b\u9762\u3078\u306e\u30cd\u30b9\u30c8\u3055\u308c\u305f\u30eb\u30fc\u30c8\n   GoRoute(\n    name: 'details', \/\/ \u540d\u524d\u3092\u5b9a\u7fa9\n    path: '\/details',\n    builder: (context, state) => DetailScreen(),\n    pageBuilder: GoTransitions.slide.toLeft.withFade,\n ),\n ],\n ),\n ],\n);\n\n\/\/\/ 2. MaterialApp.router \u306e\u8a2d\u5b9a\nclass MyApp extends StatelessWidget {\n  const MyApp({super.key});\n @override\n Widget build(BuildContext context) {\n   GoTransition.defaultCurve = Curves.easeInOut;\n   GoTransition.defaultDuration = const Duration(milliseconds: 600);\n return MaterialApp.router(\n   debugShowCheckedModeBanner: false, \n   routerConfig: router, title:'GoRouter Test',);\n }\n}\n\/\/\/ \u30db\u30fc\u30e0\u753b\u9762\nclass HomeScreen extends StatelessWidget {\n  const HomeScreen({super.key});\n\n@override\n Widget build(BuildContext context) {\n return Scaffold(\n backgroundColor: Colors.limeAccent, \n  appBar: AppBar(\n  title: const Text('Home Screen'),\n  centerTitle: true,\n  backgroundColor: Colors.green,\/\/\u80cc\u666f\u8272\u3092\u6307\u5b9a\n      ),\nbody: Center(\n  child: ElevatedButton(\n  \/\/ onPressed: () => context.go('\/details'),\n   onPressed: () =>context.goNamed('details'),\n  \/\/ onPressed: () {\n  \/\/ 3. \u753b\u9762\u9077\u79fb\uff08context.go\uff09\n  \/\/    context.go('\/details');\n  \/\/   },\n child: const Text('Go to Details'),\n ),\n ),\n);\n}\n}\n\n\/\/ \u8a73\u7d30\u753b\u9762\nclass DetailScreen extends StatelessWidget {\n const DetailScreen({super.key});\n\n@override\n Widget build(BuildContext context) {\n return Scaffold(\n appBar: AppBar(title: const Text('Detail Screen'),\n  centerTitle: true,\n  backgroundColor: Colors.orange,\/\/\u80cc\u666f\u8272\u3092\u6307\u5b9a\n ),\n body: Center(\n child: ElevatedButton(\n  \/\/  onPressed: () {context.pop(); },\n  \/\/  onPressed: () => context.go('\/'),\n  onPressed: () =>context.goNamed('home'),\n  child: const Text('Back'),\n   ),\n   ),\n );\n }\n}\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":10,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"saved_in_kubio":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-44","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dart"],"_links":{"self":[{"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/posts\/44","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":4,"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":59,"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/posts\/44\/revisions\/59"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/media\/10"}],"wp:attachment":[{"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpb\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}