Flutter 入門之 ListTile 使用指南

ListTile 一般用於在 Flutter 中填充 ListView。在這篇文章中,我將用可視化的例子來講明全部的參數。bash

title

title 參數能夠接受任何小部件,但一般是文本小部件app

ListTile(
  title: Text('Horse'),
)
複製代碼

title.png

subtitle

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

ListTile(
  title: Text('Horse'),
  subtitle: Text('A strong animal'),
)
複製代碼

subtitle.png

dense

使文本更小,並將全部內容打包在一塊兒ide

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

設置拖尾將在列表的末尾放置一個圖像。這對於指示主-細節佈局特別有用。ui

ListTile(
  title: Text('Horse'),
  trailing: Icon(Icons.keyboard_arrow_right),
)
複製代碼

trailing.png

contentPadding

設置內容邊距,默認是 16,但咱們在這裏設置爲 0this

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

contentPadding.png

selected

若是選中列表的 item 項,那麼文本和圖標的顏色將成爲主題的主顏色。spa

ListTile(
  title: Text('Horse'),
  trailing: Icon(Icons.keyboard_arrow_right),
  selected: true,
)
複製代碼

selected.png

Gesture recognition

ListTile 能夠檢測用戶的點擊和長按事件,onTap 爲單擊,onLongPress 爲長按。對於波紋效果是內置的debug

ListTile(
  title: Text('Horse'),
  onTap: () {
    // do something
  },
  onLongPress: (){
    // do something else
  },
)
複製代碼

Gesture.gif

enabled

經過將 enable 設置爲 false,來禁止點擊事件3d

ListTile(
  title: Text('Horse'),
  onTap: () {
    // this will not get called
  },
  enabled: false,
)
複製代碼

enabled.png

ListTile.divideTiles

靜態方法 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

做者介紹

  • 陳堅潤:廣州蘆葦科技 APP 團隊 Android 開發工程師

內推信息

  • 咱們正在招募小夥伴,有興趣的小夥伴能夠把簡歷發到 app@talkmoney.cn,備註:來自掘金社區
  • 詳情能夠戳這裏--> 廣州蘆葦信息科技
相關文章
相關標籤/搜索