|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +class Index extends StatefulWidget { |
| 4 | + @override |
| 5 | + State<StatefulWidget> createState() => _IndexState(); |
| 6 | +} |
| 7 | + |
| 8 | +class _IndexState extends State<Index> { |
| 9 | + int _index = 0; |
| 10 | + bool _scrollDirection = true; |
| 11 | + bool _pageSnapping = true; |
| 12 | + |
| 13 | + PageController _pageController; |
| 14 | + @override |
| 15 | + void initState() { |
| 16 | + super.initState(); |
| 17 | + _pageController = PageController(); |
| 18 | + } |
| 19 | + |
| 20 | + String getScrollDirectionText () { |
| 21 | + return _scrollDirection ? '左右' : '上下'; |
| 22 | + } |
| 23 | + |
| 24 | + Axis getScrollDirection () { |
| 25 | + return _scrollDirection ? Axis.horizontal : Axis.vertical; |
| 26 | + } |
| 27 | + |
| 28 | + @override |
| 29 | + Widget build(BuildContext context) { |
| 30 | + return Scaffold( |
| 31 | + appBar: AppBar( |
| 32 | + title: Text('PageView'), |
| 33 | + ), |
| 34 | + body: PageView( |
| 35 | + pageSnapping: _pageSnapping, // 与滚动行为相关 |
| 36 | + reverse: false, // 滚动反方向 |
| 37 | + scrollDirection: getScrollDirection(), |
| 38 | + controller: _pageController, |
| 39 | + onPageChanged: (index) { |
| 40 | + setState(() { |
| 41 | + _index = index; |
| 42 | + }); |
| 43 | + }, |
| 44 | + children: [ |
| 45 | + Center( |
| 46 | + child: Column( |
| 47 | + children: [ |
| 48 | + Text('页面一'), |
| 49 | + Text('${getScrollDirectionText()}滑动切换页面哦~~~'), |
| 50 | + RaisedButton( |
| 51 | + child: Text('当前为${getScrollDirectionText()}滑动,可点击切换'), |
| 52 | + onPressed: () { |
| 53 | + setState(() { |
| 54 | + _scrollDirection = !_scrollDirection; |
| 55 | + }); |
| 56 | + }, |
| 57 | + ), |
| 58 | + ], |
| 59 | + ), |
| 60 | + ), |
| 61 | + Center( |
| 62 | + child: Column( |
| 63 | + mainAxisAlignment: MainAxisAlignment.center, |
| 64 | + children: [ |
| 65 | + Text('页面二'), |
| 66 | + Text('${getScrollDirectionText()}滑动切换页面哦~~~'), |
| 67 | + RaisedButton( |
| 68 | + child: Text('当前为属性值pageSnapping为${_pageSnapping},${_pageSnapping ? '滚动时,默认被捕获' : '由用户自行滚动'},可点击切换'), |
| 69 | + onPressed: () { |
| 70 | + setState(() { |
| 71 | + _pageSnapping = !_pageSnapping; |
| 72 | + }); |
| 73 | + }, |
| 74 | + ), |
| 75 | + ], |
| 76 | + ), |
| 77 | + ), |
| 78 | + Center( |
| 79 | + child: Column( |
| 80 | + mainAxisAlignment: MainAxisAlignment.end, |
| 81 | + children: [ |
| 82 | + Text('页面三'), |
| 83 | + Text('${getScrollDirectionText()}滑动切换页面哦~~~'), |
| 84 | + ], |
| 85 | + ), |
| 86 | + ), |
| 87 | + ], |
| 88 | + ), |
| 89 | + bottomNavigationBar: BottomNavigationBar( |
| 90 | + currentIndex: _index, |
| 91 | + type: BottomNavigationBarType.shifting, |
| 92 | + onTap: (index) { |
| 93 | + setState(() { |
| 94 | + _index = index; |
| 95 | + }); |
| 96 | + _pageController.jumpToPage(index); |
| 97 | + }, |
| 98 | + fixedColor: Colors.red, |
| 99 | + iconSize: 28, |
| 100 | + items: [ |
| 101 | + BottomNavigationBarItem( |
| 102 | + title: Text( |
| 103 | + '导航一', |
| 104 | + style: TextStyle(color: Colors.red), |
| 105 | + ), |
| 106 | + icon: Icon( |
| 107 | + Icons.navigate_before, |
| 108 | + color: Colors.redAccent, |
| 109 | + ), |
| 110 | + ), |
| 111 | + BottomNavigationBarItem( |
| 112 | + title: Text( |
| 113 | + '导航二', |
| 114 | + style: TextStyle(color: Colors.red), |
| 115 | + ), |
| 116 | + icon: Icon( |
| 117 | + Icons.notifications_active, |
| 118 | + color: Colors.redAccent, |
| 119 | + ), |
| 120 | + ), |
| 121 | + BottomNavigationBarItem( |
| 122 | + title: Text( |
| 123 | + '导航三', |
| 124 | + style: TextStyle(color: Colors.red), |
| 125 | + ), |
| 126 | + icon: Icon( |
| 127 | + Icons.navigate_next, |
| 128 | + color: Colors.redAccent, |
| 129 | + ), |
| 130 | + ), |
| 131 | + ], |
| 132 | + ), |
| 133 | + ); |
| 134 | + } |
| 135 | +} |
0 commit comments