hiding Tabbar during navigation - flutter

My main screen has got AppBar and BottomNavigationBar. if I navigate to screen 2 BottomNavigationBar is staying. It is perfect for me.
But I would like to remove the Tabbar for screen 2. Because the content of Screen 2 is irrelevant with the TabbarMenu.
I am going to screen 2 from screen 1 return Page2 like this:
Widget showPage(int selectedPage) {
if (selectedPage == 0) {
return const NewsView(id: '1');
} else if (selectedPage == 1) {
return const Settings();
any help?

Related

How to get the autorotate setting status with Flutter?

Would like get current status of 'autorotate' system setting in Flutter application. Need to identify weather it is ON or OFF.
This can be done in android with the answer mentioned in the stackoverflow question.
Android auto rotate on off status link. But need the same with Flutter which should work for both android and ios.
Apricates your suggestion.
To know the Orientation of the screen, you can use the OrientationBuilder Widget, it will determine the current Orientation and rebuild when the Orientation changes its state.
#override
Widget build(BuildContext context) {
return Scaffold(
body: OrientationBuilder(builder: (_, orientation) {
if (orientation == Orientation.portrait)
return Something(); // if orientation is portrait, do something
else
return Something(); // else do something
}),
);
}

Flutter - How will you check from which screen you have clicked bottom navigation tab

I am trying to find how can I know from which screen user has clicked the bottom nav screen. Basically I have to send event from which screen user clicked on botton nav. I have 4 bottom nav.
There is no direct way to check a previously selected item in the bottom bar what you can do you can make use of
onTap: (index) => changeTab(index)
It gives an index of the item user taps, Save index to some variable, and next time when use tap on the item you can consider previously saved value as an item from which the user is tapping.
You can check the last selected index in bottom navigation bar :
static int currentTab = 0;
static int lastIndex = 0;
When you implement the onPressed or onTap method on bottom bar:
onTap: (){
setState(() {
currentTab = index;
lastIndex = currentTab; // It keeps the last selected index.
}
});
}
And then u can call
currentTab = lastIndex.

How to preserve selections of user during screen transitions in Flutter

I have a bottom navigation bar which includes home button, screen1 button and screen2 button. User is making some selections on home screen.
After that he taps the screen1 button on bottom navigation bar and goes to screen 1.
Then in order to return home screen, he taps home button. But home screen rebuild and all information has gone.
I tried AutomaticKeepAliveClientMixin but it is not working. My code sample is below. Is there any mistake here?
class _HomeScreenState extends State<HomeScreen>
with AutomaticKeepAliveClientMixin {
#override
bool get wantKeepAlive => true;
#override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(

How to react to scroll down and up?

I want to know the direction of scrolling at the moment of scrolling up or down in the list view. but I couldn't find a solution.
I tried to use ScrollController to listen to the direction of scrolling for dealing with different work as up and down scrolls. But there is no way to listen to that with ScrollController.
Is there anyone to have dealt with this problem?
You can make use of NotificationListener<ScrollNotification>. You will need to wrap your scroll view in that widget and then listen to UserScrollNotifications:
NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification notification) {
if (notification is UserScrollNotification) {
if (notification.direction == ScrollDirection.forward) {
// Handle scroll down.
} else if (notification.direction == ScrollDirection.reverse) {
// Handle scroll up.
}
}
// Returning null (or false) to
// "allow the notification to continue to be dispatched to further ancestors".
return null;
},
child: ListView(..), // Or whatever scroll view you use.
)

Dismiss keyboard on swipe back gesture in Flutter app

I am trying to dismiss the keyboard when the user swipes from the edge to pop route.
Currently the keyboard doesn't dismiss until the route is completely gone popped, messing up some of the other pages layout until it dismisses
I did try to use a WillPopScope to determine when the user was going to pop the route, but unfortunately this disables the swipe to pop functionality from iOS or the CupertinoPageRoute.
I just want to find out if there's anyway I can determine when the user swipes from the edge to pop or taps the back button on the appBar and dismiss the keyboard as they do so.
If possible, I am trying to dismiss keyboard as soon as they start swiping to pop, as it happens in many apps.
I am attaching attaching a gif showing the effect I'm trying to achieve.
As suggested by Ovidiu
class DismissKeyboardNavigationObserver extends NavigatorObserver {
#override
void didStartUserGesture(Route route, Route previousRoute) {
SystemChannels.textInput.invokeMethod('TextInput.hide');
super.didStartUserGesture(route, previousRoute);
}
}
and in your Material App
MaterialApp(
navigatorObservers: [DismissKeyboardNavigationObserver()],
)
You need to create a custom class extending NavigatorObserver, and pass an instance of it to the navigatorObservers property of your MaterialApp or CupertinoApp.
Within that custom class, you can override didStartUserGesture and didStopUserGesture, which will be called when the swipe gesture starts/ends. This should allow you to achieve the behavior you are looking for. Note that didStartUserGesture indicates the current route as well as the previous route, based on which you could add logic to determine whether the keyboard should be dismissed or not.
This should come naturally and you shouldn't be directly concerned with that because actually, when you pop a route with the keyboard on, it should dismiss properly.
However, if you want to detect when the user starts swiping and dismiss the keyboard along with it and then pop the current route, you can easily achieve it by wrapping your screen widget with a GestureDetector like so:
Widget build(BuildContext context) {
double dragStart = 0.0;
return GestureDetector(
onHorizontalDragStart: (details) => dragStart = details.globalPosition.dx,
onHorizontalDragUpdate: (details) {
final double screenWidth = MediaQuery.of(context).size.width;
// Here I considered a back swipe only when the user swipes until half of the screen width, but you can tweak it to your needs.
if (dragStart <= screenWidth * 0.05 && details.globalPosition.dx >= screenWidth) {
FocusScope.of(context).unfocus();
}
child: // Your other widgets...
},
this is something i wrote to handle this issue. doesnt use any external packages, you would just wrap your content in the main function at the top.
Widget swipeOffKeyboard(BuildContext context, {Widget? child}) {
return Listener(
onPointerMove: (PointerMoveEvent pointer) {
disKeyboard(pointer, context);
},
child: child, // your content should go here
);
}
void disKeyboard(PointerMoveEvent pointer, BuildContext context) {
double insets = MediaQuery.of(context).viewInsets.bottom;
double screenHeight = MediaQuery.of(context).size.height;
double position = pointer.position.dy;
double keyboardHeight = screenHeight - insets;
if (position > keyboardHeight && insets > 0) FocusManager.instance.primaryFocus?.unfocus();
}