I am still a little stuck. How do I pass the PageController between multiple screens, so I can call it's methods?
My current code:
main.dart
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'WeinCheckerApp',
home: PageViewScreen(),
);
}
}
class PageViewScreen extends StatefulWidget {
@override
_PageViewScreenState createState() => _PageViewScreenState();
}
class _PageViewScreenState extends State<PageViewScreen> {
PageController _pageController = PageController(
initialPage: 1,
);
@override
void dispose() {
_pageController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[800],
appBar: AppBar(
backgroundColor: Colors.red[900],
title: Center(child: Text("WeinCheckerApp")),
),
body: PageView(
controller: _pageController,
children: [
SearchScreen(),
ScanScreen(),
FavScreen(),
],
),
);
}
}
ScanScreen.dart (one of 3 screens currently all the same):
class ScanScreen extends StatefulWidget {
@override
_ScanScreenState createState() => _ScanScreenState();
}
class _ScanScreenState extends State<ScanScreen> {
@override
Widget build(BuildContext context) {
SizeConfig().init(context);
return SafeArea(
minimum: EdgeInsets.only(
left: SizeConfig.safeBlockVertical,
right: SizeConfig.safeBlockVertical,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Expanded(
child: IconButton(
icon: Icon(Icons.search),
color: Colors.white,
onPressed: () => {},
iconSize: SizeConfig.safeBlockVertical * 5,
),
),
Ink(
decoration:
ShapeDecoration(shape: CircleBorder(), color: Colors.white),
child: IconButton(
icon: Icon(Icons.camera_alt),
color: Colors.grey[800],
onPressed: () => print("ScanButtonPressed"),
iconSize: SizeConfig.safeBlockVertical * 6,
),
),
Expanded(
child: IconButton(
icon: Icon(Icons.star),
color: Colors.white,
onPressed: () => {},
iconSize: SizeConfig.safeBlockVertical * 5,
),
),
],
),
],
),
);
}
}
The IconButtons shoud lead to the other screens.