Flutter教学

Flutter(33):Material组件之ListTile、

2020-10-04  本文已影响0人  starryxp

Flutter教学目录持续更新中

Github源代码持续更新中

1.简介

这一节呢这三个组件一起来介绍

2.ListTile属性

selected = false.png selected = true.png
_myListTitle() {
  return ListTile(
    leading: Icon(Icons.account_circle),
    title: Text('title'),
    subtitle: Text('subtitle'),
    trailing: Icon(Icons.account_box),
    dense: false,
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(5),
    ),
    contentPadding: EdgeInsets.all(10),
    enabled: true,
    onTap: () {
      ToastUtil.showToast('onTap');
    },
    onLongPress: () {
      ToastUtil.showToast('onLongPress');
    },
    selected: true,
  );
}

3.RefreshIndicator属性

1601720169(1).png
_myRefreshIndicator() {
  return RefreshIndicator(
    child: _myListView(),
    displacement: 40,
    onRefresh: () {
      ToastUtil.showToast('onRefresh');
      return _onRefresh();
    },
    color: Colors.blue,
    backgroundColor: Colors.white,
    strokeWidth: 2,
  );
}
_onRefresh() async {
  await Future.delayed(Duration(seconds: 3), () {
    setState(() {});
  });
}

4.ListView

ListView的创建方式有多种:

4.1ListView()

这种是最简单基础的方式,子节点是返回widgets

_getListWidgets() {
    for (var i = 0; i < 10; i++) {
      _widgetList.add(_myListTitle());
    }
    return _widgetList;
  }

  _myListView() {
    return ListView(
      scrollDirection: Axis.vertical,
      reverse: false,
      controller: null,
      primary: false,
      itemExtent: 60,
      cacheExtent: 60,
      padding: EdgeInsets.all(10),
      children: _getListWidgets(),
    );
  }

4.2ListView.builder()

跟ListView()创建方式区别不大,主要是子节点属性变了,itemBuilder返回子节点,itemCount确定子节点数量

_myListViewBuild() {
    return ListView.builder(
      scrollDirection: Axis.vertical,
      reverse: false,
      controller: null,
      primary: false,
      itemExtent: 60,
      cacheExtent: 60,
      padding: EdgeInsets.all(10),
      itemBuilder: (BuildContext context, int index) {
        return _myListTitle();
      },
      itemCount: _widgetList.length,
    );
  }

4.3ListView.separated()

这个是可以设置分割线的,那么我们就先了解一下fluter里面的分割线组件
Divider、VerticalDivider、PopupMenuDivider
前面两个的属性是一样的,只是一个横向一个纵向,后面一个是看名字也知道,给PopupMenu用的,当然用在ListView其实也是可以的,但是他只有一个height属性

进入正题:

这个就是在ListView.builder()基础上多了分割线属性

  _myListViewSeparated() {
    return ListView.separated(
      primary: true,
      itemBuilder: (BuildContext context, int index) => _myListTitle(),
      separatorBuilder: (BuildContext context, int index) {
        return Divider(
          height: 1,
          thickness: 1,
          indent: 10,
          endIndent: 10,
        );
        return Container(
          child: Text('----分割线----'),
          color: Colors.grey,
        );
      },
      itemCount: _widgetList.length,
    );
  }

4.4ListView.custom()

  _myListViewCustom() {
    return ListView.custom(
      childrenDelegate: SliverChildBuilderDelegate(
        (BuildContext context, int index) {
          return _myListTitle();
        },
        childCount: _widgetList.length,
        findChildIndexCallback: (key){
          print('key = $key');
          return 0;
        }
      ),
    );
  }

下一节:Material组件之ExpansionPanelList、ExpansionPanel、ExpansionPanelRadio、ExpansionTile

Flutter(34):Material组件之ExpansionPanelList、ExpansionPanel、ExpansionPanelRadio、ExpansionTile

Flutter教学目录持续更新中

Github源代码持续更新中

上一篇下一篇

猜你喜欢

热点阅读