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
382 views
in Technique[技术] by (71.8m points)

How to make Listview.builder start from the current day in Flutter

Hello guys I have a Listview.builder which display the week days, and I want to make this listview.builder start always from the current days which is highlighted. This is my Code:

ListView.builder(
                  scrollDirection: Axis.horizontal,
                  itemCount: 7,
                  itemBuilder: (BuildContext context, int index) {
                    DateTime now = DateTime.now();
                    int milliseconds = now.millisecondsSinceEpoch -
                        (now.weekday - 1) * 24 * 60 * 60 * 1000 +
                        weekIndex * 7 * 24 * 60 * 60 * 1000 +
                        index * 24 * 60 * 60 * 1000;
                    DateTime dayDateTime =
                    DateTime.fromMillisecondsSinceEpoch(milliseconds);
                    int monthIndex = dayDateTime.month - 1;
                    List months = [
                      'Gennaio',
                      'Febbraio',
                      'Marzo',
                      'Aprile',
                      'Maggio',
                      'Giugno',
                      'Luglio',
                      'Agosto',
                      'Settembre',
                      'Ottobre',
                      'Novembre',
                      'Dicembre'
                    ];
                    List weekdays = [
                      'Lunedì',
                      'Martedì',
                      'Mercoledì',
                      'Giovedì',
                      'Venerdì',
                      'Sabato',
                      'Domenica'
                    ];
                    String month = months[monthIndex];
                    String day = dayDateTime.day.toString();
                    String weekday = weekdays[dayDateTime.weekday - 1];
                    String date = weekday + ' ' + day + ' ' + month;
                    return Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Column(
                          children: [
                            Container(
                              height: 50,
                              width: MediaQuery.of(context).size.width*0.50,
                              child: Card(
                                color: now.weekday == index + 1 && weekIndex == 0 ? Colors.orange[900]: Colors.orange,
                                child: Center(
                                  child: Text(
                                    date,
                                    style: TextStyle(
                                        fontSize: 13, color: Colors.white),
                                  ),
                                ),
                              ),
                            ),

as you can see the current days is controlled by now.weekday == index + 1 && weekIndex == 0

I guess I need to add a controller to Listviewbuilder to achieve my purpose but I dont know how to do. And I need also to consider when is Sunday it remaine just one day in the list. Any Input?

question from:https://stackoverflow.com/questions/65868635/how-to-make-listview-builder-start-from-the-current-day-in-flutter

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

1 Reply

0 votes
by (71.8m points)

You can use add method for dateTime that gets a Duration.

Hope below code helps you:

DateTime dateTime = DateTime.now();
dateTime.add(Duration(days: index));
//
//
String month = months[dateTime.month - 1];
String day = dateTime.day.toString();
String weekday = weekdays[dateTime.weekday - 1];
String date = weekday + ' ' + day + ' ' + month;
//
//
color: index == 0 ? Colors.orange[900]: Colors.orange,

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

...