From 4caa35425bc1f3954ea40a73aa24b7c4ab077b59 Mon Sep 17 00:00:00 2001 From: morikann Date: Tue, 28 Mar 2023 12:00:02 +0900 Subject: [PATCH 1/9] Add LaunchView with Green Background Color --- lib/view/launch_view.dart | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 lib/view/launch_view.dart diff --git a/lib/view/launch_view.dart b/lib/view/launch_view.dart new file mode 100644 index 0000000..837a77c --- /dev/null +++ b/lib/view/launch_view.dart @@ -0,0 +1,15 @@ +import 'package:flutter/material.dart'; + +class LaunchView extends StatefulWidget { + const LaunchView({super.key}); + + @override + State createState() => _LaunchViewState(); +} + +class _LaunchViewState extends State { + @override + Widget build(BuildContext context) { + return Container(color: Colors.green); + } +} From 3428a6420e0eb36ffaf27dc4c8769397d63d74b9 Mon Sep 17 00:00:00 2001 From: morikann Date: Tue, 28 Mar 2023 12:01:20 +0900 Subject: [PATCH 2/9] Fix LaunchScreen to LaunchView --- lib/app.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/app.dart b/lib/app.dart index b2098cc..22d4489 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -1,5 +1,5 @@ import 'package:flutter/material.dart'; -import 'package:flutter_training/view/weather_view/weather_page.dart'; +import 'package:flutter_training/view/launch_view.dart'; class App extends StatelessWidget { const App({super.key}); @@ -12,7 +12,7 @@ class App extends StatelessWidget { theme: ThemeData( primarySwatch: Colors.blue, ), - home: const WeatherPage(), + home: const LaunchView(), ); } } From bf0c7f96ce93a3cb778c39059e5b0f51b8f6db45 Mon Sep 17 00:00:00 2001 From: morikann Date: Tue, 28 Mar 2023 12:33:41 +0900 Subject: [PATCH 3/9] Implement Screen Transition from LaunchView to WeatherView --- lib/view/launch_view.dart | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/view/launch_view.dart b/lib/view/launch_view.dart index 837a77c..5dd162d 100644 --- a/lib/view/launch_view.dart +++ b/lib/view/launch_view.dart @@ -1,4 +1,6 @@ +import 'dart:async'; import 'package:flutter/material.dart'; +import 'package:flutter_training/view/weather_view.dart'; class LaunchView extends StatefulWidget { const LaunchView({super.key}); @@ -8,6 +10,21 @@ class LaunchView extends StatefulWidget { } class _LaunchViewState extends State { + @override + void initState() { + super.initState(); + + WidgetsBinding.instance.endOfFrame.then((_) { + Future.delayed(const Duration(milliseconds: 500), () { + Navigator.of(context).push( + MaterialPageRoute( + builder: (context) => const WeatherView(), + ), + ); + }); + }); + } + @override Widget build(BuildContext context) { return Container(color: Colors.green); From 1d5ff40466d4ba02dd270c25bddc914da177ff11 Mon Sep 17 00:00:00 2001 From: morikann Date: Tue, 28 Mar 2023 12:58:59 +0900 Subject: [PATCH 4/9] Add go_router package --- pubspec.lock | 21 +++++++++++++++++++++ pubspec.yaml | 1 + 2 files changed, 22 insertions(+) diff --git a/pubspec.lock b/pubspec.lock index f92a237..d86c94c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -99,6 +99,19 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" + go_router: + dependency: "direct main" + description: + name: go_router + sha256: "432409518740645ce7f28802171b78783197d01149fad44f9b8ae55f40277139" + url: "https://pub.dev" + source: hosted + version: "6.5.0" js: dependency: transitive description: @@ -115,6 +128,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.1" + logging: + dependency: transitive + description: + name: logging + sha256: "04094f2eb032cbb06c6f6e8d3607edcfcb0455e2bb6cbc010cb01171dcb64e6d" + url: "https://pub.dev" + source: hosted + version: "1.1.1" matcher: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index e7245e6..9b10566 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,6 +12,7 @@ dependencies: flutter: sdk: flutter flutter_svg: ^2.0.4 + go_router: ^6.5.0 yumemi_weather: git: url: https://github.com/yumemi-inc/flutter-training-template.git From 5145d2aa201d7d18b5fd6e0d2c41229ae6745bb1 Mon Sep 17 00:00:00 2001 From: morikann Date: Tue, 28 Mar 2023 13:55:47 +0900 Subject: [PATCH 5/9] Modified to Navigation with go_router --- lib/app.dart | 6 +++--- lib/routes.dart | 18 ++++++++++++++++++ lib/view/launch_view.dart | 8 ++------ 3 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 lib/routes.dart diff --git a/lib/app.dart b/lib/app.dart index 22d4489..65a1383 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -1,18 +1,18 @@ import 'package:flutter/material.dart'; -import 'package:flutter_training/view/launch_view.dart'; +import 'package:flutter_training/routes.dart'; class App extends StatelessWidget { const App({super.key}); @override Widget build(BuildContext context) { - return MaterialApp( + return MaterialApp.router( + routerConfig: router, debugShowCheckedModeBanner: false, title: 'Weather App', theme: ThemeData( primarySwatch: Colors.blue, ), - home: const LaunchView(), ); } } diff --git a/lib/routes.dart b/lib/routes.dart new file mode 100644 index 0000000..d23b624 --- /dev/null +++ b/lib/routes.dart @@ -0,0 +1,18 @@ +import 'package:flutter_training/view/launch_view.dart'; +import 'package:flutter_training/view/weather_view/weather_page.dart'; +import 'package:go_router/go_router.dart'; + +final router = GoRouter( + routes: [ + GoRoute( + path: '/', + builder: (context, state) => const LaunchView(), + routes: [ + GoRoute( + path: 'weather_page', + builder: (context, state) => const WeatherPage(), + ) + ], + ), + ], +); diff --git a/lib/view/launch_view.dart b/lib/view/launch_view.dart index 5dd162d..0cf34d1 100644 --- a/lib/view/launch_view.dart +++ b/lib/view/launch_view.dart @@ -1,6 +1,6 @@ import 'dart:async'; import 'package:flutter/material.dart'; -import 'package:flutter_training/view/weather_view.dart'; +import 'package:go_router/go_router.dart'; class LaunchView extends StatefulWidget { const LaunchView({super.key}); @@ -16,11 +16,7 @@ class _LaunchViewState extends State { WidgetsBinding.instance.endOfFrame.then((_) { Future.delayed(const Duration(milliseconds: 500), () { - Navigator.of(context).push( - MaterialPageRoute( - builder: (context) => const WeatherView(), - ), - ); + context.go('/weather_page'); }); }); } From d8f8bc3cad1878fca4923f84e2d952eb66195394 Mon Sep 17 00:00:00 2001 From: morikann Date: Tue, 28 Mar 2023 14:53:39 +0900 Subject: [PATCH 6/9] Fix Transition Using Recursion --- lib/view/launch_view.dart | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/view/launch_view.dart b/lib/view/launch_view.dart index 0cf34d1..643f5f2 100644 --- a/lib/view/launch_view.dart +++ b/lib/view/launch_view.dart @@ -10,14 +10,20 @@ class LaunchView extends StatefulWidget { } class _LaunchViewState extends State { + void _toWeatherView() { + Future.delayed(const Duration(milliseconds: 500), () { + context.push('/weather_page').then((_) { + _toWeatherView(); + }); + }); + } + @override void initState() { super.initState(); WidgetsBinding.instance.endOfFrame.then((_) { - Future.delayed(const Duration(milliseconds: 500), () { - context.go('/weather_page'); - }); + _toWeatherView(); }); } From 2c9ea643eced386961b5b948404fe4796478c383 Mon Sep 17 00:00:00 2001 From: morikann Date: Fri, 31 Mar 2023 13:59:14 +0900 Subject: [PATCH 7/9] Define Path as Constant --- lib/routes.dart | 12 +++++------- lib/view/launch_view.dart | 5 ++++- lib/view/weather_view/weather_page.dart | 3 +++ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/routes.dart b/lib/routes.dart index d23b624..aa61975 100644 --- a/lib/routes.dart +++ b/lib/routes.dart @@ -5,14 +5,12 @@ import 'package:go_router/go_router.dart'; final router = GoRouter( routes: [ GoRoute( - path: '/', + path: LaunchView.path, builder: (context, state) => const LaunchView(), - routes: [ - GoRoute( - path: 'weather_page', - builder: (context, state) => const WeatherPage(), - ) - ], ), + GoRoute( + path: WeatherPage.path, + builder: (context, state) => const WeatherPage(), + ) ], ); diff --git a/lib/view/launch_view.dart b/lib/view/launch_view.dart index 643f5f2..e5509de 100644 --- a/lib/view/launch_view.dart +++ b/lib/view/launch_view.dart @@ -1,10 +1,13 @@ import 'dart:async'; import 'package:flutter/material.dart'; +import 'package:flutter_training/view/weather_view/weather_page.dart'; import 'package:go_router/go_router.dart'; class LaunchView extends StatefulWidget { const LaunchView({super.key}); + static const path = '/'; + @override State createState() => _LaunchViewState(); } @@ -12,7 +15,7 @@ class LaunchView extends StatefulWidget { class _LaunchViewState extends State { void _toWeatherView() { Future.delayed(const Duration(milliseconds: 500), () { - context.push('/weather_page').then((_) { + context.push(WeatherPage.path).then((_) { _toWeatherView(); }); }); diff --git a/lib/view/weather_view/weather_page.dart b/lib/view/weather_view/weather_page.dart index a430cdc..1cbd221 100644 --- a/lib/view/weather_view/weather_page.dart +++ b/lib/view/weather_view/weather_page.dart @@ -4,9 +4,12 @@ import 'package:flutter_training/view/weather_view/component/weather_forecast.da import 'package:yumemi_weather/yumemi_weather.dart'; final _weather = Weather(YumemiWeather()); + class WeatherPage extends StatefulWidget { const WeatherPage({super.key}); + static const path = '/weather_page'; + @override State createState() => _WeatherPageState(); } From d60559fef0e0d2aedd3448d54f835db7efb8d3e1 Mon Sep 17 00:00:00 2001 From: morikann Date: Fri, 31 Mar 2023 14:46:30 +0900 Subject: [PATCH 8/9] Add Mounted Check in _toWeatherView --- lib/view/launch_view.dart | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/view/launch_view.dart b/lib/view/launch_view.dart index e5509de..821e306 100644 --- a/lib/view/launch_view.dart +++ b/lib/view/launch_view.dart @@ -13,12 +13,13 @@ class LaunchView extends StatefulWidget { } class _LaunchViewState extends State { - void _toWeatherView() { - Future.delayed(const Duration(milliseconds: 500), () { - context.push(WeatherPage.path).then((_) { - _toWeatherView(); - }); - }); + Future _toWeatherView() async { + await Future.delayed(const Duration(milliseconds: 500)); + if (!mounted) { + return; + } + await context.push(WeatherPage.path); + await _toWeatherView(); } @override From 52a28b0db8f7db2ee221872a3b2cb0d139ef7412 Mon Sep 17 00:00:00 2001 From: morikann Date: Fri, 31 Mar 2023 15:16:53 +0900 Subject: [PATCH 9/9] Pop WeatherPage when Close Button Tapped --- lib/view/weather_view/weather_page.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/view/weather_view/weather_page.dart b/lib/view/weather_view/weather_page.dart index 1cbd221..1590e15 100644 --- a/lib/view/weather_view/weather_page.dart +++ b/lib/view/weather_view/weather_page.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_training/model/weather.dart'; import 'package:flutter_training/view/weather_view/component/weather_forecast.dart'; +import 'package:go_router/go_router.dart'; import 'package:yumemi_weather/yumemi_weather.dart'; final _weather = Weather(YumemiWeather()); @@ -37,8 +38,8 @@ class _WeatherPageState extends State { children: [ Expanded( child: TextButton( + onPressed: context.pop, child: const Text('Close'), - onPressed: () {}, ), ), Expanded(