Add lines on Bottom Navigation Bar - flutter

I wanna Add this type of line on bottomNavigation Bar in flutter I want this type of blue lines when navigate or tap to another tab
this is my code
BottomNavigationBar( //Bottom navigation bar
onTap: onTabTapped,
showSelectedLabels: true,
currentIndex: _currentIndex,
items: [
BottomNavigationBarItem( //Items of BottomNavigation Bar
icon: Image.asset(
unSelectedTabs[0],
width: iconSize,
height: iconSize,
),
title:
Text("Home", style: TextStyle(color: _currentIndex == 0 ? AppTheme.mainThemeColor() : AppTheme.hintTextColor())),
activeIcon: Image.asset(
selectedTabs[0],
width: iconSize,
height: iconSize,
),
),
.........
other childItem
Please help to find out this

This package should help you achieve that. You may just need to customize it per your need.

you can use tabbar instead of BottomNavigationBar
Here is a sample
class Home extends StatefulWidget {
Home({Key key, this.title}) : super(key: key);
final String title;
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> with TickerProviderStateMixin {
final PageStorageBucket bucket = PageStorageBucket();
TabController tabController;
final List<Widget> mainScreens = [
HomePage(),
FriendHomePage(),
QrPage(),
BillingHomePage(),
BorrowedHomePage(),
];
#override
void initState() {
super.initState();
tabController = TabController(initialIndex: 0, length: 5, vsync: this);
}
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: true,
body: SafeArea(
child: PageStorage(
child: TabBarView(
controller: tabController,
physics: NeverScrollableScrollPhysics(),
children: mainScreens,
),
bucket: bucket,
),
),
bottomNavigationBar: SafeArea(
child: Material(
color: Colors.white,
elevation: 10,
child: BottomAppBar(
notchMargin: 8,
shape: CircularNotchedRectangle(),
child: TabBar(
tabs: [
Tab(
icon: ImageIcon(
AssetImage(
'assets/bottombar/home.png',
),
),
text: 'Үндсэн'),
Tab(
icon: ImageIcon(
AssetImage(
'assets/bottombar/friends.png',
),
),
text: 'Найзууд',
),
SizedBox(),
Tab(
icon: ImageIcon(
AssetImage(
'assets/bottombar/billing.png',
),
),
text: 'Төлбөр',
),
Tab(
icon: ImageIcon(
AssetImage(
'assets/bottombar/awlaga.png',
),
),
text: 'Авлага',
)
],
labelStyle: TextStyle(fontSize: 10),
labelColor: Theme.of(context).primaryColor,
unselectedLabelColor: Theme.of(context).accentColor,
isScrollable: false,
indicatorSize: TabBarIndicatorSize.tab,
indicatorPadding: EdgeInsets.all(5.0),
indicatorColor: Theme.of(context).primaryColor,
controller: tabController,
indicator: UnderlineTabIndicator(
insets: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 75.0),
borderSide:
BorderSide(color: Theme.of(context).primaryColor, width: 3),
),
),
),
),
),
floatingActionButton: Material(
elevation: 10,
color: Theme.of(context).primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(36)),
),
child: InkWell(
customBorder: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(36)),
),
onTap: () => tabController.animateTo(2),
child: Container(
width: 65,
height: 65,
padding: EdgeInsets.all(8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset(
'assets/bottombar/qr.png',
height: 25,
fit: BoxFit.cover,
color: Theme.of(context).accentColor,
),
],
),
),
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
drawer: Drawer(),
);
}
}

You can do like this, this is not the best way though it worked for me.
Also this is suitable for 2-3 items
bottomNavigationBar: Column(
mainAxisSize: MainAxisSize.min,
children: [
Align(
alignment: model.currentIndex == 0
? Alignment.bottomLeft
: model.currentIndex == 1
? Alignment.bottomCenter
: Alignment.bottomRight,
child: Container(
height: 2,
color: AppColors.secondaryColor,
width: width(context) * 0.33,
),
),
BottomNavigationBar(
....
currentIndex: model.currentIndex,
onTap: (demo) {
model.setIndex(demo);
}),
],
),

Related

Exception caught by widgets library in bottom navigation bar

Hi, anyone know what the problem is? I'm trying to add a bottom navigation bar, but the following message is showing i tried to add bottom navigation bar and this is the code i made.
class _HomePageState extends State<HomePage> {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Builder(builder: (context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
selectedFontSize: 12,
unselectedItemColor: Colors.grey,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Beranda',
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
label: 'Akademik',
),
BottomNavigationBarItem(
icon: Icon(Icons.credit_card),
label: 'Pembayaran',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
],
currentIndex: _selectedIndex,
selectedItemColor: primaryColor,
onTap: (index) {
setState(
() {
_selectedIndex = index;
},
);
},
),
body: Column(
children: [
_widgetOptions.elementAt(_selectedIndex),
body(),
],
),
);
}),
),
);
}
but when I try to run it the following error occurs, and I feel a little confused, I've tried to find some references but still can't find a solution that fits my case
Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...
lib\main.dart:1
√ Built build\app\outputs\flutter-apk\app-debug.apk.
Connecting to VM Service at ws://127.0.0.1:62216/ZIcDnqzIkLk=/ws
E/SurfaceSyncer( 7593): Failed to find sync for id=0
W/Parcel ( 7593): Expecting binder but got null!
D/EGL_emulation( 7593): app_time_stats: avg=7.90ms min=2.99ms max=180.93ms count=62
D/EGL_emulation( 7593): app_time_stats: avg=15.06ms min=3.37ms max=21.43ms count=60
════════ Exception caught by widgets library ═══════════════════════════════════
The following StackOverflowError was thrown building Builder:
Stack Overflow
The relevant error-causing widget was
Builder
lib\…\homepage\home_page.dart:31
When the exception was thrown, this was the stack
this my home_page.dart
class HomePage extends StatefulWidget {
const HomePage({super.key});
#override
State<HomePage> createState() => _HomePageState();
}
int _selectedIndex = 0;
final List<Widget> _widgetOptions = <Widget>[
const HomePage(),
const AcademicPage(),
const CreditPage(),
const ProfilePage(),
];
class _HomePageState extends State<HomePage> {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Scaffold(
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
selectedFontSize: 12,
unselectedItemColor: Colors.grey,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Beranda',
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
label: 'Akademik',
),
BottomNavigationBarItem(
icon: Icon(Icons.credit_card),
label: 'Pembayaran',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
],
currentIndex: _selectedIndex,
selectedItemColor: primaryColor,
onTap: (index) {
setState(
() {
_selectedIndex = index;
},
);
},
),
body: Column(
children: [
_widgetOptions.elementAt(_selectedIndex),
body(),
],
),
),
),
);
}
Widget body() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(
height: 30,
),
Container(
child: FutureBuilder<UserBiodata>(
future: AuthProvider().getDataMahasiswa(),
builder: (context, snapshot) {
if (snapshot.hasData) {
// return Text(snapshot.data!.status);
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${snapshot.data!.data.name}',
style: bold4,
),
Text(
'${snapshot.data!.data.prodi}',
style: bold6,
),
Text(
'Semester ${snapshot.data!.data.smt}',
style: regular6,
)
],
),
Image.asset(
'assets/images/user.png',
width: 60,
),
],
);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return const CircularProgressIndicator();
},
),
),
const SizedBox(
height: 30,
),
Image.asset(
'assets/images/statis.png',
),
const SizedBox(
height: 30,
),
Text(
'Akses Cepat',
style: bold4,
),
const SizedBox(
height: 18,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Container(
height: 90,
width: 90,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: const BorderRadius.all(
Radius.circular(20),
),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.2),
spreadRadius: 1,
blurRadius: 9,
offset: const Offset(1, 2), // changes position of shadow
),
],
),
child: Column(
children: [
const SizedBox(
height: 10,
),
Image.asset(
'assets/images/icon_jadwal.png',
height: 40,
),
const SizedBox(
height: 12,
),
Text(
'Jadwal Kuliah',
style: regular7,
)
],
),
),
Container(
height: 90,
width: 90,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: const BorderRadius.all(
Radius.circular(20),
),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.2),
spreadRadius: 1,
blurRadius: 9,
offset: const Offset(1, 2), // changes position of shadow
),
],
),
child: Column(
children: [
const SizedBox(
height: 10,
),
Image.asset(
'assets/images/icon_jadwal.png',
height: 40,
),
const SizedBox(
height: 12,
),
Text(
'Jadwal Kuliah',
style: regular7,
)
],
),
),
Container(
height: 90,
width: 90,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: const BorderRadius.all(
Radius.circular(20),
),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.2),
spreadRadius: 1,
blurRadius: 9,
offset: const Offset(1, 2), // changes position of shadow
),
],
),
child: Column(
children: [
const SizedBox(
height: 10,
),
Image.asset(
'assets/images/icon_jadwal.png',
height: 40,
),
const SizedBox(
height: 12,
),
Text(
'Jadwal Kuliah',
style: regular7,
)
],
),
),
],
),
const SizedBox(
height: 18,
),
Text(
'Dosen',
style: bold4,
),
const SizedBox(
height: 18,
),
Container(
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: const BorderRadius.all(
Radius.circular(8),
),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.2),
spreadRadius: 1,
blurRadius: 9,
offset: const Offset(1, 2), // changes position of shadow
),
],
),
child: Padding(
padding: const EdgeInsets.all(14),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Muhamad Fahmi Nugraha M.Kom',
style: bold6,
),
Text(
'kaprodi Fakultas Komputer',
style: bold7,
),
const SizedBox(
height: 12,
),
Row(
children: [
Container(
width: 94,
height: 32,
child: TextButton(
onPressed: () => showDialog(
context: context,
builder: (BuildContext context) =>
_signOutDialog(context),
),
style: TextButton.styleFrom(
backgroundColor: primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text(
"WhatsApp",
style: boldButton1,
),
),
),
const SizedBox(
width: 8,
),
Container(
width: 94,
height: 32,
child: TextButton(
onPressed: (() {
var result = Navigator.pushNamed(
context,
AppRoute.biodataeRoute,
);
}),
style: TextButton.styleFrom(
backgroundColor: button,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text(
"Detail",
style: boldButton2,
),
),
),
],
)
],
),
Image.asset(
'assets/images/user.png',
width: 50,
)
],
),
),
),
const SizedBox(
height: 18,
),
Container(
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: const BorderRadius.all(
Radius.circular(8),
),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.2),
spreadRadius: 1,
blurRadius: 9,
offset: const Offset(1, 2), // changes position of shadow
),
],
),
child: Padding(
padding: const EdgeInsets.all(14),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Muhamad Fahmi Nugraha M.Kom',
style: bold6,
),
Text(
'kaprodi Fakultas Komputer',
style: bold7,
),
const SizedBox(
height: 12,
),
Row(
children: [
Container(
width: 94,
height: 32,
child: TextButton(
onPressed: () {},
style: TextButton.styleFrom(
backgroundColor: primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text(
"WhatsApp",
style: boldButton1,
),
),
),
const SizedBox(
width: 8,
),
Container(
width: 94,
height: 32,
child: TextButton(
onPressed: () {},
style: TextButton.styleFrom(
backgroundColor: button,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text(
"Detail",
style: boldButton2,
),
),
),
],
)
],
),
Image.asset(
'assets/images/user.png',
width: 50,
)
],
),
),
),
],
);
}
}
Widget _signOutDialog(BuildContext context) {
return AlertDialog(
title: Column(
children: [
Image.asset(
'assets/images/ic_lgt.png',
)
],
),
content: const Text('Apakah kamu yakin ingin logout?'),
actions: <Widget>[
TextButton(
onPressed: () => AuthProvider().signOut(context),
child: const Text('Ya'),
),
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text(
'TIDAK',
style: TextStyle(color: Colors.red),
),
),
],
);
}
replace your code with this.
class _HomePageState extends State<HomePage> {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Padding(
padding: const EdgeInsets.all(8.0),
child:Scaffold(
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
selectedFontSize: 12,
unselectedItemColor: Colors.grey,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Beranda',
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
label: 'Akademik',
),
BottomNavigationBarItem(
icon: Icon(Icons.credit_card),
label: 'Pembayaran',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
],
currentIndex: _selectedIndex,
selectedItemColor: primaryColor,
onTap: (index) {
setState(
() {
_selectedIndex = index;
},
);
},
),
body: Column(
children: [
_widgetOptions.elementAt(_selectedIndex),
body(),
],
),
),
),
);
}

Is it possible to create bottom navigation bar in a stack flutter

I want to add a bottom navigation bar for my code. but don't know where to add it properly. is it possible to add it in a stack? if not how do I add a navigation bar to my code without errors? can someone please suggest to me what to do about this? here's my home.dart and bottom navigation bar code I hope to add.
home.dart
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget{
#override
_HomePageState createState() => _HomePageState();}
class _HomePageState extends State <HomePage>{
#override
Widget build (BuildContext context){
return Scaffold(
backgroundColor: Color(0xffEDEFF4),
body: Padding(
padding: const EdgeInsets.only(top: 40, left: 20, right: 20),
child: ListView (
children: [
buildSearchInput(),
Stack(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(0, 45, 10, 0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.0),
color: Color(0xffCAEC93),
),
height: 137,
width: 327,
child: Column(
children: const [
Padding(
padding: const EdgeInsets.fromLTRB(20, 40, 100, 40),
child: Text(
"Banana",
style: TextStyle(
fontFamily:'Roboto',
fontSize: 22,
color: Colors.black,
fontWeight: FontWeight.w700),
textAlign: TextAlign.left,
),
),
],
)
)
),
const Padding(
padding: EdgeInsets.fromLTRB(200, 0, 10, 0),
child: Image(
image: AssetImage("assets/images/banana.png"),
),
),
],
),
Stack(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(0, 45, 10, 0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.0),
color: Color(0xffCAEC93),
),
height: 137,
width: 327,
child: Column(
children: const [
Padding(
padding: const EdgeInsets.fromLTRB(20, 40, 100, 40),
child: Text(
"Tangerine",
style: TextStyle(
fontFamily:'Roboto',
fontSize: 22,
color: Colors.black,
fontWeight: FontWeight.w700),
textAlign: TextAlign.left,
),
),
],
)
)
),
const Padding(
padding: EdgeInsets.fromLTRB(200, 0, 10, 0),
child: Image(
image: AssetImage("assets/images/orange.png"),
),
),
],
),
Stack(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(0, 45, 10, 0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.0),
color: Color(0xffCAEC93),
),
height: 137,
width: 327,
child: Column(
children: const [
Padding(
padding: const EdgeInsets.fromLTRB(20, 40, 100, 40),
child: Text(
"Kiwi",
style: TextStyle(
fontFamily:'Roboto',
fontSize: 22,
color: Colors.black,
fontWeight: FontWeight.w700),
textAlign: TextAlign.left,
),
),
],
)
)
),
const Padding(
padding: EdgeInsets.fromLTRB(200, 0, 10, 0),
child: Image(
image: AssetImage("assets/images/kiwi.png"),
),
),
],
),
],
),
),
);
}
Widget buildSearchInput() => Container(
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(14)),
child: Padding(
padding: const EdgeInsets.only(left: 20.0, right: 20),
child: Row(
children: [
Icon(
Icons.search,
size: 30,
color: Colors.grey.shade300,
),
Flexible(
child: TextField(
decoration: InputDecoration(border: InputBorder.none),
),
),
],
),
),
);
Widget buildCard()=> Container (
height: 23,
width: 90,
color: Color(0xffE6E848),
);
}
my code for bottom navigation bar
Stack(
children: [
bottomNaavigationBar:BottomNaavigationBar(
items:[
BottomNavigationBarItem(
icon:Icon(Icons.Icons.home),
backgroundColor:Colors.blue,
),
BottomNavigationBarItem(
icon:Icon(Icons.Icons.Icons.favorite),
label:'Feed'
backgroundColor:Colors.blue,
),
BottomNavigationBarItem(
icon:Icon(Icons.Icons.chat),
label:'Chat'
backgroundColor:Colors.blue,
),
BottomNavigationBarItem(
icon:Icon(Icons.Icons.person),
label:'Profile'
backgroundColor:Colors.blue,
),
),
],
),
You can add it in Scaffold, sure if you want.
Here is BottomNavBar example with PageView.
Let's define a pageController and currentTab variable for PageView inside our class,
PageController _pageController = PageController();
int _currentTab = 0;
Then we can initialize our controller for initial page.
#override
void initState() {
_pageController = PageController(initialPage: _currentTab);
super.initState();
}
Then here our view codes.
Scaffold(
drawer: const CustomDrawerWidget(),
bottomNavigationBar: BottomAppBar(
shape: const CircularNotchedRectangle(),
child: BottomNavigationBar(
currentIndex: _currentTab,
iconSize: 24,
selectedItemColor: Theme.of(context).colorScheme.primary,
type: BottomNavigationBarType.fixed,
items: _items,
onTap: (index) {
setState(() {
_pageController.jumpToPage(index);
});
},
),
),
body: PageView(
physics: const NeverScrollableScrollPhysics(),
pageSnapping: true,
controller: _pageController,
children: _pages,
onPageChanged: (page) {
setState(() {
_currentTab = page;
});
},
),
);
BottomNavigationBar items:
const _items = [
BottomNavigationBarItem(icon: Icon(Icons.home), label: "Feed"),
BottomNavigationBarItem(icon: Icon(Icons.people), label: "Chat"),
BottomNavigationBarItem(icon: Icon(Icons.person), label: "Profile"),
];
Here is page list:
const _pages= [
FeedPage(),
ChatPage(),
ProfilePage(),
];
Scaffold has a property bottomNavigationBar
Use it like so:
Scaffold(
bottomNavigationBar: BottomNavigationBar(
items: [],
),
...
)

Is it possible to change the size of the selected tab bar icon in Flutter?

I'm working on a Flutter project, and got a problem with my TabBar.
I would like to increase the size of the selected icon tabBar. Is it just possible ? I see that we can increase text size, but it doesn't work, of course, with an Icon.
Here is the code I am using :
return DefaultTabController(
length: 5,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Theme.of(context).primaryColor,
const Color.fromRGBO(0, 60, 99, 1.0)
]),
),
child: Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
backgroundColor: Colors.transparent,
shadowColor: Colors.transparent,
flexibleSpace: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: const [
TabBar(
tabs: [
Tab(
icon: Icon(
CupertinoIcons.add,
),
),
Tab(
icon: Icon(CupertinoIcons.add),
),
Tab(
icon: Icon(CupertinoIcons.add),
),
Tab(
icon: Icon(CupertinoIcons.add),
),
Tab(
icon: Icon(CupertinoIcons.add),
)
],
unselectedLabelColor: Color.fromRGBO(142, 142, 147, 1),
labelColor: Color.fromRGBO(0, 60, 255, 1),
unselectedLabelStyle: TextStyle(fontSize: 15), // Test with text
labelStyle: TextStyle(
fontSize: 20,
),
),
],
),
),
body: TabBarView(
physics: NeverScrollableScrollPhysics(),
children: [
FirstScreen(),
SecondScreen(),
ThirdScreen(),
FourthScreen(),
FifthScreen()
],
),
),
),
);
I'm really stuck, I hope there is a solution!
try this:
int _selectedTab = 0;
return DefaultTabController(
length: 5,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Theme.of(context).primaryColor,
const Color.fromRGBO(0, 60, 99, 1.0)
]),
),
child: Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
backgroundColor: Colors.transparent,
shadowColor: Colors.transparent,
flexibleSpace: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: const [
TabBar(
onTap: (index) {
_selectedTab = index;
setState((){});
},
tabs: [
Tab(
icon: Icon(
CupertinoIcons.add,
size: _selectedTab == 0 ? 30 : 18
),
),
Tab(
icon: Icon(
CupertinoIcons.add,
size: _selectedTab == 1 ? 30 : 18
),
),
Tab(
icon: Icon(
CupertinoIcons.add,
size: _selectedTab == 2 ? 30 : 18
),
),
Tab(
icon: Icon(
CupertinoIcons.add,
size: _selectedTab == 3 ? 30 : 18
),
),
Tab(
icon: Icon(
CupertinoIcons.add,
size: _selectedTab == 4 ? 30 : 18
),
)
],
unselectedLabelColor: Color.fromRGBO(142, 142, 147, 1),
labelColor: Color.fromRGBO(0, 60, 255, 1),
unselectedLabelStyle: TextStyle(fontSize: 15), // Test with text
labelStyle: TextStyle(
fontSize: 20,
),
),
],
),
),
body: TabBarView(
physics: NeverScrollableScrollPhysics(),
children: [
FirstScreen(),
SecondScreen(),
ThirdScreen(),
FourthScreen(),
FifthScreen()
],
),
),
),
);
You need a TabController and for that you should add TickerProviderStateMixin to your class. Then set controller to Tabbar and use tabController.index in a condition to size your Icon. But it won't work when you switch tabs so you should add a listener and setState.
here's the code:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:task_manager_v3/Utilities/CBase.dart';
class Test extends StatefulWidget {
const Test({Key? key}) : super(key: key);
#override
_TestState createState() => _TestState();
}
class _TestState extends State<Test> with TickerProviderStateMixin {
TabController? tabController;
#override
void initState() {
super.initState();
tabController = TabController(length: 5, vsync: this);
tabController!.addListener(() {
setState(() {});
});
}
#override
Widget build(BuildContext context) {
return DefaultTabController(
length: 5,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Theme.of(context).primaryColor,
const Color.fromRGBO(0, 60, 99, 1.0)
]),
),
child: Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
backgroundColor: Colors.transparent,
shadowColor: Colors.transparent,
flexibleSpace: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TabBar(
controller: tabController,
tabs: [
Tab(
icon: Icon(
CupertinoIcons.add,
size: tabController!.index == 0 ? 20 : 10,
),
),
Tab(
icon: Icon(
CupertinoIcons.add,
size: tabController!.index == 1 ? 20 : 10,
),
),
Tab(
icon: Icon(
CupertinoIcons.add,
size: tabController!.index == 2 ? 20 : 10,
),
),
Tab(
icon: Icon(
CupertinoIcons.add,
size: tabController!.index == 3 ? 20 : 10,
),
),
Tab(
icon: Icon(
CupertinoIcons.add,
size: tabController!.index == 4 ? 20 : 10,
),
)
],
unselectedLabelColor: Color.fromRGBO(142, 142, 147, 1),
labelColor: Color.fromRGBO(0, 60, 255, 1),
unselectedLabelStyle:
TextStyle(fontSize: 15), // Test with text
labelStyle: TextStyle(
fontSize: 20,
),
),
],
),
),
body: TabBarView(
physics: NeverScrollableScrollPhysics(),
children: [
FirstScreen(),
SecondScreen(),
ThirdScreen(),
FourthScreen(),
FifthScreen()
],
),
),
),
);
}
}

I want BottomNavigationBar as well as TabBar in flutter without having appBar

I want BottomNavigationBar as well as TabBar in flutter without having appBar, Individually both widgets are working but when combined, it is saying The following assertion was thrown during performLayout():
RenderCustomMultiChildLayoutBox object was given an infinite size during layout.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Haate Khori',
theme: ThemeData.light().copyWith(
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
int _selectedIndex = 0;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static List<Widget> _widgetOptions = <Widget>[
Donation(),
Text(
'EXPENSES',
style: optionStyle,
),
Text(
'ACTIVITIES',
style: optionStyle,
),
Text(
'GALLERY',
style: optionStyle,
),
Text(
'DONATE US',
style: optionStyle,
),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
child: Padding(
padding: const EdgeInsets.all(40.0),
child: SizedBox(
height: 70.0,
),
),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.white, Colors.teal], // whitish to gray
stops: [0.0, 0.8]),
),
),
Container(
padding: EdgeInsets.all(10.0),
child: Text(
'Title goes here',
style: TextStyle(color: Colors.white, fontSize: 25.0),
),
color: Colors.teal,
),
_widgetOptions.elementAt(_selectedIndex),
],
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
backgroundColor: Colors.teal,
),
BottomNavigationBarItem(
icon: Icon(Icons.receipt),
title: Text('Expenses'),
backgroundColor: Colors.teal,
),
BottomNavigationBarItem(
icon: Icon(Icons.fiber_smart_record),
title: Text('Activities'),
backgroundColor: Colors.teal,
),
BottomNavigationBarItem(
icon: Icon(Icons.collections),
title: Text('Gallery'),
backgroundColor: Colors.teal,
),
BottomNavigationBarItem(
icon: Icon(Icons.favorite),
title: Text('Donate us'),
backgroundColor: Colors.teal,
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.white,
unselectedItemColor: Colors.white,
onTap: _onItemTapped,
),
),
);
}
}
class Donation extends StatefulWidget {
#override
_DonationState createState() => _DonationState();
}
class _DonationState extends State<Donation>
with SingleTickerProviderStateMixin {
TabController tabController;
#override
void initState() {
// TODO: implement initState
super.initState();
tabController = TabController(length: 2, vsync: this);
}
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
backgroundColor: Colors.white,
body: Column(
children: <Widget>[
SizedBox(
height: 10.0,
),
TabBar(
labelColor: Colors.teal,
controller: tabController,
tabs: <Widget>[
Tab(
child: Text('Tab1'),
),
Tab(
child: Text('Tab2'),
)
],
),
Expanded(
child: Container(
child: TabBarView(
controller: tabController,
children: <Widget>[
Text(
'Hello',
style: TextStyle(fontSize: 20.0),
textAlign: TextAlign.center,
),
Text(
'World',
style: TextStyle(
fontSize: 20.0,
),
textAlign: TextAlign.center,
),
],
),
),
),
],
),
),
);
}
}
Target : What I want
&
This is what I am getting
I have attached a code snippet which is from my unComplete web app same can be useful for you I haven't used TabBar in the scaffold tab instead created a row inside the scaffold and used it, you can look into my code and can figure out something for you hope it helps.
import 'package:flutter/material.dart';
import 'package:transportWeb/provider/dimensions.dart';
import 'package:transportWeb/screens/mainScreenViews/aboutUs.dart';
import 'package:transportWeb/screens/mainScreenViews/contactUs.dart';
import 'package:transportWeb/screens/mainScreenViews/home.dart';
import 'package:url_launcher/url_launcher.dart';
import '../constants.dart';
class MainScreen extends StatefulWidget {
#override
_MainScreenState createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen>
with SingleTickerProviderStateMixin {
TabController _tabController;
var _currentDateTime = DateTime.now();
var _currentHours;
#override
void initState() {
super.initState();
_currentHours = _currentDateTime.hour;
print(_currentHours);
_tabController = TabController(length: 3, vsync: this, initialIndex: 1);
}
#override
void dispose() {
// TODO: implement dispose
super.dispose();
_tabController.dispose();
}
#override
Widget build(BuildContext context) {
Dimensions(context);
return Scaffold(
floatingActionButton: FloatingActionButton(
tooltip: "Request Call",
backgroundColor: Colors.deepPurpleAccent,
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(
"Enter your Phone no",
style: kh3,
),
);
},
);
},
child: Icon(
Icons.phone,
color: Colors.white,
size: 30,
),
),
body: ListView(
children: [
Container(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 0),
height: 70,
decoration: BoxDecoration(
color: Colors.transparent,
border: Border(
bottom: BorderSide(
color: Colors.grey.shade200,
width: 3,
),
),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 15),
child: RichText(
text: TextSpan(
style: kh2.copyWith(letterSpacing: 10),
children: <TextSpan>[
TextSpan(text: "JSSR "),
TextSpan(text: "Road"),
TextSpan(
text: "L",
style: kh2.copyWith(color: Colors.red),
),
TextSpan(text: "ines"),
],
),
),
),
Expanded(
child: SizedBox(
width: 0,
height: 0,
),
),
TabBar(
isScrollable: true,
indicatorColor: Colors.black,
indicatorSize: TabBarIndicatorSize.label,
labelColor: Colors.black,
unselectedLabelColor: Colors.grey.withOpacity(0.6),
controller: _tabController,
tabs: [
Tab(
child: Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Text(
"HOME",
style: kh5,
),
),
),
Tab(
child: Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Text(
"ABOUT US",
style: kh5,
),
),
),
Tab(
child: Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Text(
"CONTACT US",
style: kh5,
),
),
),
],
),
GestureDetector(
onTap: () {
launch(
"https://pub.dev/packages/url_launcher#-installing-tab-");
},
child: Container(
margin: EdgeInsets.only(left: 15, bottom: 12),
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
color: Colors.deepPurpleAccent,
),
child: Text(
"DOWNLOAD APP",
style: kh5.copyWith(color: Colors.white),
),
),
),
],
),
),
Container(
width: double.maxFinite,
height: double.maxFinite,
child: TabBarView(
controller: _tabController,
children: [
Home(),
AboutUs(),
ContactUs(),
],
),
)
],
),
);
}
}

Horizontally scrollable tabs focus on center with snap in flutter

Here I want to ask or can I make a tutorial like tabs, focusing center but the left and right tabs are 30% transparent like this, thank you!
Same can be achieved using - unselectedLabelColor: & indicatorColor: of TabBar widget.
Example Code:
#override
Widget build(BuildContext context) {
return DefaultTabController(
length: 6,
child: Scaffold(
appBar: AppBar(
centerTitle: true,
leading: Icon(Icons.person_outline),
title: Text('DASHBOARD',style: TextStyle(fontSize: 16.0),),
bottom: PreferredSize(
child: TabBar(
isScrollable: true,
unselectedLabelColor: Colors.white.withOpacity(0.3),
indicatorColor: Colors.white,
tabs: [
Tab(
child: Text('Tab 1'),
),
Tab(
child: Text('Investment'),
),
Tab(
child: Text('Your Earning'),
),
Tab(
child: Text('Current Balance'),
),
Tab(
child: Text('Tab 5'),
),
Tab(
child: Text('Tab 6'),
)
]),
preferredSize: Size.fromHeight(30.0)),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 16.0),
child: Icon(Icons.add_alert),
),
],
),
body: TabBarView(
children: <Widget>[
Container(
child: Center(
child: Text('Tab 1'),
),
),
Container(
child: Center(
child: Text('Tab 2'),
),
),
Container(
child: Center(
child: Text('Tab 3'),
),
),
Container(
child: Center(
child: Text('Tab 4'),
),
),
Container(
child: Center(
child: Text('Tab 5'),
),
),
Container(
child: Center(
child: Text('Tab 6'),
),
),
],
)),
);
}
Output:
add isScrollable: true, inside TabBar
like
TabBar(
isScrollable: true,
.
.
.
)
Screenshot:
Code:
TabBar(
isScrollable: true, // Required
unselectedLabelColor: Colors.white30, // Other tabs color
labelPadding: EdgeInsets.symmetric(horizontal: 30), // Space between tabs
indicator: UnderlineTabIndicator(
borderSide: BorderSide(color: Colors.white, width: 2), // Indicator height
insets: EdgeInsets.symmetric(horizontal: 48), // Indicator width
),
tabs: [
Tab(text: 'Total Investment'),
Tab(text: 'Your Earnings'),
Tab(text: 'Current Balance'),
],
)
Placing isScrollable: true, in the TabBar worked for me.
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
final _kTabPages = <Widget>[
Center(child: Icon(Icons.cloud, size: 64.0, color: Colors.teal)),
Center(child: Icon(Icons.alarm, size: 64.0, color: Colors.cyan)),
Center(child: Icon(Icons.forum, size: 64.0, color: Colors.blue)),
Center(child: Icon(Icons.forum, size: 64.0, color: Colors.blue)),
Center(child: Icon(Icons.forum, size: 64.0, color: Colors.blue)),
Center(child: Icon(Icons.forum, size: 64.0, color: Colors.blue)),
Center(child: Icon(Icons.forum, size: 64.0, color: Colors.blue)),
Center(child: Icon(Icons.forum, size: 64.0, color: Colors.blue)),
];
final _kTabs = <Tab>[
Tab(text: 'NK'),
Tab(text:'ActiveTools'),
Tab(text:'Coxmate'),
Tab(text:'Concept2'),
Tab(text:'Croker'),
Tab(text:'Hudson'),
Tab(text:'Swift'),
Tab(text:'Rowshop'),
];
return DefaultTabController(
length: _kTabs.length,
child: Scaffold(
appBar: AppBar(
leading:Image.asset("assets/Macarbi Logo.jpg"),//TODO image or icon here
title: Text(widget.title),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () {},//TODO do something
),
IconButton(
icon: Icon(Icons.account_circle),
onPressed: () {},//TODO do something
),
],
bottom: TabBar(
isScrollable: true,
tabs: _kTabs,
),
),
body: TabBarView(
children: _kTabPages,
),
),
);
}