Flutter組件ListTile 使用說明

ListTile 通常用于在 Flutter 中填充 ListView祥款。在這篇文章中清笨,我將用可視化的例子來說明所有的參數(shù)。

title

title 參數(shù)可以接受任何小部件刃跛,但通常是文本小部件

ListTile(
  title: Text('Horse'),
)
復制代碼
title.png

subtitle

副標題是標題下面較小的文本

ListTile(
  title: Text('Horse'),
  subtitle: Text('A strong animal'),
)
復制代碼
subtitle.png

dense

使文本更小抠艾,并將所有內容打包在一起

ListTile(
  title: Text('Horse'),
  subtitle: Text('A strong animal'),
  dense: true,
)
復制代碼
dense.png

leading

將圖像或圖標添加到列表的開頭。這通常是一個圖標桨昙。

ListTile(
  leading: CircleAvatar(
    backgroundImage: NetworkImage(imageUrl),
  ),
  title: Text('Horse'),
)
復制代碼
leading.png
ListTile(
  leading: Icon(Icons.home),
  title: Text('House'),
)
復制代碼
leading1.png

trailing

設置拖尾將在列表的末尾放置一個圖像检号。這對于指示主-細節(jié)布局特別有用。

ListTile(
  title: Text('Horse'),
  trailing: Icon(Icons.keyboard_arrow_right),
)
復制代碼
trailing.png

contentPadding

設置內容邊距蛙酪,默認是 16齐苛,但我們在這里設置為 0

ListTile(
  title: Text('Horse'),
  trailing: Icon(Icons.keyboard_arrow_right),
  contentPadding: EdgeInsets.symmetric(horizontal: 0.0),
)
復制代碼
contentPadding.png

selected

如果選中列表的 item 項,那么文本和圖標的顏色將成為主題的主顏色桂塞。

ListTile(
  title: Text('Horse'),
  trailing: Icon(Icons.keyboard_arrow_right),
  selected: true,
)
復制代碼
selected.png

Gesture recognition

ListTile 可以檢測用戶的點擊和長按事件凹蜂,onTap 為單擊,onLongPress 為長按藐俺。對于波紋效果是內置的

ListTile(
  title: Text('Horse'),
  onTap: () {
    // do something
  },
  onLongPress: (){
    // do something else
  },
)
復制代碼
Gesture.gif

enabled

通過將 enable 設置為 false炊甲,來禁止點擊事件

ListTile(
  title: Text('Horse'),
  onTap: () {
    // this will not get called
  },
  enabled: false,
)
復制代碼
enabled.png

ListTile.divideTiles

靜態(tài)方法 divideTiles 可以在 titles 之間添加分隔符,這個顏色有點淡欲芹,需要看仔細點才能看出來卿啡,哈哈哈哈

ListView(
  children: ListTile.divideTiles(
      context: context,
      tiles: [
        ListTile(
          title: Text('Horse'),
        ),
        ListTile(
          title: Text('Cow'),
        ),
        ListTile(
          title: Text('Camel'),
        ),
        ListTile(
          title: Text('Sheep'),
        ),
        ListTile(
          title: Text('Goat'),
        ),
      ]
  ).toList(),
)
復制代碼
divideTiles.png

使用實例

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'My App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(title: Text('ListTile guide')),
        body: BodyWidget(),
      ),
    );
  }
}

String horseUrl = 'https://i.stack.imgur.com/Dw6f7.png';
String cowUrl = 'https://i.stack.imgur.com/XPOr3.png';
String camelUrl = 'https://i.stack.imgur.com/YN0m7.png';
String sheepUrl = 'https://i.stack.imgur.com/wKzo8.png';
String goatUrl = 'https://i.stack.imgur.com/Qt4JP.png';

class BodyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView(
      children: <Widget>[
        ListTile(
          leading: CircleAvatar(
            backgroundImage: NetworkImage(horseUrl),
          ),
          title: Text('Horse'),
          subtitle: Text('A strong animal'),
          trailing: Icon(Icons.keyboard_arrow_right),
          onTap: () {
            print('horse');
          },
          selected: true,
        ),
        ListTile(
          leading: CircleAvatar(
            backgroundImage: NetworkImage(cowUrl),
          ),
          title: Text('Cow'),
          subtitle: Text('Provider of milk'),
          trailing: Icon(Icons.keyboard_arrow_right),
          onTap: () {
            print('cow');
          },
        ),
        ListTile(
          leading: CircleAvatar(
            backgroundImage: NetworkImage(camelUrl),
          ),
          title: Text('Camel'),
          subtitle: Text('Comes with humps'),
          trailing: Icon(Icons.keyboard_arrow_right),
          onTap: () {
            print('camel');
          },
          enabled: false,
        ),
        ListTile(
          leading: CircleAvatar(
            backgroundImage: NetworkImage(sheepUrl),
          ),
          title: Text('Sheep'),
          subtitle: Text('Provides wool'),
          trailing: Icon(Icons.keyboard_arrow_right),
          onTap: () {
            print('sheep');
          },
        ),
        ListTile(
          leading: CircleAvatar(
            backgroundImage: NetworkImage(goatUrl),
          ),
          title: Text('Goat'),
          subtitle: Text('Some have horns'),
          trailing: Icon(Icons.keyboard_arrow_right),
          onTap: () {
            print('goat');
          },
        ),
      ],
    );
  }
}
復制代碼
demo.png

code.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市菱父,隨后出現(xiàn)的幾起案子颈娜,更是在濱河造成了極大的恐慌,老刑警劉巖浙宜,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件官辽,死亡現(xiàn)場離奇詭異,居然都是意外死亡粟瞬,警方通過查閱死者的電腦和手機同仆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來裙品,“玉大人俗批,你說我怎么就攤上這事俗或。” “怎么了岁忘?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵辛慰,是天一觀的道長。 經(jīng)常有香客問我干像,道長帅腌,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任麻汰,我火速辦了婚禮速客,結果婚禮上,老公的妹妹穿的比我還像新娘什乙。我一直安慰自己挽封,他們只是感情好,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布臣镣。 她就那樣靜靜地躺著,像睡著了一般智亮。 火紅的嫁衣襯著肌膚如雪忆某。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天阔蛉,我揣著相機與錄音弃舒,去河邊找鬼。 笑死状原,一個胖子當著我的面吹牛聋呢,可吹牛的內容都是我干的。 我是一名探鬼主播颠区,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼削锰,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了毕莱?” 一聲冷哼從身側響起器贩,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎朋截,沒想到半個月后蛹稍,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡部服,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年唆姐,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片廓八。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡奉芦,死狀恐怖胆描,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情仗阅,我是刑警寧澤昌讲,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站减噪,受9級特大地震影響短绸,放射性物質發(fā)生泄漏。R本人自食惡果不足惜筹裕,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一醋闭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧朝卒,春花似錦证逻、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至瑞眼,卻和暖如春龙宏,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背伤疙。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工银酗, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人徒像。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓黍特,卻偏偏與公主長得像,于是被迫代替她去往敵國和親锯蛀。 傳聞我的和親對象是個殘疾皇子灭衷,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內容