Last active
January 23, 2020 13:10
-
-
Save gladimdim/000ccfee0d52ee6c684d75e7c4cd5271 to your computer and use it in GitHub Desktop.
Two PageViews connected with buttons and scroll callbacks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: LinkedPageViews( | |
mainViews: [ | |
Image.network( | |
'https://locadeserta.com/game/assets/images/background/landing/3.jpg', | |
), | |
Image.network( | |
'https://locadeserta.com/game/assets/images/background/landing/1.jpg', | |
), | |
Image.network( | |
'https://locadeserta.com/game/assets/images/background/landing/4.jpg', | |
), | |
Image.network( | |
'https://locadeserta.com/game/assets/images/background/landing/5.jpg', | |
), | |
], | |
titles: [ | |
Center( | |
child: Column( | |
children: [ | |
Text('Swip below left/right to navigate in page view.'), | |
Text( | |
' The top navigation page view will auto adjust to the selected main page view.') | |
], | |
), | |
), | |
Center( | |
child: Text('Or Use arrow buttons for navigation'), | |
), | |
Center( | |
child: Text('The last slide'), | |
), | |
Text('Another slide') | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class LinkedPageViews extends StatefulWidget { | |
final List<Widget> mainViews; | |
final List<Widget> titles; | |
LinkedPageViews({this.mainViews, this.titles}); | |
@override | |
_LinkedPageViewsState createState() => _LinkedPageViewsState(); | |
} | |
class _LinkedPageViewsState extends State<LinkedPageViews> { | |
PageController _topPageController; | |
PageController _mainPageController; | |
@override | |
initState() { | |
super.initState(); | |
_topPageController = PageController(initialPage: 0, viewportFraction: 1); | |
_mainPageController = PageController(initialPage: 0) | |
..addListener(_onMainScroll); | |
} | |
_onMainScroll() { | |
_topPageController.animateTo(_mainPageController.offset, | |
duration: Duration(milliseconds: 150), curve: Curves.decelerate); | |
} | |
_goToPage(int page) { | |
_topPageController.animateToPage(page, | |
duration: Duration(milliseconds: 150), curve: Curves.decelerate); | |
_mainPageController.animateToPage(page, | |
duration: Duration(milliseconds: 40), curve: Curves.decelerate); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: <Widget>[ | |
Expanded( | |
flex: 1, | |
child: PageView.builder( | |
physics: NeverScrollableScrollPhysics(), | |
controller: _topPageController, | |
itemCount: widget.titles.length, | |
itemBuilder: (context, index) { | |
return Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Center( | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
children: <Widget>[ | |
if (index != 0) | |
Container( | |
child: IconButton( | |
icon: Icon(Icons.arrow_back), | |
onPressed: () { | |
_goToPage(index - 1); | |
}, | |
), | |
), | |
if (index == 0) SizedBox(width: 50), | |
widget.titles[index], | |
if (index != widget.titles.length - 1) | |
Container( | |
child: IconButton( | |
icon: Icon(Icons.arrow_forward), | |
onPressed: () { | |
_goToPage(index + 1); | |
}, | |
), | |
), | |
if (index == widget.titles.length - 1) | |
SizedBox(width: 50), | |
], | |
), | |
), | |
); | |
}, | |
), | |
), | |
Expanded( | |
flex: 9, | |
child: PageView.builder( | |
controller: _mainPageController, | |
scrollDirection: Axis.horizontal, | |
itemCount: widget.mainViews.length, | |
itemBuilder: (context, index) { | |
return widget.mainViews[index]; | |
}, | |
), | |
), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment