In a screen of my flutter app I have some buttons that allow me to open new pages, created with the following code:
@override
Widget build(BuildContext context) {
return ElevatedButton.icon(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Back Swipe")),
body: const Center(
child: SizedBox(),
),
);
},
),
);
},
icon: const Icon(Icons.sunny),
label: const Text("Hello"),
);
}
The problem is that by doing so, once I enter the subpage, to go back I have to click on the back button.
If instead I use the back swipe (from left or right) the app closes.
Adding the rootNavigator: true
parameter like this:
Navigator.of(context, rootNavigator: true).push(
the back swipe works and I go back, but using this parameter, the NavigationBar
disappears, and I need it to stay.
How can I solve it?
Thanks in advance
2
Answers
You can use leading and automaticallyImplyLeading properties of the AppBar:
In this way you can choose what happens when you click back button.
You Can try this package:swipeable_page_route: ^0.4.4
Orelse
In Flutter, you can implement a swipe-to-change-page functionality using the PageView widget. Here’s an example of how to achieve this:
Explanation:
The PageView widget allows horizontal swiping between pages.
The PageController is used to manage and control the pages programmatically if needed.
Each page is represented as a widget inside the children property of the PageView.
You can customize the PageView further by adding indicators, animations, or custom gestures if needed.