Flutter GridView 控件

一個可滾動的2D排列的小部件扮超。

控件網(wǎng)格的主軸方向是它滾動的方向( scrollDirection)帖池。

最常用的網(wǎng)格布局是 GridView.count年扩,它創(chuàng)建了一個在橫軸上具有固定數(shù)量 網(wǎng)格塊 的平鋪的布局,和 GridView.extent 雷激,它使用具有最大橫軸范圍的 網(wǎng)格塊 創(chuàng)建布局替蔬。自定義 SliverGridDelegate 可以生成任意2D排列的子代,包括未對齊或重疊的排列屎暇。

要創(chuàng)建具有大量(或無限)個子節(jié)點的網(wǎng)格承桥,請將 GridView.builder 構(gòu)造函數(shù)與gridDelegateSliverGridDelegateWithFixedCrossAxisCountSliverGridDelegateWithMaxCrossAxisExtent 一起使用。

要使用自定義SliverChildDelegate 恭垦,請使用 GridView.custom快毛。

要創(chuàng)建線性數(shù)組的子代格嗅,請使用 ListView番挺。

若要控制滾動視圖的初始滾動偏移,請為控制器 controller 提供其ScrollController.initialScrollOffset 屬性集屯掖。

轉(zhuǎn)換到CustomScrollView

GridView基本上是一個 CustomScrollView 玄柏,其 CustomScrollView.slivers 中包含一個SliverGrid

如果GridView不再足夠贴铜,例如因為滾動視圖既有網(wǎng)格又有列表粪摘,或者因為網(wǎng)格要與 SliverAppBar 等組合在一起,直接將代碼從使用 GridView 移植到 CustomScrollView 直接使用绍坝。

CustomScrollView.slivers 屬性只能使用包含 SliverGrid 的列表徘意。

GridView上 的 childrenDelegate 屬性對應(yīng)于 SliverGrid.delegate 屬性,GridView上 的 gridDelegate 屬性對應(yīng)于 SliverGrid.gridDelegate 屬性轩褐。

GridView椎咧,GridView.count和GridView.extent構(gòu)造函數(shù)的子參數(shù)對應(yīng)于 GridView 中 childrenDelegate 是具有相同參數(shù)的 SliverChildListDelegate

GridView.builder 構(gòu)造函數(shù)的 itemBuilder 和 childCount 參數(shù)對應(yīng)于 GridView 中的 childrenDelegate把介,它們是具有匹配參數(shù)的 SliverChildBuilderDelegate

GridView.count 和 GridView.extent 構(gòu)造函數(shù)創(chuàng)建自定義網(wǎng)格委托勤讽,并在 SliverGrid上 具有等效命名的構(gòu)造函數(shù)以簡化轉(zhuǎn)換:分別為 SliverGrid.count 和 SliverGrid.extent。

padding 屬性對應(yīng)于在 CustomScrollView.slivers 屬性中使用 SliverPadding 而不是網(wǎng)格本身拗踢,并且讓 SliverGrid 成為 SliverPadding 的子級脚牍。

默認情況下,ListView將自動填充列表的可滾動范圍巢墅,以避免MediaQuery的padding 造成的部分障礙诸狭。要避免此行為券膀,請使用padding屬性為0進行覆蓋。

將代碼移植到使用CustomScrollView后作谚,可以將其他slivers(例如 SliverListSliverAppBar)放入CustomScrollView.slivers 中三娩。

以下是使用 CustomScrollView 顯示 GridView 等效的代碼片段:

GridView.count(
  primary: false,
  padding: const EdgeInsets.all(20.0),
  crossAxisSpacing: 10.0,
  crossAxisCount: 2,
  children: <Widget>[
    const Text('He\'d have you all unravel at the'),
    const Text('Heed not the rabble'),
    const Text('Sound of screams but the'),
    const Text('Who scream'),
    const Text('Revolution is coming...'),
    const Text('Revolution, they...'),
  ],
)
CustomScrollView(
  primary: false,
  slivers: <Widget>[
    SliverPadding(
      padding: const EdgeInsets.all(20.0),
      sliver: SliverGrid.count(
        crossAxisSpacing: 10.0,
        crossAxisCount: 2,
        children: <Widget>[
          const Text('He\'d have you all unravel at the'),
          const Text('Heed not the rabble'),
          const Text('Sound of screams but the'),
          const Text('Who scream'),
          const Text('Revolution is coming...'),
          const Text('Revolution, they...'),
        ],
      ),
    ),
  ],
)

也可以看看:

構(gòu)造函數(shù)

使用自定義SliverGridDelegate創(chuàng)建可滾動的2D小部件數(shù)組
GridView({Key key, Axis scrollDirection: Axis.vertical, bool reverse: false, ScrollController controller, bool primary, ScrollPhysics physics, bool shrinkWrap: false, EdgeInsetsGeometry padding, @required SliverGridDelegate gridDelegate, bool addAutomaticKeepAlives: true, bool addRepaintBoundaries: true, bool addSemanticIndexes: true, double cacheExtent, List<Widget> children: const <widget>[]</widget>, int semanticChildCount })

創(chuàng)建 按需創(chuàng)建的可滾動的2D小部件數(shù)組
GridView.builder({Key key, Axis scrollDirection: Axis.vertical, bool reverse: false, ScrollController controller, bool primary, ScrollPhysics physics, bool shrinkWrap: false, EdgeInsetsGeometry padding, @required SliverGridDelegate gridDelegate, @required IndexedWidgetBuilder itemBuilder, int itemCount, bool addAutomaticKeepAlives: true, bool addRepaintBoundaries: true, bool addSemanticIndexes: true, double cacheExtent, int semanticChildCount })

創(chuàng)建一個可滾動的2D小部件數(shù)組已卷,在橫軸上具有固定數(shù)量的網(wǎng)格塊
GridView.count({Key key, Axis scrollDirection: Axis.vertical, bool reverse: false, ScrollController controller, bool primary, ScrollPhysics physics, bool shrinkWrap: false, EdgeInsetsGeometry padding, @required int crossAxisCount, double mainAxisSpacing: 0.0, double crossAxisSpacing: 0.0, double childAspectRatio: 1.0, bool addAutomaticKeepAlives: true, bool addRepaintBoundaries: true, bool addSemanticIndexes: true, double cacheExtent, List<Widget> children: const <widget>[]</widget>, int semanticChildCount })

使用自定義SliverGridDelegate和自定義SliverChildDelegate創(chuàng)建可滾動的2D小部件數(shù)組
GridView.custom({Key key, Axis scrollDirection: Axis.vertical, bool reverse: false, ScrollController controller, bool primary, ScrollPhysics physics, bool shrinkWrap: false, EdgeInsetsGeometry padding, @required SliverGridDelegate gridDelegate, @required SliverChildDelegate childrenDelegate, double cacheExtent, int semanticChildCount })

使用每個都具有最大橫軸范圍的 網(wǎng)格塊 創(chuàng)建可滾動的2D小部件數(shù)組。
GridView.extent({Key key, Axis scrollDirection: Axis.vertical, bool reverse: false, ScrollController controller, bool primary, ScrollPhysics physics, bool shrinkWrap: false, EdgeInsetsGeometry padding, @required double maxCrossAxisExtent, double mainAxisSpacing: 0.0, double crossAxisSpacing: 0.0, double childAspectRatio: 1.0, bool addAutomaticKeepAlives: true, bool addRepaintBoundaries: true, bool addSemanticIndexes: true, List<Widget> children: const <widget>[]</widget>, int semanticChildCount })

屬性

名稱 類型 描述
childrenDelegateSliverChildDelegate 為GridView提供子代的委托
gridDelegateSliverGridDelegate 一個控制 GridView 中子項布局的委托淳蔼。
cacheExtentdouble 視口在可見區(qū)域之前和之后有一個區(qū)域侧蘸,用于緩存在用戶滾動時即將變?yōu)榭梢姷捻椖?/td>
controllerScrollController 一個對象,可用于控制滾動滾動視圖的位置
hashCodeint 此對象的哈希碼
keyKey 控制一個小部件如何替換樹中的另一個小部件
paddingEdgeInsetsGeometry 插入子代的內(nèi)邊距
physicsScrollPhysics 滾動視圖應(yīng)如何響應(yīng)用戶輸入鹉梨,物理滾動方式
primarybool 這是否是與父 PrimaryScrollController 關(guān)聯(lián)的主滾動視圖讳癌。
reversebool 滾動視圖是否在讀取方向上滾動
runtimeTypeType 表示對象的運行時類型
scrollDirectionAxis 滾動視圖的滾動軸
semanticChildCountint 將提供語義信息的子代數(shù)量
shrinkWrapbool 是否應(yīng)該由正在查看的內(nèi)容確定scrollDirection中滾動視圖的范圍。

方法

名稱 類型 描述
buildChildLayout(BuildContext context) → Widget 子類應(yīng)重寫此方法以構(gòu)建布局模型
build(BuildContext context) → Widget 描述此窗口小部件表示的用戶界面部分
buildSlivers(BuildContext context) → List<Widget> 構(gòu)建要放置在視口內(nèi)的窗口小部件列表
buildViewport(BuildContext context, ViewportOffset offset, AxisDirection axisDirection, List<Widget> slivers) → Widget 構(gòu)建視口
createElement() → StatelessElement 創(chuàng)建StatelessElement以管理此窗口小部件在樹中的位置存皂。
debugDescribeChildren() → List<DiagnosticsNode> 返回描述此節(jié)點的子節(jié)點的DiagnosticsNode對象列表晌坤。
debugFillProperties(DiagnosticPropertiesBuilder properties) → void 添加與節(jié)點關(guān)聯(lián)的其他屬性
getDirection(BuildContext context) → AxisDirection 返回滾動視圖滾動的AxisDirection
noSuchMethod(Invocation invocation) → dynamic 訪問不存在的方法或?qū)傩詴r調(diào)用
toDiagnosticsNode({String name, DiagnosticsTreeStyle style }) → DiagnosticsNode 返回調(diào)試工具和toStringDeep使用的對象的調(diào)試表示形式
toString({DiagnosticLevel minLevel: DiagnosticLevel.debug }) → String 返回此對象的字符串表示形式
toStringDeep({String prefixLineOne: '', String prefixOtherLines, DiagnosticLevel minLevel: DiagnosticLevel.debug }) → String 返回此節(jié)點及其后代的字符串表示形式。
toStringShallow({String joiner: ', ', DiagnosticLevel minLevel: DiagnosticLevel.debug }) → String 返回對象的單行詳細描述
toStringShort() → String 這個小部件的簡短文字描述
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末艰垂,一起剝皮案震驚了整個濱河市泡仗,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌猜憎,老刑警劉巖娩怎,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異胰柑,居然都是意外死亡截亦,警方通過查閱死者的電腦和手機爬泥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來崩瓤,“玉大人袍啡,你說我怎么就攤上這事∪赐埃” “怎么了境输?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長颖系。 經(jīng)常有香客問我嗅剖,道長,這世上最難降的妖魔是什么嘁扼? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任信粮,我火速辦了婚禮,結(jié)果婚禮上趁啸,老公的妹妹穿的比我還像新娘强缘。我一直安慰自己,他們只是感情好不傅,可當(dāng)我...
    茶點故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布旅掂。 她就那樣靜靜地躺著,像睡著了一般蛤签。 火紅的嫁衣襯著肌膚如雪辞友。 梳的紋絲不亂的頭發(fā)上栅哀,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天震肮,我揣著相機與錄音,去河邊找鬼留拾。 笑死戳晌,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的痴柔。 我是一名探鬼主播沦偎,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼咳蔚!你這毒婦竟也來了豪嚎?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤谈火,失蹤者是張志新(化名)和其女友劉穎侈询,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體糯耍,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡扔字,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年囊嘉,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片革为。...
    茶點故事閱讀 40,615評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡扭粱,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出震檩,到底是詐尸還是另有隱情琢蛤,我是刑警寧澤,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布抛虏,位于F島的核電站虐块,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏嘉蕾。R本人自食惡果不足惜贺奠,卻給世界環(huán)境...
    茶點故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望错忱。 院中可真熱鬧儡率,春花似錦、人聲如沸以清。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽掷倔。三九已至眉孩,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間勒葱,已是汗流浹背浪汪。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留凛虽,地道東北人死遭。 一個月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像凯旋,于是被迫代替她去往敵國和親呀潭。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,630評論 2 359