Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

Insert alternating row of data into ListView in Flutter/Dart

I have a ListView that displays a list of dates. What I want to do is insert a row of data in between each of these rows, that shows the time difference between the dates. Such as:

|Title: Red Date: 21/01/2021 4:45pm|
|5 hours 10 mins|
|Title: Green Date: 21/01/2021 10:25pm|
|55 mins|
|Title: Blue Date: 21/01/2021 9:30am|
         

Does anyone know how this can be achieved?

Thanks

question from:https://stackoverflow.com/questions/65848127/insert-alternating-row-of-data-into-listview-in-flutter-dart

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Coded this.

List<DateTime> dates = [DateTime.now(), DateTime.utc(1998, 2, 11)]; 

Scaffold(
      body: Center(
        child: ListView.builder(
          itemCount: dates.length,
          itemBuilder: (context, index) {
            return Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text('${DateFormat.yMd().add_jm().format(dates[index])}'),
              ),

                if(index < dates.length - 1)
               Padding(
                 padding: const EdgeInsets.all(8.0),
                 child: Text('${dates[index].difference(dates[index + 1]).inHours} hours  ${dates[index].difference(dates[index + 1]).inMinutes.remainder(60)} minute'),
               )

              ],
            );
          },
        ),
      ),
    );

Note: Refer to this document for details on DateTime formatting. https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...