-
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
Changes from 9 commits
b4aaa0c
387cca8
f72e08d
9044e30
ebdf747
3cafa59
0cf5f17
abcd197
9179ad5
02f1e31
e41481e
1ef3712
4959e8f
52cdb0c
53080e9
61ce4a2
7c87aa9
1e9cb92
6b8e569
ab57921
0002225
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,21 +1,46 @@ | ||||||||||||||
import 'package:chat_flutter/config/app_space.dart'; | ||||||||||||||
import 'package:chat_flutter/ui/molecules/home/list_tile.dart'; | ||||||||||||||
import 'package:chat_flutter/ui/pages/main/home/home_controller.dart'; | ||||||||||||||
import 'package:flutter/material.dart'; | ||||||||||||||
import 'package:chat_flutter/model/user.dart'; | ||||||||||||||
import 'package:chat_flutter/model/group.dart'; | ||||||||||||||
|
||||||||||||||
import 'package:chat_flutter/config/app_text_size.dart'; | ||||||||||||||
import 'package:provider/provider.dart'; | ||||||||||||||
|
||||||||||||||
class HomePage extends StatelessWidget { | ||||||||||||||
@override | ||||||||||||||
Widget build(BuildContext context) { | ||||||||||||||
final _homeController = Provider.of<HomeController>(context); | ||||||||||||||
return SingleChildScrollView( | ||||||||||||||
child: Column( | ||||||||||||||
children: <Widget>[ | ||||||||||||||
HomePageListTile( | ||||||||||||||
name: 'name', | ||||||||||||||
imgUrl: 'https://dot.asahi.com/S2000/upload/2019100100055_1.jpg', | ||||||||||||||
isMe: true, | ||||||||||||||
FutureBuilder( | ||||||||||||||
future: _homeController.getMeById('test'), | ||||||||||||||
builder: (BuildContext context, AsyncSnapshot<User> snapshot) { | ||||||||||||||
if (snapshot.hasError) { | ||||||||||||||
return Center(child: Text('Error: ${snapshot.error}')); | ||||||||||||||
} | ||||||||||||||
switch (snapshot.connectionState) { | ||||||||||||||
case ConnectionState.waiting: // データの取得まち | ||||||||||||||
return Center( | ||||||||||||||
child: CircularProgressIndicator(), | ||||||||||||||
); | ||||||||||||||
|
||||||||||||||
default: | ||||||||||||||
if (snapshot.hasData) { | ||||||||||||||
return HomePageListTile( | ||||||||||||||
name: snapshot.data.name, | ||||||||||||||
imgUrl: snapshot.data.imgUrl, | ||||||||||||||
isMe: true, | ||||||||||||||
); | ||||||||||||||
} else { | ||||||||||||||
return Center( | ||||||||||||||
child: Text("該当するユーザーがいません"), | ||||||||||||||
); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
のように書けますかね?
https://medium.com/flutter-jp/state-performance-7a5f67d62edd |
||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
}, | ||||||||||||||
), | ||||||||||||||
Container( | ||||||||||||||
constraints: BoxConstraints( | ||||||||||||||
|
@@ -46,7 +71,7 @@ class HomePage extends StatelessWidget { | |||||||||||||
), | ||||||||||||||
), | ||||||||||||||
FutureBuilder( | ||||||||||||||
future: _getGroupList(), | ||||||||||||||
future: _homeController.getGroupList(), | ||||||||||||||
builder: ( | ||||||||||||||
BuildContext context, | ||||||||||||||
AsyncSnapshot<List<Group>> snapshot, | ||||||||||||||
|
@@ -120,7 +145,7 @@ class HomePage extends StatelessWidget { | |||||||||||||
), | ||||||||||||||
), | ||||||||||||||
FutureBuilder( | ||||||||||||||
future: _getFriendList(), | ||||||||||||||
future: _homeController.getFriendList(), | ||||||||||||||
builder: ( | ||||||||||||||
BuildContext context, | ||||||||||||||
AsyncSnapshot<List<User>> snapshot, | ||||||||||||||
|
@@ -185,56 +210,4 @@ class HomePage extends StatelessWidget { | |||||||||||||
), | ||||||||||||||
); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
Future<List<Group>> _getGroupList() async { | ||||||||||||||
final List<Group> groupList = [ | ||||||||||||||
Group( | ||||||||||||||
name: "Sport", | ||||||||||||||
imgUrl: "https://prtimes.jp/i/24101/70/resize/d24101-70-320114-0.jpg", | ||||||||||||||
), | ||||||||||||||
Group( | ||||||||||||||
name: "Study", | ||||||||||||||
imgUrl: "https://prtimes.jp/i/24101/70/resize/d24101-70-320114-0.jpg", | ||||||||||||||
), | ||||||||||||||
Group( | ||||||||||||||
name: "Hobby", | ||||||||||||||
imgUrl: "https://prtimes.jp/i/24101/70/resize/d24101-70-320114-0.jpg", | ||||||||||||||
), | ||||||||||||||
]; | ||||||||||||||
|
||||||||||||||
await Future.delayed(Duration(seconds: 1)); | ||||||||||||||
return await Future.value(groupList); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
Future<List<User>> _getFriendList() async { | ||||||||||||||
final List<User> friendList = [ | ||||||||||||||
User( | ||||||||||||||
name: "Alex", | ||||||||||||||
imgUrl: | ||||||||||||||
"https://pbs.twimg.com/profile_images/581025665727655936/9CnwZZ6j.jpg", | ||||||||||||||
isMe: false, | ||||||||||||||
), | ||||||||||||||
User( | ||||||||||||||
name: "Alex2(笑)", | ||||||||||||||
imgUrl: | ||||||||||||||
"https://pbs.twimg.com/profile_images/581025665727655936/9CnwZZ6j.jpg", | ||||||||||||||
isMe: false, | ||||||||||||||
), | ||||||||||||||
User( | ||||||||||||||
name: "Jack", | ||||||||||||||
imgUrl: | ||||||||||||||
"https://pbs.twimg.com/profile_images/581025665727655936/9CnwZZ6j.jpg", | ||||||||||||||
isMe: false, | ||||||||||||||
), | ||||||||||||||
User( | ||||||||||||||
name: "Brian", | ||||||||||||||
imgUrl: | ||||||||||||||
"https://pbs.twimg.com/profile_images/581025665727655936/9CnwZZ6j.jpg", | ||||||||||||||
isMe: false, | ||||||||||||||
), | ||||||||||||||
]; | ||||||||||||||
|
||||||||||||||
await Future.delayed(Duration(seconds: 3)); | ||||||||||||||
return await Future.value(friendList); | ||||||||||||||
} | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,66 @@ | ||||||||||||||
import 'package:chat_flutter/model/group.dart'; | ||||||||||||||
import 'package:chat_flutter/model/user.dart'; | ||||||||||||||
import 'package:flutter/material.dart'; | ||||||||||||||
|
||||||||||||||
class HomeController with ChangeNotifier{ | ||||||||||||||
HomeController(); | ||||||||||||||
|
||||||||||||||
Future<User> getMeById(String userId) async { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 基本Controllerのメソッドは戻り値を
Suggested change
|
||||||||||||||
User user = User(name: "test", imgUrl: "https://dot.asahi.com/S2000/upload/2019100100055_1.jpg"); | ||||||||||||||
|
||||||||||||||
await Future.delayed(Duration(seconds: 1)); | ||||||||||||||
return await Future.value(user); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
Future<List<Group>> getGroupList() async { | ||||||||||||||
final List<Group> groupList = [ | ||||||||||||||
Group( | ||||||||||||||
name: "Sport", | ||||||||||||||
imgUrl: "https://prtimes.jp/i/24101/70/resize/d24101-70-320114-0.jpg", | ||||||||||||||
), | ||||||||||||||
Group( | ||||||||||||||
name: "Study", | ||||||||||||||
imgUrl: "https://prtimes.jp/i/24101/70/resize/d24101-70-320114-0.jpg", | ||||||||||||||
), | ||||||||||||||
Group( | ||||||||||||||
name: "Hobby", | ||||||||||||||
imgUrl: "https://prtimes.jp/i/24101/70/resize/d24101-70-320114-0.jpg", | ||||||||||||||
), | ||||||||||||||
]; | ||||||||||||||
|
||||||||||||||
await Future.delayed(Duration(seconds: 1)); | ||||||||||||||
return await Future.value(groupList); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
Future<List<User>> getFriendList() async { | ||||||||||||||
final List<User> friendList = [ | ||||||||||||||
User( | ||||||||||||||
name: "Alex", | ||||||||||||||
imgUrl: | ||||||||||||||
"https://pbs.twimg.com/profile_images/581025665727655936/9CnwZZ6j.jpg", | ||||||||||||||
isMe: false, | ||||||||||||||
), | ||||||||||||||
User( | ||||||||||||||
name: "Alex2(笑)", | ||||||||||||||
imgUrl: | ||||||||||||||
"https://pbs.twimg.com/profile_images/581025665727655936/9CnwZZ6j.jpg", | ||||||||||||||
isMe: false, | ||||||||||||||
), | ||||||||||||||
User( | ||||||||||||||
name: "Jack", | ||||||||||||||
imgUrl: | ||||||||||||||
"https://pbs.twimg.com/profile_images/581025665727655936/9CnwZZ6j.jpg", | ||||||||||||||
isMe: false, | ||||||||||||||
), | ||||||||||||||
User( | ||||||||||||||
name: "Brian", | ||||||||||||||
imgUrl: | ||||||||||||||
"https://pbs.twimg.com/profile_images/581025665727655936/9CnwZZ6j.jpg", | ||||||||||||||
isMe: false, | ||||||||||||||
), | ||||||||||||||
]; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここら辺はMockを作成しても良さそうですね 🙆♂️ (チームでの話し合いを聞いていなかったのでもし見当違いのことを言っていたらすいません 🙇 ) |
||||||||||||||
|
||||||||||||||
await Future.delayed(Duration(seconds: 3)); | ||||||||||||||
return await Future.value(friendList); | ||||||||||||||
} | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,17 @@ | ||
import 'package:chat_flutter/ui/molecules/home/app_bar.dart'; | ||
import 'package:chat_flutter/ui/molecules/profile/app_bar.dart'; | ||
import 'package:chat_flutter/ui/molecules/talk/app_bar.dart'; | ||
import 'package:chat_flutter/ui/pages/home.dart'; | ||
import 'package:chat_flutter/ui/pages/profile.dart'; | ||
import 'package:chat_flutter/ui/pages/talk.dart'; | ||
import 'package:chat_flutter/ui/pages/main/home/home.dart'; | ||
import 'package:chat_flutter/ui/pages/main/main_controller.dart'; | ||
import 'package:chat_flutter/ui/pages/main/profile/profile.dart'; | ||
import 'package:chat_flutter/ui/pages/main/talk/talk.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:provider/provider.dart'; | ||
|
||
class MainPage extends StatefulWidget { | ||
@override | ||
_MainPageState createState() => _MainPageState(); | ||
} | ||
|
||
class _MainPageState extends State<MainPage> { | ||
int _currentIndex = 0; | ||
void changePage(int index) { | ||
setState( | ||
() => _currentIndex = index, | ||
); | ||
} | ||
|
||
class MainPage extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
final _mainController = Provider.of<MainController>(context); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. メソッド内で定義されたものはそもそもメソッド内のスコープでしか使用できないのでprivateにする必要はなさそうですね 👌 |
||
final List appBarList = [ | ||
HomePageAppBar(), | ||
TalkPageAppBar(), | ||
|
@@ -40,20 +31,21 @@ class _MainPageState extends State<MainPage> { | |
]; | ||
|
||
return Scaffold( | ||
appBar: appBarList[_currentIndex], | ||
backgroundColor: backgroundColor[_currentIndex], | ||
bottomNavigationBar: bottomNavigation(), | ||
body: pages[_currentIndex], | ||
appBar: appBarList[_mainController.currentIndex], | ||
backgroundColor: backgroundColor[_mainController.currentIndex], | ||
bottomNavigationBar: bottomNavigation(context), | ||
body: pages[_mainController.currentIndex], | ||
); | ||
} | ||
|
||
Widget bottomNavigation() { | ||
Widget bottomNavigation(BuildContext context) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 逆にここは他のファイルから参照されていなければprivateにして良さそうです 👌 |
||
final _mainController = Provider.of<MainController>(context); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 上に同じくprivateにする必要はなさそうですね 👌 |
||
return BottomNavigationBar( | ||
backgroundColor: Colors.white, | ||
type: BottomNavigationBarType.fixed, | ||
showSelectedLabels: false, | ||
showUnselectedLabels: false, | ||
currentIndex: _currentIndex, | ||
currentIndex: _mainController.currentIndex, | ||
items: [ | ||
BottomNavigationBarItem( | ||
icon: Icon( | ||
|
@@ -80,9 +72,9 @@ class _MainPageState extends State<MainPage> { | |
), | ||
), | ||
], | ||
onTap: (index) => setState( | ||
() => _currentIndex = index, | ||
), | ||
onTap: (index){ | ||
_mainController.changePage(index); | ||
} | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class MainController with ChangeNotifier{ | ||
int currentIndex; | ||
MainController(){ | ||
currentIndex = 0; | ||
} | ||
|
||
void changePage(int index){ | ||
currentIndex = index; | ||
notifyListeners(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:chat_flutter/model/user.dart'; | ||
|
||
class ProfileController with ChangeNotifier{ | ||
ProfileController(); | ||
|
||
Future<User> getUserById(String userId) async { | ||
User user = User(name: "test", imgUrl: ""); | ||
|
||
await Future.delayed(Duration(seconds: 1)); | ||
return await Future.value(user); | ||
} | ||
|
||
void changeProfileInfo(){ | ||
//Firebaseへの変更通知 | ||
} | ||
} |
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