PageView

基本用法

  1. PageView(
  2. scrollDirection : Axis.horizontal, // 主轴方向
  3. controller : PageController( // 设置子元素沾满多少
  4. viewportFraction : 0.9
  5. ),
  6. reverse : false, // 颠倒顺序
  7. physics: ScrollPhysics(),
  8. pageSnapping : true, // 断裂 如false则就不是一快一块来滑动了。
  9. allowImplicitScrolling : true,
  10. children: <Widget>[
  11. _Container( Colors.yellow[200] ),
  12. _Container( Colors.green[300] ),
  13. _Container( Colors.blue[400] ),
  14. _Container( Colors.grey[500] ),
  15. _Container( Colors.pink[600] ),
  16. _Container( Colors.orange[700] ),
  17. ],
  18. onPageChanged: (e){
  19. print("this is onPageChanged! $e");
  20. },
  21. )

基本属性

  1. PageView({
  2. Key key,
  3. this.scrollDirection = Axis.horizontal,
  4. this.reverse = false,
  5. PageController controller,
  6. this.physics,
  7. this.pageSnapping = true,
  8. this.onPageChanged,
  9. List<Widget> children = const <Widget>[],
  10. this.dragStartBehavior = DragStartBehavior.start,
  11. this.allowImplicitScrolling = false,
  12. }) : assert(allowImplicitScrolling != null),
  13. controller = controller ?? _defaultPageController,
  14. childrenDelegate = SliverChildListDelegate(children),
  15. super(key: key);

image.png image.png

PageView.builder 实现无限轮播

基本用法

  1. List _pageList = [
  2. _Container( Colors.yellow[200] ),
  3. _Container( Colors.green[300] ),
  4. _Container( Colors.blue[400] ),
  5. _Container( Colors.grey[500] ),
  6. _Container( Colors.pink[600] ),
  7. _Container( Colors.orange[700] ),
  8. ];
  9. PageView.builder(
  10. controller : PageController(
  11. viewportFraction : 0.9
  12. ),
  13. // typedef IndexedWidgetBuilder = Widget Function(BuildContext context, int index);
  14. itemBuilder: (context,index){
  15. return _pageList[index % _pageList.length];
  16. },
  17. itemCount : 2000 // 项目个数,不要设置为佳
  18. )

基本属性

  1. PageView.builder({
  2. Key key,
  3. this.scrollDirection = Axis.horizontal,
  4. this.reverse = false,
  5. PageController controller,
  6. this.physics,
  7. this.pageSnapping = true,
  8. this.onPageChanged,
  9. @required IndexedWidgetBuilder itemBuilder,
  10. int itemCount,
  11. this.dragStartBehavior = DragStartBehavior.start,
  12. this.allowImplicitScrolling = false,
  13. }) : assert(allowImplicitScrolling != null),
  14. controller = controller ?? _defaultPageController,
  15. childrenDelegate = SliverChildBuilderDelegate(itemBuilder, childCount: itemCount),
  16. super(key: key);

示例

  1. class ExchangeScene extends StatefulWidget {
  2. @override
  3. State<StatefulWidget> createState() => _ViewPageState();
  4. }
  5. class _ViewPageState extends State<ExchangeScene> {
  6. var imgList = [
  7. 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=904970218,1071764187&fm=11&gp=0.jpg',
  8. 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3398997146,632450674&fm=11&gp=0.jpg',
  9. "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2988284211,4212612697&fm=11&gp=0.jpg",
  10. "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1356733267,2792424737&fm=11&gp=0.jpg",
  11. ];
  12. PageController _pageController;
  13. var _currPageValue = 0.0;
  14. //缩放系数
  15. double _scaleFactor = .8;
  16. //view page height
  17. double _height = 230.0;
  18. @override
  19. void initState() {
  20. super.initState();
  21. _pageController = PageController(viewportFraction: 0.9);
  22. _pageController.addListener(() {
  23. setState(() {
  24. _currPageValue = _pageController.page;
  25. });
  26. });
  27. }
  28. @override
  29. void dispose() {
  30. super.dispose();
  31. _pageController.dispose();
  32. }
  33. @override
  34. Widget build(BuildContext context) {
  35. return Column(
  36. children: <Widget>[
  37. Container(
  38. width: 300,
  39. height: 100,
  40. ),
  41. Container(
  42. height: _height,
  43. child: PageView.builder(
  44. itemBuilder: (context, index) => _buildPageItem(index),
  45. itemCount: 1000,
  46. controller: _pageController,
  47. )),
  48. ],
  49. );
  50. }
  51. _buildPageItem(int index) {
  52. Matrix4 matrix4 = Matrix4.identity();
  53. if (index == _currPageValue.floor()) {
  54. //当前的item
  55. var currScale = 1 - (_currPageValue - index) * (1 - _scaleFactor);
  56. var currTrans = _height * (1 - currScale) / 2;
  57. matrix4 = Matrix4.diagonal3Values(1.0, currScale, 1.0)
  58. ..setTranslationRaw(0.0, currTrans, 0.0);
  59. } else if (index == _currPageValue.floor() + 1) {
  60. //右边的item
  61. var currScale =
  62. _scaleFactor + (_currPageValue - index + 1) * (1 - _scaleFactor);
  63. var currTrans = _height * (1 - currScale) / 2;
  64. matrix4 = Matrix4.diagonal3Values(1.0, currScale, 1.0)
  65. ..setTranslationRaw(0.0, currTrans, 0.0);
  66. } else if (index == _currPageValue.floor() - 1) {
  67. //左边
  68. var currScale = 1 - (_currPageValue - index) * (1 - _scaleFactor);
  69. var currTrans = _height * (1 - currScale) / 2;
  70. matrix4 = Matrix4.diagonal3Values(1.0, currScale, 1.0)
  71. ..setTranslationRaw(0.0, currTrans, 0.0);
  72. } else {
  73. //其他,不在屏幕显示的item
  74. matrix4 = Matrix4.diagonal3Values(1.0, _scaleFactor, 1.0)
  75. ..setTranslationRaw(0.0, _height * (1 - _scaleFactor) / 2, 0.0);
  76. }
  77. return Transform(
  78. transform: matrix4,
  79. child: Stack(children: <Widget>[
  80. Padding(
  81. padding: EdgeInsets.symmetric(horizontal: 10),
  82. child: Container(
  83. decoration: BoxDecoration(
  84. borderRadius: BorderRadius.circular(12),
  85. image: DecorationImage(
  86. image: NetworkImage(imgList[index % 4]), fit: BoxFit.fill),
  87. ),
  88. ),
  89. ),
  90. ],)
  91. );
  92. }
  93. }

image.png

结合 flutter_swiper 实现轮图

  1. class ExchangeScene extends StatefulWidget {
  2. @override
  3. State<StatefulWidget> createState() => _ViewPageState();
  4. }
  5. class _ViewPageState extends State<ExchangeScene> {
  6. var imgList = [
  7. 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=904970218,1071764187&fm=11&gp=0.jpg',
  8. 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3398997146,632450674&fm=11&gp=0.jpg',
  9. "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2988284211,4212612697&fm=11&gp=0.jpg",
  10. "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1356733267,2792424737&fm=11&gp=0.jpg",
  11. ];
  12. PageController _pageController;
  13. var _currPageValue = 0.0;
  14. //缩放系数
  15. double _scaleFactor = .8;
  16. //view page height
  17. double _height = 230.0;
  18. @override
  19. void initState() {
  20. super.initState();
  21. _pageController = PageController(viewportFraction: 0.9);
  22. _pageController.addListener(() {
  23. setState(() {
  24. _currPageValue = _pageController.page;
  25. });
  26. });
  27. }
  28. @override
  29. void dispose() {
  30. super.dispose();
  31. _pageController.dispose();
  32. }
  33. @override
  34. Widget build(BuildContext context) {
  35. return Column(
  36. children: <Widget>[
  37. Container(
  38. width: 300,
  39. height: 100,
  40. ),
  41. Container(
  42. height: _height,
  43. child: PageView.builder(
  44. itemBuilder: (context, index) => _buildPageItem(index),
  45. itemCount: 1000,
  46. controller: _pageController,
  47. )),
  48. Container(
  49. height: 300,
  50. // width: 300,
  51. child: Swiper(
  52. itemBuilder: (context, index) => _buildPageItem(index),
  53. autoplay : true,
  54. itemCount: 100000,
  55. duration : 1000,
  56. viewportFraction: 0.8,
  57. scale: 0.9,
  58. )
  59. ,
  60. )
  61. ],
  62. );
  63. }
  64. _buildPageItem(int index) {
  65. Matrix4 matrix4 = Matrix4.identity();
  66. if (index == _currPageValue.floor()) {
  67. //当前的item
  68. var currScale = 1 - (_currPageValue - index) * (1 - _scaleFactor);
  69. var currTrans = _height * (1 - currScale) / 2;
  70. matrix4 = Matrix4.diagonal3Values(1.0, currScale, 1.0)
  71. ..setTranslationRaw(0.0, currTrans, 0.0);
  72. } else if (index == _currPageValue.floor() + 1) {
  73. //右边的item
  74. var currScale =
  75. _scaleFactor + (_currPageValue - index + 1) * (1 - _scaleFactor);
  76. var currTrans = _height * (1 - currScale) / 2;
  77. matrix4 = Matrix4.diagonal3Values(1.0, currScale, 1.0)
  78. ..setTranslationRaw(0.0, currTrans, 0.0);
  79. } else if (index == _currPageValue.floor() - 1) {
  80. //左边
  81. var currScale = 1 - (_currPageValue - index) * (1 - _scaleFactor);
  82. var currTrans = _height * (1 - currScale) / 2;
  83. matrix4 = Matrix4.diagonal3Values(1.0, currScale, 1.0)
  84. ..setTranslationRaw(0.0, currTrans, 0.0);
  85. } else {
  86. //其他,不在屏幕显示的item
  87. matrix4 = Matrix4.diagonal3Values(1.0, _scaleFactor, 1.0)
  88. ..setTranslationRaw(0.0, _height * (1 - _scaleFactor) / 2, 0.0);
  89. }
  90. return Transform(
  91. transform: matrix4,
  92. child: Stack(children: <Widget>[
  93. Padding(
  94. padding: EdgeInsets.symmetric(horizontal: 10),
  95. child: Container(
  96. decoration: BoxDecoration(
  97. borderRadius: BorderRadius.circular(12),
  98. image: DecorationImage(
  99. image: NetworkImage(imgList[index % 4]), fit: BoxFit.fill),
  100. ),
  101. ),
  102. ),
  103. ],)
  104. );
  105. }
  106. }

image.pngimage.png