Flutter 7.2 - TabBar & BottomNavigationBar

 1, Tab bar: 

class _TabScreenState extends State<TabScreen> {
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2, // số tab
child: Scaffold(
appBar: AppBar(
title: Text('Meals'),
bottom: TabBar(
tabs: [
Tab(
icon: Icon(Icons.category),
text: 'Category',
),
Tab(
icon: Icon(Icons.star),
text: 'Favorites',
),
],
),
),
body: TabBarView(
children: [
CategoriesScreen(),
FavoritesScreen(),
],
),
));
}
}


2, BottomNavigationBar

class _TabScreenState extends State<TabScreen> {
final List<Map<String, Object>> _pages = [
{'title': 'Categories', 'page': CategoriesScreen()},
{'title': 'Favorites', 'page': FavoritesScreen()},
];
int _selectedPageIndex = 0;

void _selectPage(int index) {
setState(() {
_selectedPageIndex = index;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_pages[_selectedPageIndex]['title'] as String),
),
body: _pages[_selectedPageIndex]['page'] as Widget,
bottomNavigationBar: BottomNavigationBar(
onTap: _selectPage,
backgroundColor: Theme.of(context).primaryColor,
unselectedItemColor: Colors.white,
selectedItemColor: Colors.lightGreenAccent,
currentIndex: _selectedPageIndex,
type: BottomNavigationBarType.fixed,
// kiu bottom bar
items: [
BottomNavigationBarItem(
backgroundColor: Theme.of(context).primaryColor,
icon: Icon(Icons.category),
title: Text('Categories'),
),
BottomNavigationBarItem(
backgroundColor: Theme.of(context).primaryColor,
icon: Icon(Icons.star),
title: Text('Favorites'),
),
],
),
);
}
}


3, Left navigation bar

Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_pages[_selectedPageIndex]['title'] as String),
),
drawer: MainDrawer(), // navigation left
body: _pages[_selectedPageIndex]['page'] as Widget,


main_drawer:

class MainDrawer extends StatelessWidget {

Widget buildListTile(String title, IconData icon) {
return ListTile(
leading: Icon(icon, size: 26),
title: Text(
title,
style: TextStyle(
fontFamily: 'RobotoCondensed',
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
onTap: () {

},
);
}


@override
Widget build(BuildContext context) {
return Drawer(
child: Column(
children: [
Container(
height: 120,
width: double.infinity,
padding: EdgeInsets.all(20),
alignment: Alignment.centerLeft,
color: Theme
.of(context)
.accentColor,
child: Text(
'Cooking Up!',
style: TextStyle(
fontWeight: FontWeight.w900,
fontSize: 30,
color: Theme
.of(context)
.primaryColor,
),
),
),
SizedBox(
height: 20,
),
buildListTile('Meals', Icons.restaurant),
buildListTile('Filters', Icons.settings),
],
),);
}
}


Nhận xét

Bài đăng phổ biến từ blog này

Flutter 4.5 - Custom theme, font, image

Flutter 1.2 - Flutter Architecture

Flutter 2.5 - First App | Building a Widget Tree