Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Session/3 #16

Merged
merged 9 commits into from
Mar 31, 2023
Merged

Session/3 #16

merged 9 commits into from
Mar 31, 2023

Conversation

morikann
Copy link
Owner

@morikann morikann commented Mar 28, 2023

課題

close #4

対応箇所

  • StatefulWidget を継承した Widget で構築された新しい画面を追加する
    • Container を表示する StatefulWidget の LaunchView を作成
  • 新しい画面の背景色は Colors.green に設定する
    • Container の背景色を Colors.green に設定
  • アプリ起動時に新しい画面に遷移する
    • アプリのルート画面を LaunchView に設定
  • 新しい画面が表示されたら、0.5 秒後に前回まで作っていた画面に遷移する
    • Future.delayed() を用いて 0.5 秒後に WeatherView に遷移
  • 前回まで作っていた画面の Close ボタンをタップすると画面を閉じる

動作

expected actual
actual.mp4

@morikann morikann self-assigned this Mar 28, 2023
}

class _LaunchViewState extends State<LaunchView> {
void _toWeatherView() {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

再帰的に実装してCloseボタン押下後にWeatherViewへ遷移するようにしました。🙋‍♂️

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[感想]
ばっちり実装できています 🎉

import 'package:flutter_training/view/weather_view.dart';
import 'package:go_router/go_router.dart';

final router = GoRouter(
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

遷移先が増えていくにつれて見通しが悪くなるのでroutesの定義はroutesファイルに切り出しました。🙋‍♂️


class _LaunchViewState extends State<LaunchView> {
void _toWeatherView() {
Future.delayed(const Duration(milliseconds: 500), () {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Durationの引数seconds は int 型で「0.5」の指定ができなかったので、milliseconds で「500」を指定して 0.5 秒にしています。🙋‍♂️

@morikann morikann marked this pull request as ready for review March 28, 2023 06:12
@github-actions github-actions bot requested a review from blendthink March 28, 2023 06:13
@github-actions
Copy link

Ready for review 🚀

Copy link
Collaborator

@blendthink blendthink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@morikann
コメントしましたのでご確認お願いします!

@@ -12,6 +12,7 @@ dependencies:
flutter:
sdk: flutter
flutter_svg: ^2.0.4
go_router: ^6.5.0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[感想]
今流行りのパッケージを使用していてチャレンジングでいいですね〜✨

lib/routes.dart Outdated
builder: (context, state) => const LaunchView(),
routes: [
GoRoute(
path: 'weather_view',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nits]
このような path に指定している文字リテラルは定数に切り出して、使いまわせるようにするか、 go_router_builder を使って型安全に遷移できるようにするなどするとより安全に開発進められ良さそうです!

https://pub.dev/packages/go_router_builder

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、確かにそうですね!
ご指摘ありがとうございます!

現在 go_router_builder を使っているのですが、自動生成された push メソッドの返り値の型が Future<void> -> void に変換されており、画面遷移から戻ってきたら処理を実行するというのができなさそうなのですが、この解決策があればご教示いただきたいです🙇‍♂️

extension $WeatherRouteExtension on WeatherRoute {
  static WeatherRoute _fromState(GoRouterState state) => const WeatherRoute();

  String get location => GoRouteData.$location(
        '/weather_view',
      );

  void go(BuildContext context) => context.go(location);

  // Future<void> から void に変換されている
  void push(BuildContext context) => context.push(location);

  void pushReplacement(BuildContext context) =>
      context.pushReplacement(location);
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上記解決策がわからなかったため、path を定数に切り出して対応しました。🙋‍♂️
e39fee3

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

現在 go_router_builder を使っているのですが、自動生成された push メソッドの返り値の型が Future -> void に変換されており、画面遷移から戻ってきたら処理を実行するというのができなさそうなのですが、この解決策があればご教示いただきたいです🙇‍♂️

ああ、、これは go_router_builder まだ対応していないのですね、、
Flutterチーム管轄になってから、 go_router の開発の進め方が悪くなっちゃったんですよね。。

いったん、定数に切り出しだけでいいかなと思います!

}

class _LaunchViewState extends State<LaunchView> {
void _toWeatherView() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[感想]
ばっちり実装できています 🎉

void _toWeatherView() {
Future.delayed(const Duration(milliseconds: 500), () {
context.push('/weather_view').then((_) {
_toWeatherView();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[感想]
go_router だと、 iOS のスワイプで戻ってきた時に、ここの処理が実行されないようでした。
修正は不要ですが、実案件ではこの辺りも気をつけながら実装進めていただければと思います 💪

Simulator.Screen.Recording.-.iPhone.14.Pro.-.2023-03-29.at.00.59.23.mp4

push で戻り値が返ってくるようになったのがここ最近だったので、おそらくまだ対応されていない気がしています。。
↓対応されたプルリクエスト
flutter/packages#3368

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これは気づきませんでした...。ご教示いただきありがとうございます!
実案件では気をつけるようにします!!

class _LaunchViewState extends State<LaunchView> {
void _toWeatherView() {
Future.delayed(const Duration(milliseconds: 500), () {
context.push('/weather_view').then((_) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nits]
mounted のチェックをしておかないと、クラッシュする危険性があるため注意が必要です。

if (mounted) {
  context.push('/weather_view').then((_) {
    _toWeatherView();
  });
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DON'T use BuildContext across asynchronous gaps. という警告がなかったため気づきませんでした... 。
ご指摘ありがとうございます!
ついでに書き方も then から async/await を使った書き方に変更しました。🙋‍♂️

cdbcf9f

Base automatically changed from session/2 to main March 31, 2023 04:02
@morikann
Copy link
Owner Author

@blendthink
rebase & conflict 解消が完了しましたのでお手隙の際にご確認お願いいたします。🙇‍♂️

Copy link
Collaborator

@blendthink blendthink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@morikann
Copy link
Owner Author

レビューありがとうございます!
マージします。🙋‍♂️

@morikann morikann merged commit 7c89db0 into main Mar 31, 2023
@morikann morikann deleted the session/3 branch March 31, 2023 06:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Session3
2 participants