|
import 'package:flutter/material.dart'; |
|
import 'package:provider/provider.dart'; |
|
|
|
void main() => runApp(MyApp()); |
|
|
|
class MyApp extends StatelessWidget { |
|
@override |
|
Widget build(BuildContext context) { |
|
return MaterialApp( |
|
title: 'Flutter Demo', |
|
theme: ThemeData( |
|
primaryColor: Colors.cyan, |
|
backgroundColor: Colors.cyan[50], |
|
bottomAppBarColor: Colors.cyan, |
|
), |
|
darkTheme: ThemeData.dark(), |
|
home: ChangeNotifierProvider<BottomNavigationSelectedState>( |
|
child: BottomNavigationBarPage(), |
|
create: (BuildContext context) => BottomNavigationSelectedState(), |
|
)); |
|
} |
|
} |
|
|
|
class BottomNavigationBarPage extends StatelessWidget { |
|
final currentTab = [ |
|
TimerPage(), |
|
Home(), |
|
Profile(), |
|
Setting(), |
|
]; |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
var provider = Provider.of<BottomNavigationSelectedState>(context); |
|
return Scaffold( |
|
appBar: AppBar( |
|
title: Text('サンプル'), |
|
), |
|
body: currentTab[provider.currentIndex], |
|
bottomNavigationBar: BottomNavigationBar( |
|
type: BottomNavigationBarType.fixed, |
|
selectedItemColor: Colors.cyan, |
|
currentIndex: provider.currentIndex, |
|
onTap: (index) { |
|
provider.currentIndex = index; |
|
}, |
|
items: [ |
|
BottomNavigationBarItem( |
|
icon: new Icon(Icons.timer), title: new Text('Timer')), |
|
BottomNavigationBarItem( |
|
icon: new Icon(Icons.home), |
|
title: new Text('Home'), |
|
), |
|
BottomNavigationBarItem( |
|
icon: new Icon(Icons.near_me), |
|
title: new Text('Profile'), |
|
), |
|
BottomNavigationBarItem( |
|
icon: new Icon(Icons.settings), title: new Text('Settings')), |
|
], |
|
), |
|
); |
|
} |
|
} |
|
|
|
class TimerPage extends StatelessWidget { |
|
@override |
|
Widget build(BuildContext context) { |
|
return Container( |
|
color: Colors.cyan[50], |
|
); |
|
} |
|
} |
|
|
|
class Home extends StatelessWidget { |
|
@override |
|
Widget build(BuildContext context) { |
|
return Container( |
|
color: Colors.pink[50], |
|
); |
|
} |
|
} |
|
|
|
class Profile extends StatelessWidget { |
|
@override |
|
Widget build(BuildContext context) { |
|
return Container( |
|
color: Colors.black, |
|
); |
|
} |
|
} |
|
|
|
class Setting extends StatelessWidget { |
|
@override |
|
Widget build(BuildContext context) { |
|
return Container( |
|
color: Colors.green[50], |
|
); |
|
} |
|
} |
|
|
|
class BottomNavigationSelectedState with ChangeNotifier { |
|
int _currentIndex = 0; |
|
|
|
get currentIndex => _currentIndex; |
|
|
|
set currentIndex(int index) { |
|
_currentIndex = index; |
|
notifyListeners(); |
|
} |
|
} |