how to change icon color when navigate to another page in flutter? - flutter

I made a BottomBar and make them enable to navigate to the other pages using onPressed function like below codes. but after navigating to other page that'd be can't change icon color because I think i made bottomnavigationbar: BottomBar in other page so that the page build BottomBar newly and 'int current Tab=0' is build again..
how can I change the way to can be change icon's color?
--BottomBar--
class BottomBar extends StatefulWidget {
const BottomBar({Key? key}) : super(key: key);
#override
State<BottomBar> createState() => BottomBarState();
}
class BottomBarState extends State<BottomBar> {
int currentTab = 0;
PageStorageBucket bucket = PageStorageBucket();
Widget currentScreen = HomePage();
final List<Widget> screens = [
HomePage(),
ShopPage(),
PeoplePage(),
WalletPage()
];
#override
Widget build(BuildContext context) {
return BottomAppBar(
shape: CircularNotchedRectangle(),
notchMargin: 10,
child: Container(
height: 60,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MaterialButton(
minWidth: 40,
onPressed: (){
setState(() {
currentScreen = HomePage();
currentTab = 0;
});
Navigator.push(
context, MaterialPageRoute(builder: (context)=> HomePage())
);
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.home,
color: currentTab == 0? Colors.blue : Colors.grey,
),
],
),
),
MaterialButton(
minWidth: 40,
onPressed: (){
setState(() {
currentScreen = ShopPage();
});
Navigator.push(
context, MaterialPageRoute(builder: (context)=> const ShopPage())
);
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.shop,
color: currentTab == 1? Colors.blue : Colors.grey,
),
],
),
),
],
),
//Right Tab Bar Icons
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MaterialButton(
minWidth: 40,
onPressed: (){
setState(() {
currentScreen = WalletPage();
currentTab = 2;
});
Navigator.push(
context, MaterialPageRoute(builder: (context)=> const WalletPage())
);
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.wallet,
color: currentTab == 2? Colors.blue : Colors.grey,
),
],
),
),
MaterialButton(
minWidth: 40,
onPressed: (){
setState(() {
currentScreen = PeoplePage();
currentTab = 3;
});
Navigator.push(
context, MaterialPageRoute(builder: (context)=> PeoplePage())
);
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.people,
color: currentTab == 3? Colors.blue : Colors.grey,
),
],
),
),
],
),
],
),
),
);
}
}
--the way I used BottomBar in the other page--
bottomNavigationBar: BottomBar(),

Consider trying this :
return SafeArea(
child: Scaffold(
extendBody: true,
body:pageOPtions[selectedPage]
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
backgroundColor: const Color(0xff282828),
currentIndex: selectedPage, //let's say it's 0
selectedLabelStyle: cardSecondaryTextStyle,
unselectedLabelStyle: cardSecondaryTextStyle,
selectedItemColor: const Color(0xffc8332c),
unselectedItemColor: const Color(0xff707070),
selectedIconTheme: const IconThemeData(color: Color(0xffc8332c)),
onTap: (index) {
setState(() {
selectedPage = index;
});
},
items: [
const BottomNavigationBarItem(
icon: Icon(
Icons.person,
color: Color(0xff707070),
size: 16,
),
activeIcon: Icon(
Icons.person,
color: Color(0xffc8332c),
size: 22,
),
label: 'Profile',
),
const BottomNavigationBarItem(
icon: Icon(
Icons.history,
color: Color(0xff707070),
size: 16,
),
activeIcon: Icon(
Icons.history,
color: Color(0xffc8332c),
size: 22,
),
label: 'Orders',
),
where pageOptions can be your screens like :
final pageOptions = [
const GeneralSettingsForm(),
const OrdersAndDelivery(),
];

Related

Null Check Operator Used On a Null Value? GetStorage in flutter give me this error

I am using GetStorage package in Flutter to check if the current user is login or not but it gives the error.
For example, when a user does not login it shows the null error and when I login it then it is filn, check if you can.
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:mobihub_2/Screens/main_login.dart';
import 'package:mobihub_2/Screens/navigationscreens.dart/add.dart';
import 'navigationscreens.dart/adds.dart';
import 'navigationscreens.dart/chat.dart';
import 'navigationscreens.dart/home2.dart';
import 'navigationscreens.dart/person.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
#override
// ignore: library_private_types_in_public_api
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
final auth = FirebaseAuth.instance;
int currentTab = 0;
final List<Widget> screens = const [
Home2(),
Chat(),
Youradds(),
Account(),
];
final PageStorageBucket bucket = PageStorageBucket();
Widget currentScreen = const Home2();
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey.shade100,
body: PageStorage(
bucket: bucket,
child: currentScreen,
),
floatingActionButton: FloatingActionButton(
backgroundColor: const Color(0xFFFFDC3D),
elevation: 2.5,
onPressed: () {
if(FirebaseAuth.instance.currentUser != null){
Navigator.push(context,
MaterialPageRoute(builder: (context) => const AddItems()));
}else{
Navigator.push(context,
MaterialPageRoute(builder: (context) => MainLogin()));
}
},
child: const Icon(
Icons.add,
color: Colors.black,
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: BottomAppBar(
color: Colors.white,
shape: const CircularNotchedRectangle(),
notchMargin: 10,
child: SizedBox(
height: 60,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MaterialButton(
highlightElevation: 0.0,
highlightColor: Colors.white,
hoverColor: Colors.white,
hoverElevation: 0.0,
minWidth: 40,
onPressed: () {
setState(() {
currentScreen = const Home2();
currentTab = 0;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.home,
color: currentTab == 0
? const Color(0xFFFFDC3D)
: Colors.black,
size: 25,
),
Text(
'',
style: TextStyle(
color: currentTab == 0
? const Color(0xFFFFDC3D)
: Colors.white),
)
],
),
),
MaterialButton(
highlightElevation: 0.0,
highlightColor: Colors.white,
hoverColor: Colors.white,
hoverElevation: 0.0,
minWidth: 40,
onPressed: () {
setState(() {
if(FirebaseAuth.instance.currentUser != null){
currentScreen = const Chat();
currentTab = 1;
}else{
Navigator.push(context,
MaterialPageRoute(builder: (context) => MainLogin()));
}
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.chat,
color: currentTab == 1
? const Color(0xFFFFDC3D)
: Colors.black,
size: 25,
),
Text(
'',
style: TextStyle(
color: currentTab == 1
? const Color(0xFFFFDC3D)
: Colors.white),
)
],
),
)
],
),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MaterialButton(
highlightElevation: 0.0,
highlightColor: Colors.white,
hoverColor: Colors.white,
hoverElevation: 0.0,
minWidth: 40,
onPressed: () {
setState(() {
if(FirebaseAuth.instance.currentUser != null){
currentScreen = const Youradds();
currentTab = 2;
}else{
Navigator.push(context,
MaterialPageRoute(builder: (context) => MainLogin()));
}
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.favorite,
color: currentTab == 2
? const Color(0xFFFFDC3D)
: Colors.black,
size: 25,
),
Text(
'',
style: TextStyle(
color: currentTab == 2
? const Color(0xFFFFDC3D)
: Colors.white),
)
],
),
),
MaterialButton(
highlightElevation: 0.0,
highlightColor: Colors.white,
hoverColor: Colors.white,
hoverElevation: 0.0,
autofocus: false,
minWidth: 40,
onPressed: () {
setState(() {
if(FirebaseAuth.instance.currentUser != null){
currentScreen = Account();
currentTab = 3;
}else{
Navigator.push(context,
MaterialPageRoute(builder: (context) => MainLogin()));
}
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.person,
color: currentTab == 3
? const Color(0xFFFFDC3D)
: Colors.black,
size: 25,
),
Text(
'',
style: TextStyle(
color: currentTab == 3
? const Color(0xFFFFDC3D)
: Colors.white),
)
],
),
)
],
)
],
),
),
),
);
}
}
I need help if anyone expert in Flutter checks my code and gives me the right answer. Thank you in advance.

how to apply setstate in flutter?

I'm making bottomNavigationBar and I want to make theirs buttons changed color when it's clicked. So I made a state that is 'currentTab' and I used it in each buttons's setState(). but It's not working and there's no specific error. Could you review my code below?
class BottomBar extends StatefulWidget {
#override
State<BottomBar> createState() => BottomBarState();
}
class BottomBarState extends State<BottomBar> {
int currentTab = 0;
PageStorageBucket bucket = PageStorageBucket();
Widget currentScreen = HomePage();
final List<Widget> screens = [
HomePage(),
ShopPage(),
PeoplePage(),
NftPage()
];
#override
Widget build(BuildContext context) {
return BottomAppBar(
shape: CircularNotchedRectangle(),
notchMargin: 10,
child: Container(
height: 60,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MaterialButton(
minWidth: 40,
onPressed: (){
setState(() {
currentScreen = HomePage();
currentTab = 0;
});
Navigator.push(
context, MaterialPageRoute(builder: (context)=> HomePage())
);
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.home,
color: currentTab == 0? Colors.blue : Colors.grey,
),
],
),
),
MaterialButton(
minWidth: 40,
onPressed: (){
setState(() {
currentScreen = ShopPage();
currentTab = 1;
});
Navigator.push(
context, MaterialPageRoute(builder: (context)=> ShopPage())
);
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.shopping_bag_outlined,
color: currentTab == 1? Colors.blue : Colors.grey,
),
],
),
),
],
),
//Right Tab Bar Icons
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MaterialButton(
minWidth: 40,
onPressed: (){
setState(() {
currentScreen = NftPage();
currentTab = 2;
});
Navigator.push(
context, MaterialPageRoute(builder: (context)=> const NftPage())
);
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.account_balance_wallet,
color: currentTab == 2? Colors.blue : Colors.grey,
),
],
),
),
MaterialButton(
minWidth: 40,
onPressed: (){
setState(() {
currentScreen = PeoplePage();
currentTab = 3;
});
Navigator.push(
context, MaterialPageRoute(builder: (context)=> PeoplePage())
);
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.people,
color: currentTab == 3? Colors.blue : Colors.grey,
),
],
),
),
],
),
],
),
),
);
}
}
In Rows, there are MaterialButtons and I applied setstate in there.
Because when it try to rebuild view you navigate to other screen, try this:
onPressed: (){
setState(() {
currentScreen = PeoplePage();
currentTab = 3;
});
WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.push(
context, MaterialPageRoute(builder: (context)=> PeoplePage())
);
});
},
do this in all yours onPressed.
You are using setState in the onPressed of the MaterialButton.
You should use setState in the build method of the widget.
For example:
class BottomBar extends StatefulWidget {
#override
State<BottomBar> createState() => BottomBarState();
}
class BottomBarState extends State<BottomBar> {
int currentTab = 0;
PageStorageBucket bucket = PageStorageBucket();
Widget currentScreen = HomePage();
final List<Widget> screens = [
HomePage(),
ShopPage(),
PeoplePage(),
NftPage()
];
#override
Widget build(BuildContext context) {
return BottomAppBar(
shape: CircularNotchedRectangle(),
notchMargin: 10,
child: Container(
height: 60,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MaterialButton(
minWidth: 40,
onPressed: (){
Navigator.push(
context, MaterialPageRoute(builder: (context)=> HomePage())
);
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.home,
color: currentTab == 0? Colors.blue : Colors.grey,
),
],
),
),
MaterialButton(
minWidth: 40,
onPressed: (){
Navigator.push(
context, MaterialPageRoute(builder: (context)=> ShopPage())
);
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.shopping_bag_outlined,
color: currentTab == 1? Colors.blue : Colors.grey,
),
],
),
),
],
),
//Right Tab Bar Icons
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MaterialButton(
minWidth: 40,
onPressed: (){
Navigator.push(
context, MaterialPageRoute(builder: (context)=> const NftPage())
);
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.account_balance_wallet,
color: currentTab == 2? Colors.blue : Colors.grey,
),
],
),
),
MaterialButton(
minWidth: 40,
onPressed: (){
Navigator.push(
context, MaterialPageRoute(builder: (context)=> PeoplePage())
);
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.people,
color: currentTab == 3? Colors.blue : Colors.grey,
),
],
),
),
],
),
],
),
),
);
}
}
In Rows, there are MaterialButtons and I applied setstate in there.

how to solve that buttom navigation bar doesn't appear in flutter

First,, Thanks for answering of questions about flutter in everytime.
As explaining the way this time, I tried to make that seperating button navigation bar as a widget in another file from main file and try to use it in main file like below. but it doesn't appear and not specific error.. so I don't get why is it not apper and how to use this in the way that I tried to? Could you give me some advice about this? Thanks a lot.
--main page I used--
return Scaffold(
bottomNavigationBar: BottomBar(),
--bottomNavigationBar widget page--
class BottomBar extends StatefulWidget {
const BottomBar({Key? key}) : super(key: key);
#override
State<BottomBar> createState() => _BottomBarState();
}
class _BottomBarState extends State<BottomBar> {
int currentTab = 0;
final List<Widget> screens = [
HomePage(),
ShopPage(),
PeoplePage(),
WalletPage()
];
final PageStorageBucket bucket = PageStorageBucket();
Widget currentScreen = HomePage();
#override
Widget build(BuildContext context) {
return Scaffold(
body: PageStorage(
child: currentScreen,
bucket: bucket,
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: (){},
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: BottomAppBar(
shape: CircularNotchedRectangle(),
notchMargin: 10,
child: Container(
height: 60,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MaterialButton(
minWidth: 40,
onPressed: (){
setState(() {
currentScreen = HomePage();
currentTab = 0;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.home,
color: currentTab == 0? Colors.blue : Colors.grey,
),
Text(
'Home',
style: TextStyle(
color: currentTab == 0? Colors.blue : Colors.grey,
),
)
],
),
),
MaterialButton(
minWidth: 40,
onPressed: (){
setState(() {
currentScreen = ShopPage();
currentTab = 1;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.shop,
color: currentTab == 1? Colors.blue : Colors.grey,
),
Text(
'Shop',
style: TextStyle(
color: currentTab == 1? Colors.blue : Colors.grey,
),
)
],
),
),
],
),
//Right Tab Bar Icons
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MaterialButton(
minWidth: 40,
onPressed: (){
setState(() {
currentScreen = WalletPage();
currentTab = 2;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.wallet,
color: currentTab == 2? Colors.blue : Colors.grey,
),
Text(
'Home',
style: TextStyle(
color: currentTab == 3? Colors.blue : Colors.grey,
),
)
],
),
),
MaterialButton(
minWidth: 40,
onPressed: (){
setState(() {
currentScreen = PeoplePage();
currentTab = 3;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.people,
color: currentTab == 3? Colors.blue : Colors.grey,
),
Text(
'Shop',
style: TextStyle(
color: currentTab == 3? Colors.blue : Colors.grey,
),
)
],
),
),
],
),
],
),
),
),
);
}
}
It is likely due to the fact that your BottomBar class' build method contains a Scaffold and it should not. It should only return a BottomAppBar widget. You can move all of your FAB widget code to the outer scaffold in your Main Page class.

Getx in not updating value in navigation rails flutter

I am using Obx in the navigation rails and updating the text widget. When I tap on the filter by source widget and select some values it does not update the length/count but when I tap on the filter by country widget the filter by a source shows the count. Same for the widget filter by country. It does not update the value in real-time. This is my rails widget
NavigationRail(
selectedIndex: homeController.rails.value,
onDestinationSelected: (int index) {
homeController.rails.value = index;
},
labelType: NavigationRailLabelType.all,
destinations: <NavigationRailDestination>[
const NavigationRailDestination(
icon: Icon(Icons.list_alt_sharp),
selectedIcon: Icon(Icons.list_alt),
label: Text('Layouts'),
),
const NavigationRailDestination(
icon: Icon(Icons.language_outlined),
selectedIcon: Icon(Icons.language),
label: Text('Filter by language'),
),
NavigationRailDestination(
icon: const Icon(Icons.source_outlined),
selectedIcon: const Icon(Icons.source),
label: Obx(() => Text(
'Filter by source ${homeController.filterSources.value.length}'))),
NavigationRailDestination(
icon: const Icon(FontAwesomeIcons.earthAsia),
selectedIcon:
const FaIcon(FontAwesomeIcons.earthAsia),
label: Obx(
() => Text(
'Filter by country ${homeController.filterCountry.value.length}'),
)),
],
),
This is my get controller
class HomeController extends GetxController {
var filterCountry = [].obs;
var filterSources = [].obs;
void filterCountryFunc(String e) {
if (filterCountry.value.contains(e)) {
filterCountry.value.remove(e);
} else {
filterCountry.value.add(e);
}
update();
}
void filterSourcesFunc(String e) {
if (filterSources.value.contains(e)) {
filterSources.value.remove(e);
} else {
filterSources.value.add(e);
}
update();
}
}
This is my complete dart stateful class
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get/get.dart';
import 'package:newsconnects/constants.dart';
import 'package:newsconnects/controllers/home_controller.dart';
import 'package:newsconnects/controllers/sources_controller.dart';
import 'package:newsconnects/translations/helper.dart';
import 'package:newsconnects/views/widgets/custom_checkBoxTile.dart';
import 'news_view.dart';
class HomeView extends StatefulWidget {
HomeView({Key? key}) : super(key: key);
#override
State<HomeView> createState() => _HomeViewState();
}
class _HomeViewState extends State<HomeView>
with SingleTickerProviderStateMixin {
late TabController controller;
late final List<String> _syncChannels = [];
HomeController homeController = Get.put(HomeController());
SourcesController sourcesController = Get.find<SourcesController>();
final List _screens = <Widget>[
const NewsView(),
Center(
child: Text(t.getText('notifications')),
),
const Center(
child: Text('Politics'),
),
];
#override
void initState() {
controller = TabController(length: 3, vsync: this);
super.initState();
print('Home');
_resources();
}
#override
void dispose() {
controller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
toolbarHeight: 75.0,
leadingWidth: 90,
leading: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
GestureDetector(
onTap: () {},
child: const Icon(
Icons.refresh,
size: 30.0,
),
),
InkWell(
onTap: () => _showModelSheet(context),
child: const Icon(
Icons.add_box_sharp,
size: 30.0,
),
),
const Icon(
Icons.search,
size: 30.0,
),
]),
centerTitle: true,
title: const Center(
child: Text('NewsConnect'),
),
actions: [
const Center(
child: Text(
'Options',
style: TextStyle(fontSize: 14),
),
),
IconButton(onPressed: () {}, icon: const Icon(Icons.more_vert))
],
bottom: PreferredSize(
preferredSize: const Size.fromHeight(30.0),
child: TabBar(
controller: controller,
isScrollable: true,
unselectedLabelColor: Colors.white.withOpacity(0.3),
indicatorColor: Colors.white,
tabs: const [
Tab(
child: Text('All News'),
),
Tab(
child: Text('Notifications'),
),
Tab(
child: Text('Politics'),
),
]),
),
),
body: TabBarView(
controller: controller,
children: <Widget>[..._screens],
),
);
}
_showModelSheet(BuildContext context) {
showModalBottomSheet(
context: context,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(25.0)),
),
builder: (context) {
return SizedBox(
height: 400.0,
child: Container(
padding: const EdgeInsets.all(16),
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10.0),
topRight: Radius.circular(10.0))),
child: Obx(
() => Column(
children: [
Expanded(
flex: 2,
child: Row(
children: <Widget>[
NavigationRail(
selectedIndex: homeController.rails.value,
onDestinationSelected: (int index) {
homeController.rails.value = index;
},
labelType: NavigationRailLabelType.all,
destinations: <NavigationRailDestination>[
const NavigationRailDestination(
icon: Icon(Icons.list_alt_sharp),
selectedIcon: Icon(Icons.list_alt),
label: Text('Layouts'),
),
const NavigationRailDestination(
icon: Icon(Icons.language_outlined),
selectedIcon: Icon(Icons.language),
label: Text('Filter by language'),
),
NavigationRailDestination(
icon: const Icon(Icons.source_outlined),
selectedIcon: const Icon(Icons.source),
label: Obx(() => Text(
'Filter by source ${homeController.filterSources.value.length}'))),
NavigationRailDestination(
icon: const Icon(FontAwesomeIcons.earthAsia),
selectedIcon:
const FaIcon(FontAwesomeIcons.earthAsia),
label: Obx(
() => Text(
'Filter by country ${homeController.filterCountry.value.length}'),
)),
],
),
const VerticalDivider(thickness: 1, width: 1),
// This is the main content.
Expanded(
child: IndexedStack(
index: homeController.rails.value,
children: [
_layouts(),
_language(),
_sources(),
_country(),
],
))
],
),
),
const Divider(thickness: 1, height: 1),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
TextButton(
onPressed: () {
homeController.filterSources.value.clear();
homeController.filterCountry.value.clear();
},
child: const Text('CLEAR All')),
ElevatedButton(
onPressed: () {}, child: const Text('APPLY'))
],
)
],
),
),
),
);
});
}
Widget _myRadioButton({required String title, required int index}) {
return GetBuilder<HomeController>(
builder: (_) => RadioListTile(
value: homeController.layout[index],
groupValue: homeController.isGrid.value,
onChanged: (newValue) =>
homeController.onClickRadioButton(int.parse(newValue.toString())),
title: Text(title),
activeColor: Consts.appMainColor,
),
);
}
_layouts() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_myRadioButton(
title: "List View",
index: 0,
),
_myRadioButton(
title: "Grid View",
index: 1,
),
],
);
}
_language() {
return Obx(
() => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CustomCheckBoxListTile(
value: homeController.isEnglish.value,
title: const Text('English'),
onTap: (newVal) {
homeController.isEnglish.value = newVal ?? false;
}),
CustomCheckBoxListTile(
value: homeController.isArabic.value,
title: const Text('Arabic'),
onTap: (newVal) {
homeController.isArabic.value = newVal ?? false;
}),
],
),
);
}
_sources() {
return GetBuilder<HomeController>(builder: (_) {
return ListView(
children: _syncChannels
.map((e) => CustomCheckBoxListTile(
value: homeController.filterSources.value.contains(e),
title: Text(e),
onTap: (newVal) {
homeController.filterSourcesFunc(e);
}))
.toList());
});
}
_country() {
return GetBuilder<HomeController>(
builder: (_) => ListView(
children: sourcesController.mapSelectedChannelCheckList.keys
.map((e) => CustomCheckBoxListTile(
value: homeController.filterCountry.value.contains(e),
title: Text(e.toString()),
onTap: (newVal) {
homeController.filterCountryFunc(e);
}))
.toList()));
}
void _resources() {
List.generate(
sourcesController.mapSelectedChannelCheckList.values.length,
(index0) => List.generate(
sourcesController.mapSelectedChannelCheckList.values
.elementAt(index0)
.length,
(index1) => List.generate(
sourcesController.mapSelectedChannelCheckList.values
.elementAt(index0)
.elementAt(index1)
.values
.length,
(index2) => List.generate(
sourcesController.mapSelectedChannelCheckList.values
.elementAt(index0)
.elementAt(index1)
.values
.elementAt(index2)['channels']
.length,
(index3) => _syncChannels.add(sourcesController
.mapSelectedChannelCheckList.values
.elementAt(index0)
.elementAt(index1)
.values
.elementAt(index2)['channels'][index3]
.keys
.toString())))));
}
}

How to make a Persistent BottomAppBar?

I made a BottomAppbar and a FAB at the center of a screen. The BottomAppbar is working fine, moving by indexes but now I want to make it persistent across all screens whenever I navigate.
Here is the current code:
class BottomNavBar extends StatefulWidget {
#override
_BottomNavBarState createState() => _BottomNavBarState();
}
class _BottomNavBarState extends State<BottomNavBar> {
int currentTab = 0;
final List<Widget> screens = [
MenuPage(),
OffersPage(),
ProfilePage(),
MorePage(),
HomePage(),
];
final PageStorageBucket buckets = PageStorageBucket();
Widget currentScreen = HomePage();
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
bottomNavigationBar: BottomAppBar(
shape: const CircularNotchedRectangle(),
notchMargin: 20,
child: SizedBox(
height: 80,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MaterialButton(
minWidth: 40,
onPressed: () {
setState(() {
currentScreen = MenuPage();
currentTab = 4;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
'assets/images/menu.png',
color: currentTab == 4
? kPrimaryColor
: Colors.grey.withOpacity(0.5),
),
SizedBox(height: 0.2 * kDefaultPadding),
Text(
'Menu',
style: Theme.of(context).textTheme.caption!.copyWith(
color: currentTab == 4
? kPrimaryColor
: Colors.grey.withOpacity(0.5),
),
),
],
),
),
MaterialButton(
minWidth: 40,
onPressed: () {
setState(() {
currentScreen = OffersPage();
currentTab = 1;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
'assets/images/offers.png',
color: currentTab == 1
? kPrimaryColor
: Colors.grey.withOpacity(0.5),
),
SizedBox(height: 0.2 * kDefaultPadding),
Text(
'Offers',
style: Theme.of(context).textTheme.caption!.copyWith(
color: currentTab == 1
? kPrimaryColor
: Colors.grey.withOpacity(0.5),
),
)
],
),
),
],
),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MaterialButton(
minWidth: 40,
onPressed: () {
setState(() {
currentScreen = ProfilePage();
currentTab = 2;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
'assets/images/profile.png',
color: currentTab == 2
? kPrimaryColor
: Colors.grey.withOpacity(0.5),
),
Text(
'Profile',
style: Theme.of(context).textTheme.caption!.copyWith(
color: currentTab == 2
? kPrimaryColor
: Colors.grey.withOpacity(0.5),
),
),
],
),
),
MaterialButton(
minWidth: 40,
onPressed: () {
setState(() {
currentScreen = MorePage();
currentTab = 3;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
'assets/images/more.png',
color: currentTab == 3
? kPrimaryColor
: Colors.grey.withOpacity(0.5),
),
Text(
'Menu',
style: Theme.of(context).textTheme.caption!.copyWith(
color: currentTab == 3
? kPrimaryColor
: Colors.grey.withOpacity(0.5),
),
)
],
),
),
],
),
],
),
),
),
floatingActionButton: FloatingActionButton(
backgroundColor:
currentTab == 0 ? kPrimaryColor : Colors.grey.withOpacity(0.5),
onPressed: () {
setState(() {
currentScreen = HomePage();
currentTab = 0;
});
},
child: const Icon(
Icons.home,
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
body: PageStorage(
bucket: buckets,
child: currentScreen,
),
);
}
}