Help I'm a flutter newbie and I've built a file that has OnTap TapGestureRecognizer functions, which I'm trying to manage the pageView within a second page. I've tried a few different options and but I can't work out how to get pageController to change the page view on the second page.
Root Class
class RootApp extends StatefulWidget {
@override
RootAppState createState() => RootAppState();
}
class RootAppState extends State<RootApp> {
bool isFavorite = false;
int pageIndex = 0;
PageController _pgController = PageController();
void pgCont(pg){
_pgController.animateToPage(pg, duration: Duration(milliseconds: 250), curve: Curves.bounceInOut);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageView(controller: _pgController,
children: <Widget>[
getBody1() ,
getBody2(),
]),
bottomNavigationBar: getFooter(),
);
}
Header Home Class
class HeaderHomePage extends StatelessWidget {
HeaderHomePage({
Key ?key
}) : super(key: key);
RootAppState rooting = RootAppState();
}
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text.rich(
TextSpan(
text: "Body1",
recognizer: TapGestureRecognizer()
..onTap = (){
print('He Clicked Body1');
rooting.pgCont(1);
},
SizedBox(
width: 8,
),
Text.rich(
TextSpan(
text: "Body2",
recognizer: TapGestureRecognizer()
..onTap = (){
print('She clicked Body2');
rooting.pgCont(2);
},),
style: TextStyle(
color: white, fontSize: 17, fontWeight: FontWeight.w500),
)
],
);
Please help I'm stuck on something that seems so simple what I'm I missing or doing wrong here?
In
RootAppStateclassIn
HeaderHomeclassIt's not wise to call State of another Widget from another widget. You should use callback to deal with code from other State class.
https://www.geeksforgeeks.org/flutter-working-with-callback-functions/