How to modify parent FloatingActionButton on flutter? - flutter

I am making an app in which I have to pages, routed by a BottomNavigationBar, and each page has its own nested routes. The app contains a main FloatingActionButton centered and docked into the BottomNavigationBar. The problem is that I wanted to control what the FAB does from inside each nested route. The problem is that if I create a FAB inside each nested route the button will not be docked into the BottomNavigationBar. Bellow are some pictures. Anyone can help? Thanks in advance.
What I have :
What I wanted :

Is this what you are asking? If not please share some code which casing the problem
UPDATE
Scaffold(
appBar: AppBar(
title: Text('Test'),
),
body: _screenList[_screenIndex],
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: _screenIndex,
onTap: (index) {
print(index);
setState(() {
_screenIndex = index;
});
},
items: [
BottomNavigationBarItem(
icon: Icon(Icons.inbox),
title: Text('Screen1'),
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: Text('Screen2'),
),
]),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: FloatingActionButton(
child: Icon(
Icons.android,
),
onPressed: () {
_screenIndex == 0
? print('hello from screen1')
: print('hello from screen2');
},
),
);
OUTPUT

Related

Floating action button not moving up with snackbar flutter

I am struggling making a FAB move up when a snackbar is showing.
Here is my set up.
I have my main page with a bottom navigation bar with 3 items.
The first item is basically a widget with a ListView and a floating action button.
Once you click an item of the list view, you navigate to a details page. If you then delete the item from the details page, you come back to the main page.
A snackbar then appears as a result from deleting the item.
The problem I have is that the snack bar is not moving up and hides the FAB.
It assumes it has something to do with context, but I don't know how.
If I move the FAB to the main page, the FAB goes up but it then hides last item of the listview.
Page with list view and FAB:
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(
title,
style: Theme.of(context).textTheme.titleMedium,
),
),
body: WidgetWithListView(),
floatingActionButton: FloatingActionButton.extended(
onPressed: () {
//do something
},
label: Text('label'),
icon: const Icon(Icons.add),
));
}
Main page:
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(appBarTitle,
style: Theme.of(context)
.textTheme
.titleLarge!
.copyWith(fontWeight: FontWeight.bold)),
actions: <Widget>[
IconButton(
icon: const Icon(some icon),
tooltip: 'some tooltip',
onPressed: () {
//do something;
},
),
]),
body: Center(
child: getBody(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: const Icon(someIcon),
label: 'a label'y,
),
BottomNavigationBarItem(
icon: const Icon(someIcon),
label: 'a label',
),
BottomNavigationBarItem(
icon: const Icon(someIcon),
label: 'a label',
),
],
currentIndex: _selectedIndex,
type: BottomNavigationBarType.fixed,
onTap: _onItemTapped,
),
);
}
Thanks.

How to use a dynamic index for bottom navigation bar in flutter

I have built a bottom navigation bar in flutter using the following method:
Scaffold(
appBar: AppBar(
title: const Text('BottomNavigationBar Demo'),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.call),
label: 'Calls',
),
BottomNavigationBarItem(
icon: Icon(Icons.camera),
label: 'Camera',
),
BottomNavigationBarItem(
icon: Icon(Icons.chat),
label: 'Chats',
),
],
),
);
int _selectedIndex = 0; //New
BottomNavigationBar(
items: const <BottomNavigationBarItem>[
...
currentIndex: _selectedIndex, //New
onTap: _onItemTapped, //New
)
//New
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
But, my requirement is to have the _selectedIndex as a dynamic value which should be set to the value that I give dynamically in the code. The actually reason why I am trying this is to navigate back to a specific page in the bottom navigation bar. Like, for example, I am in some other page without navigation bar and from there I use this piece of code to navigate to a specific page of navigation bar by passing the index:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CameraScreen(
index : _selectedIndex,
),
));
This doesn't work, as it only moves to the page whose index I mentioned and all other onTap changes of index doesn't happen. Please help me fix this problem and the part I'm missing to understand. Thanks in advance!

Flutter, how to change current tab bar view or page or index from child

I have a BottomNavigationBar
import 'screen_one.dart' as screen_one;
import 'screen_two.dart' as screen_two;
List<Widget> _widgetOptions = <Widget>[
screen_one.ScreenOne(),
screen_two.ScreenTwo(),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
Scaffold(
bottomNavigationBar:
BottomNavigationBar(
backgroundColor: Colors.green,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
backgroundColor: Colors.green,
icon: Icon(Icons.person),
title: Text(''),
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text(''),
),
],
currentIndex: _selectedIndex,
onTap: _onItemTapped,
),
body: _widgetOptions.elementAt(_selectedIndex),
),
I want to change current tab index from children class (ScreenOne, ScreenTwo).
when a function occurs in ScreenOne, it needs to move on to ScreenTwo.
how could I acheive this?
Using callbacks,
Pass _onItemTapped as parameter to ScreenOne() : ScreenOne(_onItemTapped)
Inside ScreenOne : final Function(int) onItemTapped;
cCall onItemTapped(your_index_here)

Page started at begin when navigate between 2 screen using BottomTabNavigator

I am new at Flutter. I started a new project regarding social media. I used BottomTabNavigation for navigating the Home screen to the notification screen. Now the problem is that when I am showing feeds on the home page and I scroll down at many posts. let's say I am on post number 50. Now when I click on the notification and again click on the Home page its started from begin. I need to stay my scroll at a previous position on every main screen.
Here is my code for navigating one page to another.
class _DefaultLayoutWidgetState extends State<DefaultLayoutWidget> {
final List<Map<String, dynamic>> _pages = [
{'page': PostScreen(), 'title': 'SelfieCorner'},
{'page': NotificationScreen(), 'title': 'Notifications'}
];
int _curruntIndex = 0;
void handleTapEvent(inx) {
setState(() {
_curruntIndex = inx;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: PostAppBarWidget(),
body: _pages[_curruntIndex]['page'],
bottomNavigationBar: BottomNavigationBar(
onTap: (index) => handleTapEvent(index),
currentIndex: _curruntIndex,
items: [
BottomNavigationBarItem(
icon: Icon(
Icons.home,
),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(
Icons.notifications,
),
title: Text('Notifications'),
),
BottomNavigationBarItem(
icon: Icon(
Icons.lock,
),
title: Text('Profile'),
)
],
),
);
}
}
Consider using IndexedStack like this. It will save the state of the previous widget while moving to another.
body: IndexedStack(.....),
bottomNavigationBar: BottomNavigationBar(
onTap: (index) => handleTapEvent(index),
currentIndex: _curruntIndex,
items: [
BottomNavigationBarItem(
icon: Icon(
Icons.home,
),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(
Icons.notifications,
),
title: Text('Notifications'),
),
BottomNavigationBarItem(
icon: Icon(
Icons.lock,
),
title: Text('Profile'),
)
],
),
IndexedStack Widget Video
Tutorial on medium

How to perfectly align bottom navigation item with docked fab?

I am new in flutter. I am trying to create a main screen where there is bottom bar with floating action button(FAB). The fab is docked at the center of bottom app bar. Meanwhile, the bottom app bar has 4 bottom navigation items. Currently all the items are not perfectly aligned with each other. The search and notification icons are too close to the center. Is there a way i can arrange it to make it better and perfectly aligned? Please help. Thank you
Current ui:
The code:
import 'package:flutter/material.dart';
class Dashboard extends StatefulWidget {
_DashboardState createState() => _DashboardState();
}
class _DashboardState extends State<Dashboard> {
int _selectedPage = 0;
final _pageOptions = [
Home(),
Discover(),
Notifications(),
Messages(),
];
Widget build(BuildContext context) {
final _drawerNav = Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(color: colorPrimary),
),
ListTile(
title: Text('Item 1'),
onTap: () {},
),
Divider(),
ListTile(
title: Text('Item 2'),
onTap: () {},
),
Divider(),
],
),
);
final _bottomNav = BottomAppBar(
shape: CircularNotchedRectangle(),
notchMargin: 6,
clipBehavior: Clip.antiAlias,
child: BottomNavigationBar(
selectedItemColor: colorPrimary,
unselectedItemColor: Colors.grey,
currentIndex: _selectedPage,
onTap: (int index) {
setState(() {
_selectedPage = index;
});
},
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home), title: Container(height: 8.0)),
BottomNavigationBarItem(
icon: Icon(Icons.search), title: Container(height: 8.0)),
BottomNavigationBarItem(
icon: Icon(Icons.notifications), title: Container(height: 8.0)),
BottomNavigationBarItem(
icon: Icon(Icons.message), title: Container(height: 8.0)),
],
),
);
final _fab = FloatingActionButton(
child: Icon(Icons.add),
backgroundColor: colorPrimary,
onPressed: () {},
);
return Scaffold(
appBar: AppBar(
title: Text('Test'),
backgroundColor: colorPrimary,
),
drawer: _drawerNav,
body: _pageOptions[_selectedPage],
floatingActionButton: _fab,
bottomNavigationBar: _bottomNav,
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
);
}
}
I made a quick and dirty fix
I made the bottom navigation bar using row. Instead of using 4 children, I use 5. One of them is a dummy child
https://gist.github.com/hariangr/2739c25dda72edcbc18073b907ef057a
Try setting type in BottomNavigationBar to BottomNavigationBarType.fixed
The bottom navigation bar's type changes how its items are displayed. If not specified, then it's automatically set to BottomNavigationBarType.fixed when there are less than four items, and BottomNavigationBarType.shifting otherwise.
BottomNavigationBarType.fixed, the default when there are less than four items. The selected item is rendered with the selectedItemColor if it's non-null, otherwise the theme's ThemeData.primaryColor is used. If backgroundColor is null, The navigation bar's background color defaults to the Material background color, ThemeData.canvasColor (essentially opaque white).
BottomNavigationBarType.shifting, the default when there are four or more items. If selectedItemColor is null, all items are rendered in white. The navigation bar's background color is the same as the BottomNavigationBarItem.backgroundColor of the selected item. In this case it's assumed that each item will have a different background color and that background color will contrast well with white.