Flutter時(shí)間軸Timeline的實(shí)現(xiàn)
首先看看時(shí)間軸效果圖
實(shí)現(xiàn)的難點(diǎn)就是左邊的時(shí)間線,右邊的事件說白了就是一個(gè)ListView,仔細(xì)觀察一下會(huì)發(fā)現(xiàn)圓圈在ListView的一個(gè)item上,想明白這些我們就可以把圓圈和右邊的事件作為一個(gè)listitem實(shí)現(xiàn),左邊的豎線可以有兩種實(shí)現(xiàn)方法
1)listItem是一個(gè)Row,Row里含有一條豎線
2)Stack實(shí)現(xiàn),Stack有兩個(gè)child widget,一個(gè)是豎線,一個(gè)是ListView
本文簡單用第二種來實(shí)現(xiàn)它,廢話少說先上代碼
@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怎么實(shí)現(xiàn)左邊的圓圈
直接看代碼
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方法有點(diǎn)長,但實(shí)現(xiàn)圓圈的代碼很少
Padding( padding: EdgeInsets.symmetric(horizontal: 22.0 - circleRadius), child: Container( width: circleRadius * 2, height: circleRadius * 2, decoration: BoxDecoration( shape: BoxShape.circle, color: circleColor, ), ), ),
坐標(biāo)的計(jì)算就是通過左邊豎線的x坐標(biāo) - 圈圈的半徑獲得,至此我們就實(shí)現(xiàn)了簡單的timeline
到此這篇關(guān)于Flutter時(shí)間軸Timeline的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Flutter時(shí)間軸 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android平臺(tái)中實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)的5種方式
這篇文章主要為大家分享了介紹了Android平臺(tái)中實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)技術(shù)的5種方式,供大家學(xué)習(xí),感興趣的小伙伴們可以參考一下2016-06-06android中intent傳遞list或者對(duì)象的方法
這篇文章主要介紹了android中intent傳遞list或者對(duì)象的方法,分析羅列了常用的幾種方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01Android 列表倒計(jì)時(shí)的實(shí)現(xiàn)的示例代碼(CountDownTimer)
本篇文章主要介紹了Android 列表倒計(jì)時(shí)的實(shí)現(xiàn)的示例代碼(CountDownTimer),具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09webview添加參數(shù)與修改請(qǐng)求頭的user-agent實(shí)例
這篇文章主要介紹了webview添加參數(shù)與修改請(qǐng)求頭的user-agent實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03android LabelView實(shí)現(xiàn)標(biāo)簽云效果
這篇文章主要為大家詳細(xì)介紹了android LabelView實(shí)現(xiàn)標(biāo)簽云效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android Cocos Creator游戲開發(fā)平臺(tái)打包優(yōu)化實(shí)現(xiàn)方案
Cocos Creator是一款輕量、高效、免費(fèi)開源的跨平臺(tái)游戲引擎,同時(shí)也是實(shí)時(shí)3D內(nèi)容創(chuàng)作平臺(tái),不僅支持2D、3D的游戲開發(fā),同時(shí)在HMI、IoT、XR、虛擬人偶等領(lǐng)域,均可提供一套完善的行業(yè)解決方案2022-11-11Android多線程斷點(diǎn)續(xù)傳下載實(shí)現(xiàn)代碼
這篇文章主要介紹了Android多線程斷點(diǎn)續(xù)傳下載實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11