欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Flutter時間軸Timeline的實現

 更新時間:2021年05月19日 10:52:45   作者:zhuangch  
時間軸在很多地方都可以用的到,本文介紹了Flutter時間軸Timeline的實現,具有一定的參考價值,感興趣的小伙伴們可以參考一下

首先看看時間軸效果圖

在這里插入圖片描述

實現的難點就是左邊的時間線,右邊的事件說白了就是一個ListView,仔細觀察一下會發(fā)現圓圈在ListView的一個item上,想明白這些我們就可以把圓圈和右邊的事件作為一個listitem實現,左邊的豎線可以有兩種實現方法

1)listItem是一個Row,Row里含有一條豎線
2)Stack實現,Stack有兩個child widget,一個是豎線,一個是ListView

本文簡單用第二種來實現它,廢話少說先上代碼

@override
  Widget build(BuildContext context) {
    return Card(
      elevation: 0,
      margin: EdgeInsets.symmetric(horizontal: 15, vertical: 50),
      child: Stack(
        fit: StackFit.loose,
        children: <Widget>[
          // 左邊的豎線
          Positioned(
            left: 21,
            top: 15,
            bottom: 15,
            child: VerticalDivider(
              width: 1,
            ),
          ),
          // 右邊的事件列表
          ListView.separated(
            padding: EdgeInsets.zero,
            itemCount: events.length,
            physics: NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            itemBuilder: (BuildContext context, int index) {
              return FlowEventRow(events[index]);
            },
            separatorBuilder: (BuildContext context, int index) {
              return Divider(
                height: 1,
                indent: 45,
              );
            },
          ),
        ],
      ),
    );
  }

代碼很簡單我就不作過多解釋,主要來解釋下事件行FlowEventRow怎么實現左邊的圓圈
直接看代碼

class FlowEvent {
  const FlowEvent({
    this.advise,
    @required this.date,
    @required this.author,
    this.isCompleted = true,
  });

  final String advise;
  final DateTime date;
  final bool isCompleted;
  final String author;

  bool get hasAdvise =>
      isCompleted && advise != null ? advise?.isNotEmpty : false;
}

@immutable
class FlowEventRow extends StatelessWidget {
  const FlowEventRow(this.event);

  final FlowEvent event;

  double get circleRadius => event.isCompleted ? 8 : 6;
  Color get circleColor =>
      event.isCompleted ? const Color(0xFF40BE7F) : const Color(0xFFD5D5D5);

  @override
  Widget build(BuildContext context) {
    final Color dimColor = const Color(0xFFC5C5C5);
    final Color highlightColor = const Color(0xFF40BE7F);

    return Padding(
      padding: EdgeInsets.symmetric(vertical: 10),
      child: Row(
        children: <Widget>[
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 22.0 - circleRadius),
            child: Container(
              width: circleRadius * 2,
              height: circleRadius * 2,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: circleColor,
              ),
            ),
          ),
          Expanded(
            child: Padding(
              padding: EdgeInsets.only(left: 0, right: 15),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Expanded(
                        child: Text(
                          '制單',
                          style: TextStyle(
                            fontSize: 13,
                            color:
                                event.isCompleted ? highlightColor : dimColor,
                          ),
                        ),
                      ),
                      Text(
                        DateUtils.formatDay(event.date, withHms: true),
                        style: Theme.of(context)
                            .textTheme
                            .caption
                            .copyWith(color: dimColor),
                      )
                    ],
                  ),
                  ...event.hasAdvise
                      ? [
                          SizedBox(
                            height: 4,
                          ),
                          Text(
                            event.advise ?? '',
                            style: Theme.of(context)
                                .textTheme
                                .body1
                                .copyWith(fontSize: 12),
                          )
                        ]
                      : [],
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

build方法有點長,但實現圓圈的代碼很少

Padding(
            padding: EdgeInsets.symmetric(horizontal: 22.0 - circleRadius),
            child: Container(
              width: circleRadius * 2,
              height: circleRadius * 2,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: circleColor,
              ),
            ),
          ),

坐標的計算就是通過左邊豎線的x坐標 - 圈圈的半徑獲得,至此我們就實現了簡單的timeline

到此這篇關于Flutter時間軸Timeline的實現的文章就介紹到這了,更多相關Flutter時間軸 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論