I use PageView in my screen to Navigate to another screen. I have 3 screen Home
, History
and History Detail
.
Everything is fine i can move to history
screen. Until i redirect to History Detail
from History
Screen and i back with Navigator.of(context).pop
, I redirect to Home
Screen. I want after Back from History Detail
i keep stay in History
Screen.
I already initialize initialPage is 0 , and change the initialPage with onPageChanged
but nothing happens, i still redirect to Home
Screen.
How can i do for my case?
PageController _pageController;
int currentPage = 0;
@override
void initState() {
_pageController = PageController(initialPage: currentPage);
super.initState();
}
Widget build(BuildContext context) {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
double mqHeight = MediaQuery.of(context).size.height;
return Scaffold(
key: _scaffoldKey,
body: FutureBuilder(
future: Hive.openBox("debt_box"),
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError)
return Text(snapshot.error.toString());
else
return Scaffold(
body: PageView(
scrollDirection: Axis.vertical,
onPageChanged: (index) {
currentPage = index;
print(currentPage);
},
physics: NeverScrollableScrollPhysics(),
controller: _pageController,
children: <Widget>[
CustomScrollView(
slivers: <Widget>[
SliverAppBarHome(),
SliverListHome(_scaffoldKey),
],
),
HistoryDebt(),
],
),
);
} else {
return Center(child: CircularProgressIndicator());
}
},
),
bottomNavigationBar: BottomAppBar(
shape: CircularNotchedRectangle(),
child: Container(
height: mqHeight * 0.08,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(
icon: Icon(Icons.home),
onPressed: () =>
functionHelper.previousButtonPageView(_pageController),
),
IconButton(
icon: Icon(Icons.insert_chart),
onPressed: () =>
functionHelper.nextButtonPageView(_pageController),
),
],
),
),
),
}
I found the issue.
When you press back button it re-builds your main widget. In the build
method you have FutureBuilder
which also gets rebuild.
So it will show you CircularProgressIndicator
. Since Hive.openBox("debt_box")
gives the result instantaneously, you are not able to see it.
While showing CircularProgressIndicator
your PageView
got deactivated (removed from widget tree). So _pageController
loses its client.
After the CircularProgressIndicator
, a new PageView
gets created.
That is why it showed Home
page
Solution:
As per the document we should not directly assign future: Hive.openBox("debt_box")
during the build. Instead create a Future
variable and initialize it in the initState
, then pass that variable to future
property of FutureBuilder
PageController _pageController;
int currentPage = 0;
Future _openBox;
@override
void initState() {
_pageController = PageController(initialPage: currentPage);
_openBox = Hive.openBox("debt_box"); //initialize here
super.initState();
}
Widget build(BuildContext context) {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
double mqHeight = MediaQuery.of(context).size.height;
return Scaffold(
key: _scaffoldKey,
body: FutureBuilder(
future: _openBox, //pass the future variable here
builder: (ctx, snapshot) {
...
Try/Route with this on Your History page
Navigator.push(context,
MaterialPageRoute(builder: (context) => YourDestination()));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.