【问题标题】:How to make Listview.builder start from the current day in Flutter如何让 Listview.builder 在 Flutter 中从当天开始
【发布时间】:2021-01-24 08:50:48
【问题描述】:

大家好,我有一个显示工作日的 Listview.builder,我想让这个 listview.builder 始终从突出显示的当前日期开始。 这是我的代码:

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),
                                  ),
                                ),
                              ),
                            ),

如您所见,当前日期由now.weekday == index + 1 && weekIndex == 0控制

我想我需要向 Listviewbuilder 添加一个控制器来实现我的目的,但我不知道该怎么做。 我还需要考虑星期天什么时候在列表中只剩下一天。 任何输入?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您可以使用add 方法获取dateTime,从而获得Duration

    希望以下代码对您有所帮助:

    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,
    

    【讨论】:

    • 好的,但问题是如何将 Listview.builder 作为当天列表的第一项?
    • 0 index 是您的当天,我们通过它的索引将持续时间添加到当天。我错了吗?
    • 那么你的意思是添加滚动控制器并作为initialScrollOffset使用0
    【解决方案2】:

    看看这个indexed_list_view

    类似于 ListView,但允许您以编程方式按索引跳转到任何项目。索引跳转会立即发生,无论您是否有数百万个项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-28
      • 2019-12-21
      • 2017-03-25
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      相关资源
      最近更新 更多