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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android 列表倒計時的實現的示例代碼(CountDownTimer)
本篇文章主要介紹了Android 列表倒計時的實現的示例代碼(CountDownTimer),具有一定的參考價值,有興趣的可以了解一下2017-09-09
webview添加參數與修改請求頭的user-agent實例
這篇文章主要介紹了webview添加參數與修改請求頭的user-agent實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android Cocos Creator游戲開發(fā)平臺打包優(yōu)化實現方案
Cocos Creator是一款輕量、高效、免費開源的跨平臺游戲引擎,同時也是實時3D內容創(chuàng)作平臺,不僅支持2D、3D的游戲開發(fā),同時在HMI、IoT、XR、虛擬人偶等領域,均可提供一套完善的行業(yè)解決方案2022-11-11

