I am trying to write an app and I have a section to display the users profile and a login/register prompt if they aren’t signed in yet. I am using a pageView to switch between the 2 as I want to keep the profile page alive at all times so I don’t need to keep making requests for data each time I open it. The section itself is in another pageView with a map widget I made that I also always want to keep alive at all times:
import 'package:binit/auth/auth.dart';
import 'package:flutter/material.dart';
import 'package:google_nav_bar/google_nav_bar.dart';
import '../components/mapWidget.dart';
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final PageController _pageController = PageController();
int _pageInd = 0;
void _onTabChange(int index) {
setState(() {
_pageInd = index;
});
_pageController.jumpToPage(index);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageView(
physics: const NeverScrollableScrollPhysics(),
controller: _pageController,
children: <Widget>[
MapWidget(),
AuthPage(),
],
),
bottomNavigationBar: Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0),
child: GNav(
haptic: true,
rippleColor: Colors.grey[300]!,
hoverColor: Colors.grey[100]!,
activeColor: Colors.black,
duration: const Duration(milliseconds: 400),
tabBackgroundColor: Colors.green[100]!,
color: Colors.black,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
gap: 8,
tabs: const [
GButton(
icon: Icons.map,
text: "Map",
),
GButton(
icon: Icons.person,
text: "Profile",
),
],
selectedIndex: _pageInd,
onTabChange: _onTabChange,
),
),
);
}
}
My problem is that the pageView index is not updated when the users state changed, like when I delete the account from Firebase CLI, or I sign in.
I have tried to make it work with the code below, but I am not sure when the .listen() doesn’t update it.
Also on a side note, is this good practise to have pageViews like this to keep page states alive? Or are there better methods?
Thanks 🙂