Skip to content
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

Merged
merged 21 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import 'package:provider/provider.dart';
import 'package:chat_flutter/di.dart';
import 'package:chat_flutter/ui/pages/add_friend.dart';
import 'package:chat_flutter/ui/pages/create_group.dart';
import 'package:chat_flutter/ui/pages/main.dart';
import 'package:chat_flutter/ui/pages/profile_edit.dart';
import 'package:chat_flutter/ui/pages/profile.dart';
import 'package:chat_flutter/ui/pages/main/main.dart';
import 'package:chat_flutter/ui/pages/main/profile/profile_edit.dart';
import 'package:chat_flutter/ui/pages/main/profile/profile.dart';
import 'package:chat_flutter/ui/pages/room.dart';
import 'package:chat_flutter/ui/pages/sign_in.dart';
import 'package:chat_flutter/ui/pages/sign_up.dart';
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions lib/ui/pages/main/home/home_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:flutter/material.dart';

class HomeController with ChangeNotifier{

}
42 changes: 17 additions & 25 deletions lib/ui/pages/main.dart → lib/ui/pages/main/main.dart
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);

Choose a reason for hiding this comment

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

メソッド内で定義されたものはそもそもメソッド内のスコープでしか使用できないのでprivateにする必要はなさそうですね 👌

final List appBarList = [
HomePageAppBar(),
TalkPageAppBar(),
Expand All @@ -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) {

Choose a reason for hiding this comment

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

逆にここは他のファイルから参照されていなければprivateにして良さそうです 👌

final _mainController = Provider.of<MainController>(context);

Choose a reason for hiding this comment

The 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(
Expand All @@ -80,9 +72,9 @@ class _MainPageState extends State<MainPage> {
),
),
],
onTap: (index) => setState(
() => _currentIndex = index,
),
onTap: (index){
_mainController.changePage(index);
}
);
}
}
13 changes: 13 additions & 0 deletions lib/ui/pages/main/main_controller.dart
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();
}
}
File renamed without changes.
5 changes: 5 additions & 0 deletions lib/ui/pages/main/profile/profile_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:flutter/material.dart';

class ProfileController with ChangeNotifier{

}
5 changes: 5 additions & 0 deletions lib/ui/pages/main/profile/profile_edit_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:flutter/material.dart';

class ProfileEditController with ChangeNotifier{

}
File renamed without changes.
5 changes: 5 additions & 0 deletions lib/ui/pages/main/talk/talk_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:flutter/material.dart';

class TalkController with ChangeNotifier{

}