PageVIew,可滾動的視圖列表組件,而且每一個子組件的大小都和視圖窗口大小一樣。
屬性:
- controller -> PageController 用於控制視圖頁面滾動到的位置
- children 視圖頁面列表
- scrollDirection 頁面滾動的方向,從左往右,或者從上往下
- onPageChanged 視圖頁面發生轉換的時候進行的函數操作
- reverse 對視圖頁面的排列順序進行反轉
效果:
PageView的用法
在項目的main.dart中的代碼:
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.pink, ), home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Demo Code',style: TextStyle(color: Colors.white),), centerTitle: true, ), body: Container( height: 500.0,//確保pageview的高度 child: PageView( controller: PageController( initialPage: 0,//讓初始頁為第一個pageview的實例 viewportFraction: 1.0//讓頁面視圖充滿整個視圖窗口 即充滿400px高的視圖窗口 ), children: <Widget>[ Container( color: Colors.yellow, child: Center( child: Text('這是第一個pageView的實例',style: TextStyle(color: Colors.white,fontSize: 20.0),), ), ), Container( color: Colors.red, child: Center( child: Text('這是第二個pageView的實例',style: TextStyle(color: Colors.white,fontSize: 20.0),), ), ), Container( color: Colors.green, child: Center( child: Text('這是第三個pageView的實例',style: TextStyle(color: Colors.white,fontSize: 20.0),), ), ) ], scrollDirection: Axis.vertical,//上下滾動 onPageChanged: (int index) { print('這是第${index}個頁面'); }, reverse: false,//是否反轉頁面的順序 ), ), ); } }
如果container的高度500沒有設置的話,每個頁面的大小將是手機的可視高度。
如果controller中的initialPage設置為1,則當前顯示的頁面時第二個頁面
viewprotFraction的默認值為1.0,表示頁面視圖充滿整個父容器。若是0.5,則頁面視圖的高度是父容器高度的一半。