Skip to content

Instantly share code, notes, and snippets.

@tanabe1478
Last active April 29, 2020 16:41
Show Gist options
  • Select an option

  • Save tanabe1478/f4642f723f49648a2692b6ad7cd18c55 to your computer and use it in GitHub Desktop.

Select an option

Save tanabe1478/f4642f723f49648a2692b6ad7cd18c55 to your computer and use it in GitHub Desktop.
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment