Skip to content

Instantly share code, notes, and snippets.

@utgupta27
Created June 28, 2021 05:34
Show Gist options
  • Save utgupta27/e7d5e37ae4a7a56fda80a3bb59643e20 to your computer and use it in GitHub Desktop.
Save utgupta27/e7d5e37ae4a7a56fda80a3bb59643e20 to your computer and use it in GitHub Desktop.
Switch to Multiple Pages using Bottom Navigation Bar Widget in Flutter.
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
class BottomNavyBar extends StatelessWidget {
BottomNavyBar({
Key? key,
this.selectedIndex = 0,
this.showElevation = true,
this.iconSize = 24,
this.backgroundColor,
this.itemCornerRadius = 50,
this.containerHeight = 56,
this.animationDuration = const Duration(milliseconds: 270),
this.mainAxisAlignment = MainAxisAlignment.spaceBetween,
required this.items,
required this.onItemSelected,
this.curve = Curves.linear,
}) : assert(items.length >= 2 && items.length <= 5),
super(key: key);
final int selectedIndex;
final double iconSize;
final Color? backgroundColor;
final bool showElevation;
final Duration animationDuration;
final List<BottomNavyBarItem> items;
final ValueChanged<int> onItemSelected;
final MainAxisAlignment mainAxisAlignment;
final double itemCornerRadius;
final double containerHeight;
final Curve curve;
@override
Widget build(BuildContext context) {
final bgColor = backgroundColor ?? Theme.of(context).bottomAppBarColor;
return Container(
decoration: BoxDecoration(
color: bgColor,
boxShadow: [
if (showElevation)
const BoxShadow(
color: Colors.black12,
blurRadius: 2,
),
],
),
child: SafeArea(
child: Container(
width: double.infinity,
height: containerHeight,
padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 8),
child: Row(
mainAxisAlignment: mainAxisAlignment,
children: items.map((item) {
var index = items.indexOf(item);
return GestureDetector(
onTap: () => onItemSelected(index),
child: _ItemWidget(
item: item,
iconSize: iconSize,
isSelected: index == selectedIndex,
backgroundColor: bgColor,
itemCornerRadius: itemCornerRadius,
animationDuration: animationDuration,
curve: curve,
),
);
}).toList(),
),
),
),
);
}
}
class _ItemWidget extends StatelessWidget {
final double iconSize;
final bool isSelected;
final BottomNavyBarItem item;
final Color backgroundColor;
final double itemCornerRadius;
final Duration animationDuration;
final Curve curve;
const _ItemWidget({
Key? key,
required this.item,
required this.isSelected,
required this.backgroundColor,
required this.animationDuration,
required this.itemCornerRadius,
required this.iconSize,
this.curve = Curves.linear,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Semantics(
container: true,
selected: isSelected,
child: AnimatedContainer(
width: isSelected ? 200 : 100,
height: double.maxFinite,
duration: animationDuration,
curve: curve,
decoration: BoxDecoration(
color:
isSelected ? item.activeColor.withOpacity(0.2) : backgroundColor,
borderRadius: BorderRadius.circular(itemCornerRadius),
),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
physics: NeverScrollableScrollPhysics(),
child: Container(
width: isSelected ? 200 : 100,
padding: EdgeInsets.symmetric(horizontal: 10),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
IconTheme(
data: IconThemeData(
size: iconSize,
color: isSelected
? item.activeColor.withOpacity(1)
: item.inactiveColor == null
? item.activeColor
: item.inactiveColor,
),
child: item.icon,
),
if (isSelected)
Expanded(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 4),
child: DefaultTextStyle.merge(
style: TextStyle(
color: item.activeColor,
fontWeight: FontWeight.bold,
),
maxLines: 1,
textAlign: item.textAlign,
child: item.title,
),
),
),
],
),
),
),
),
);
}
}
class BottomNavyBarItem {
BottomNavyBarItem({
required this.icon,
required this.title,
this.activeColor = Colors.blue,
this.textAlign,
this.inactiveColor = Colors.grey,
});
final Widget icon;
final Widget title;
final Color activeColor;
final Color? inactiveColor;
final TextAlign? textAlign;
}
import 'package:flutter/material.dart';
import 'package:<Your-project-directory>/navigationBar/bottomNavigationBar.dart';
import 'package:<Your-project-directory>/todoPage/todoPage.dart';
import 'package:<Your-project-directory>/notesPage/notesPage.dart';
import 'package:<Your-project-directory>/reminderPage/reminderPage.dart';
import 'package:<Your-project-directory>/drawer/drawer.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _currentIndex = 0;
final List<Widget> _screens = [TodoPage(), NotesPage(), ReminderPage()];
void onItemTapped(int val) {
setState(() {
_currentIndex = val;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Project Name"),
backgroundColor: Colors.blue[800],
),
drawer: MyDrawer(),
body: _screens[_currentIndex],
bottomNavigationBar: new BottomNavyBar(
items: [
BottomNavyBarItem(
icon: Icon(
Icons.task_alt,
size: 30,
),
title: Text(
" Item 1",
style: TextStyle(fontSize: 20),
)),
BottomNavyBarItem(
icon: Icon(
Icons.note_alt_outlined,
size: 30,
),
title: Text(" Item 2", style: TextStyle(fontSize: 20))),
BottomNavyBarItem(
icon: Icon(
Icons.alarm_on_outlined,
size: 30,
),
title: Text(" Item 3", style: TextStyle(fontSize: 20)))
],
selectedIndex: _currentIndex,
onItemSelected: onItemTapped,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment