How to enable disable edit mode on tabs view in Flutter - flutter

I want to enable "edit" mode for some elements in selected tab, the problem is
if im using edit btn in appBar menu i enable edit mode for all the tabs not just selected.
When user select tab and click edit from appBar i want to switch view to edit mode for that tab only ,and on save to switch back. If user quit in the middle of editing by clicking other tab or swipe, i want to show non editing tabs again.
Im calling this fro edit btn.
void _editModeCall(String u) {
setState(() {
_editMode = true;
});
}
Than i got five tabs like this:
firstTab(){
if(_editMode){
return _editmodeView();
}else{
return _defoultView();
}
}
And on save btn aim switching bool _editMode,, that is working but this way i enable "editMode" on all tabs not just selected.
How to get index or someother way to know on witch tab are user and enable edit view just for that tab?

You can use the TabController class for this. You can create a new TabBar Widget and append a TabController to it:
_tabController = new TabController(length: 3, vsync: this);
TabBar(controller: _tabController);
Then to get the current index, call _tabController.index. Then use a setState() call and change the State of only the visible TabBarView page.
Reference: https://api.flutter.dev/flutter/material/TabController-class.html

Related

Flutter - navigate back to specific tab on a page with Navigator.pop(context)

In my app, I have a homepage that has 5 tabs on the bottom. On each tabbed page, there is an app bar that has a '+' symbol as an action, which navigates you to a different page. The navigation with that '+' button to the new page is done with the following code, alongside the Flutter Platform Widgets package:
Navigator.of(context, rootNavigator: true)
.push(
platformPageRoute(
context: context,
builder: (context) => Page1(),
),
);
I use the platformPageRoute feature as an easy way to navigate with a native feel. Now, that works fine to navigate to a new page, but the issue comes when I use
Navigator.pop(context);
to navigate back to the original page. When I use that to navigate back to that original page, it pays no attention to the tab that was selected originally. For example, if I were originally on the second tab on the homepage and then use the '+' button on that tab and then finally use
Navigator.pop(context);
on that new page, it returns the first tab of the homepage. Is there any way of ensuring when I use the above command, it goes to the right tab? I have tried something along the lines of:
Navigator.popUntil(context, '/homepageTab2');
alongside a named route, to return to the correct tab on the homepage, although that returns a black screen. Why might that be? I have also tried using:
Navigator.pushAndRemoveUntil(
context,
platformPageRoute(
context: context,
builder: (context) =>
HomePage(selectedPage: 1),
),
(route) => false,
);
This does not work either, since it returns the selected/correct page tab content, but with the first tab selected. In addition, the other
'problem' for me is that the animation is a 'push' one and that doesn't 'match' with the animation when I have more often used
Navigator.pop(context);
to navigate back to a screen. Is there a way to maybe use pushAndRemoveUntil but then change the animation to match a pop animation?
Thanks!
EDIT:
I have just noticed that with the situation I have described above, it is actually returning the correct screen content when I use Navigator.pop(context); but the tab in the tab bar at the bottom is showing as the first tab, in the second tab's position, essentially duplicating the first tab, until I navigate to a new tab and back, at which time it shows the correct tab in the correct position. I hope that makes sense!
As it turns out, the issue wasn't related to Navigator.pop(context); being used. It was the way I was controlling the selected tab. I'm posting this as an answer incase it helps someone else.
Initially, I created late values for a tab controller and the current selected page, like so:
late TabController _tabController;
late ScrollController _scrollController;
late int _selectedPage;
Then, I created a list of widgets that represented the actual page to display for each selected tab:
List<Widget> _pageWidgets = <Widget>[
Page1();
Page2();
Page3();
Page4();
Page5();
];
Then (and I think this was the bit that wasn't working) I used initState() as follows:
void initState() {
super.initState();
// Initialising a value that allows the 'final' page selector to be changed
_selectedPage = widget.selectedPage;
// Initialising the tab controller
_tabController = TabController(
length: 5,
vsync: this,
initialIndex: _selectedPage,
);
// updating the tab index when a new item is selected
_tabController.addListener(() {
setState(() {
_selectedPage = _tabController.index;
//_tabIndex = _tabController.index;
});
});
// Creating the scroll controller
_scrollViewController = ScrollController();
// Scrolling view to top when a new tab is selected
_tabController.addListener(() {
setState(() {
_scrollViewController
.jumpTo(_scrollViewController.position.minScrollExtent);
});
});
}
I then controlled the page content like this:
body: _pageWidgets.elementAt(_selectedPage),
I'm not 100% sure why this wasn't working, although I believe it would have something to do with the fact that initState() would only be called during the build and therefore placing the functionality inside there would mean changes wouldn't be detected. Either way, my new method, which works perfectly, is:
/// Controls the screen to display first
int _index = 0;
/// Creating a navigation key to control tab bar navigation
final _navigationKey = GlobalKey<CurvedNavigationBarState>();
Then, within the Scaffold() I show the page content like this:
body: _pageWidgets.elementAt(_index),
And finally, within the navigation bar (which is the CurvedNavigationBar() package from pub.dev) I give it a key and the index:
key: _navigationKey,
index: _index,
And this controls it perfectly, showing the correct tab.
Sub-pages of a TabBarView cannot be navigated using Navigator.
You can use TabController to go to your desired tab page after awaiting Navigator.push():
await Navigator.of(context, rootNavigator: true)
.push(
platformPageRoute(
context: context,
builder: (context) => Page1(),
),
);
tabController.animateTo(<index of tab>);

How do I animate to another tab from a button?

My Flutter app structure is making it hard for me to make tab transitions.
Screenshot
main.dart controls the tabs on the top, and there is a separate homeTab.dart file that controls the page below the tab: Annotated
I want to make it so that when the "Discover More" button at the bottom (which is made from homeTab.dart) is tapped, it animates to the second tab "Browse".
I added this to main.dart:
TabController tabController;
#override
void initState() {
tabController = new TabController(length: 5, vsync: this, initialIndex: 0);
getEmail();
}
changeTabToBrowse(){
tabController.animateTo(1);
}
The changeTabToBrowse() function works, but I can only call it from main.dart. I tried passing the function to homeTab.dart as a parameter, but it doesn't do anything.

Flutter: CupertinoTabScaffold tab content does not refresh after closing PageRoute

Problem: CupertinoTabScaffold tab content does not refresh after closing new screen from CupertinoPageRoute
App:
There is a list of data in a Listview.
I open one data set and remove the favourite status (checked=false).
I close the data set and the listview should refresh (FutureBuilder to an SQL database).
Details:
I've got a Flutter App with two layouts (Material and Cupertino).
In each layout i have four tabs and the content widgets are the same.
On the Cupertino App every tab consists of a CupertinoPageScaffold.
When i press on an element it navigates to a new screen with CupertinoPageRoute.
After closing the CupertinoPageRoute the tab content is not refreshing.
The tab content is refreshing when i switch between the four tabs.
If one CupertinoPageRoute is open and i open a new one from here and close it again
then the content of the opened CupertinoPageScaffold is refreshing.
The data is refreshing without a callback etc., just by calling the future (i think).
Means there should be a problem with the CupertinoTabScaffold.
There is no problem on the Material App.
The tab contents of the Material App are refreshing when closing a MaterialPageRoute.
Question:
Is there a problem at this position?
Do you have the same issue or maybe a solution or workaround?
Code:
My code is not really representative so i will create a new clean project for you.
please confirm if code is necessary.
What i tried:
I tried a lot of solutions from the internet like callbacks, setState, .then, .whencomplete for the route.
nothing seems to work.
Thank you in advance.
EDIT 03.10.2021: the tabcontroller with SingleTickerProviderStateMixin reloads the open tab when i open or close an new screen. if i just use DefaultTabController Widget than it doesn't. So i have to edit the CupertinoTabController.
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
late TabController _tabController;
final CupertinoTabController _cuptabController = CupertinoTabController();
Future<void> _refreshdata() async {
downloadJSON1();
setState(() {
});
}
callback() {
_refreshdata();
}
#override
void initState() {
_refreshdata();
super.initState();
_tabController = TabController(vsync: this, length: 2, initialIndex: 0);
_cuptabController.index = widget.starttab;
}
#override
void dispose() {
_tabController.dispose();
super.dispose();
}
...
}
I can suggest a few options:
Option 1:
change your FutureBuilder into a StreamBuilder.
FutureBuilder does not refresh automatically when you save the data which StreamBuilder does very well.
Option 2:
Use a setState but not on the context of the route you are closing, but rather on the context of the original page you want to refreh
setState(() { });
for the moment i solved it with a .then-callback from PageRoute. i just passed the callback to the deepest Widget/PageRoute. But if you have a solution/idea for the tabcontroller would be great.
Souce:
how to refresh state on Navigator.Pop or Push in flutter
Navigator.push(context,
CupertinoPageRoute(
builder: (BuildContext context) => MyWidget(
'myparameters'),),).then((value) => setState(() {callback();}));

How can I go back to recent page of history with onPopPage in navigation 2.0

I'm practicing the new API, navigation 2.0, for my web project with flutter. I'm quite new to flutter either.
I successfully implemented the routing with navigation 2.0, but still can't figure out how the onPopPage works. If I tapped the button to navigate to the sub-page, the back button is automatically generated in the header. So when I clicked the back button in the header, it takes me home.
What if I implement another nested sub-page of the sub page, then I clicked the back button it takes me all the way to the top page, home.
I think it's because of the onPopPage function which I added like this. How can I edit this function to work properly. I want the back button to pop the only recent page of navigation history.
onPopPage: (route, result) {
if (!route.didPop(result)) {
return false;
}
_currentPath = 'home';
show404 = false;
notifyListeners();
return true;
},

Show pulsing cursor but do not open the keyboard when textfield is initialized

I don't want the keyboard to automatically appear when I'm pressing on the widget. However, I want to show the cursor to indicate that there's a textfield in this area. How can I achieve this?
I can only think of one workaround for this. You can add FocusNode to your field and listen to changes. When the field get focus, hide the keyboard.
final FocusNode fn = FocusNode();
fn.addListener(() {
// You can control here whether or not you want to hide automatically shown keyboard
if (fn.hasFocus) {
// Hiding the keyboard
SystemChannels.textInput.invokeMethod('TextInput.hide');
}
});
...
// Pass the focus node to text field:
TextField(
focusNode: fn
);
I haven't tested it, though.