-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ChangeNotifierの実装(メインページ) #16
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一気にController作られたんですね:eyes:
こんな感じで、Controllerの作り方自体に修正があった際に少し修正範囲が大きくなってしまうので、ミニマムでPRを出してもらえると楽かもしれません!
lib/ui/pages/main/home/home.dart
Outdated
|
||
class HomePage extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
final _homeController = Provider.of<HomeController>(context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
build配下に listen: true
でProvideすると、HomeControllerに変更があるたびに、このcontext(HomePage)が丸ごと再描画されてしまうので、あんまり良くないかもです!
ここでは
listen: false
で定義しておいて、単発呼び出しではこれを使ってあげる- 変更を監視したいプロパティの場合はちょっとめんどうくさいけど、
Provider.of<HomeController>(context, listen: true).hoge
みたいに逐一取得してあげる感じですかね
前に記事書いてみたので参考にしてみてください!
https://qiita.com/hohohoris/items/8aecf6a99b278d67d13b
retrun ChangeNotifierProvider<HomeController>(
create: (_) => HomeController(),
child: SingleChildScrollView(
// 略
)
とすると `main.dart` でMultiProviderしなくてもpageごとにできると思います!
class HomeController with ChangeNotifier{ | ||
HomeController(); | ||
|
||
Future<User> getMeById(String userId) async { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
基本Controllerのメソッドは戻り値を Future<void>
にして、 Controllerのプロパティで Userを持ってあげます。
するとFuture型を処理せずともUserという型でWidgetに通知できるからです。
具体的には、
Future<User> getMeById(String userId) async { | |
User user; | |
Future<void> getMeById(String userId) async { | |
await Future.delayed(Duration(seconds: 1)); | |
user = User(name: "test", imgUrl: "https://dot.asahi.com/S2000/upload/2019100100055_1.jpg"); | |
notifyListeners() |
MainページとしてHome,Talk,Profileは一体なのでMultiProviderとして4つのモデルを定義した。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
恐縮ながら 🙇
間違ってたり、もうこれで決まってるからってとこあったら遠慮なく言ってください!
lib/ui/pages/main/main.dart
Outdated
@override | ||
Widget build(BuildContext context) { | ||
final _mainController = Provider.of<MainController>(context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
メソッド内で定義されたものはそもそもメソッド内のスコープでしか使用できないのでprivateにする必要はなさそうですね 👌
lib/ui/pages/main/main.dart
Outdated
); | ||
} | ||
|
||
Widget bottomNavigation() { | ||
Widget bottomNavigation(BuildContext context) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
逆にここは他のファイルから参照されていなければprivateにして良さそうです 👌
lib/ui/pages/main/main.dart
Outdated
); | ||
} | ||
|
||
Widget bottomNavigation() { | ||
Widget bottomNavigation(BuildContext context) { | ||
final _mainController = Provider.of<MainController>(context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上に同じくprivateにする必要はなさそうですね 👌
lib/ui/pages/main/talk/talk.dart
Outdated
@@ -10,8 +12,9 @@ class TalkPage extends StatefulWidget { | |||
class _TalkPageState extends State<TalkPage> { | |||
@override | |||
Widget build(BuildContext context) { | |||
final _talkController = Provider.of<TalkController>(context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
privateにする必要はなさそうですね 👌
他にも同様の場所がありそうなので確認して頂けると 🙇
lib/ui/pages/main/home/home.dart
Outdated
return Center( | ||
child: Text("該当するユーザーがいません"), | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return Center( | |
child: Text("該当するユーザーがいません"), | |
); | |
return const Center( | |
child: Text("該当するユーザーがいません"), | |
); |
のように書けますかね?
const
をつけると、コンパイル時に確定できるので、パフォーマンスが上がります
以下参考
Use const widgets where possible. (This is equivalent to caching a widget and re-using it.)
https://medium.com/flutter-jp/state-performance-7a5f67d62edd
lib/ui/pages/main/home/home.dart
Outdated
@@ -209,3 +75,102 @@ class HomePage extends StatelessWidget { | |||
); | |||
} | |||
} | |||
|
|||
class MyTile extends StatelessWidget { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
少しfileが必要以上に肥大化しいてるように思えるので、個別のWidgetは /Widgets/
を作ってその中に入れても良さそうですね 🙆♂️
このままなら
class MyTile extends StatelessWidget { | |
class _MyTile extends StatelessWidget { |
とできそうでしょうか?
lib/ui/pages/main/home/home.dart
Outdated
); | ||
} | ||
|
||
HomePage({Key key}) : super(key:key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これってないとコンパイルできませんっけ? 🤔
lib/ui/pages/main/home/home.dart
Outdated
@@ -7,7 +7,7 @@ import 'package:chat_flutter/config/app_text_size.dart'; | |||
import 'package:provider/provider.dart'; | |||
|
|||
class HomePage extends StatelessWidget { | |||
HomePage({Key key}) : super(key:key); | |||
HomePage({Key key}) : super(key: key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
別の箇所で言及しましたが、ここがなくても良いって箇所が他にもありそうですね 🙇
user = User( | ||
name: "test", | ||
imgUrl: "https://dot.asahi.com/S2000/upload/2019100100055_1.jpg"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
user = User( | |
name: "test", | |
imgUrl: "https://dot.asahi.com/S2000/upload/2019100100055_1.jpg"); | |
user = User( | |
name: "test", | |
imgUrl: "https://dot.asahi.com/S2000/upload/2019100100055_1.jpg", | |
); |
の方が自然ですかね?
currentIndex: _controller.currentIndex, | ||
items: [ | ||
BottomNavigationBarItem( | ||
icon: Icon( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
別箇所でも言及しましたが、const
付けれそうですね 👌
https://github.com/flutter/flutter/blob/master/analysis_options.yaml
ここら辺の導入に関してもうまく決めれると良さそうですね 🙆♂️
コメントして気づきましたが、 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
squash
に関して
きっと @ho2ri2s あたりが詳しいと思うのでmtgのときに聞いてみてください 👍
constの付け忘れや、statelessWidgetの別ファイルへの切り出しなど、まだ改善の余地はありそうですがそこはまた後で決めるって感じでもいいかもですね(本当はお勧めしませんが) 🙆♂️
お疲れ様です 👍
#18 がマスターにマージされたら |
), | ||
ListView.builder( | ||
physics: ScrollPhysics(), | ||
scrollDirection: Axis.vertical, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://api.flutter.dev/flutter/widgets/ListView/ListView.builder.html
今更で申し訳ないんですけどここは defaultがAxis.vertical
なのでいらなさそうですね 🆗
scrollDirection: Axis.vertical, |
), | ||
ListView.builder( | ||
physics: ScrollPhysics(), | ||
scrollDirection: Axis.vertical, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
お疲れ様でした👏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
色々とありがとうございました 🙇
issue
#13
やったこと
pages
ディレクトリ内でmain
ディレクトリを作成main
ディレクトリ内にhome
talk
profile
ディレクトリを作成しdartファイルを分ける