简体   繁体   English

如何在 flutter 的一行中获取选定的索引

[英]How do I get the selected index in a Row in flutter

Date selector日期选择器

I want to build a date selector for user to select the date they want to book a class.我想为用户构建一个日期选择器 select 他们想要预订 class 的日期。 So far I have coded the logic to generate next 8days and display it in a row, but I have trouble getting the selected index when user tap a date.到目前为止,我已经编写了生成下一个 8 天并将其连续显示的逻辑,但是当用户点击日期时,我无法获取选定的索引。 How do I achieve that?我该如何做到这一点?

Thank you very much.非常感谢。

class TimeSlot extends StatelessWidget {
  List<Map<String, String>> get upComingDay {
    return List.generate(8, (index) {
      final weekDay = DateTime.now().add(
        Duration(days: index),
      );
      return {
        'day': DateFormat.E().format(weekDay).substring(0, 2),
        'date': DateFormat.d().format(weekDay),
      };
    });
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: upComingDay.map((data) {
        return Column(
          children: <Widget>[
            Text(
              data['day'],
              style: TextStyle(
                color: Color.fromRGBO(30, 41, 51, 0.5),
              ),
            ),
            SizedBox(
              height: 7,
            ),
            GestureDetector(
              onTap: () {},
              child: CircleAvatar(
                backgroundColor: Color.fromRGBO(240, 242, 245, 1),
                radius: 14,
                child: CircleAvatar(
                  backgroundColor: Colors.white,
                  child: Text(
                    data['date'],
                    style: TextStyle(color: Colors.black, fontSize: 12),
                  ),
                  radius: 13,
                ),
              ),
            ),
          ],
        );
      }).toList(),
    );
  }
}

Try using this trick:尝试使用这个技巧:

Replace代替

.map()

with

.asMap().entries.map()

For example:例如:

children: upComingDay.asMap().entries.map((dataEntry) {
        return Column(
          children: <Widget>[
            Text(
              dataEntry.value['day'],
              style: TextStyle(
                color: Color.fromRGBO(30, 41, 51, 0.5),
              ),
            ),
            SizedBox(
              height: 7,
            ),
            GestureDetector(
              onTap: () {
                // Whatever you wanna use the index for, refer as dataEntry.key for index
              },
              child: CircleAvatar(
                backgroundColor: Color.fromRGBO(240, 242, 245, 1),
                radius: 14,
                child: CircleAvatar(
                  backgroundColor: Colors.white,
                  child: Text(
                    dataEntry.value['date'],
                    style: TextStyle(color: Colors.black, fontSize: 12),
                  ),
                  radius: 13,
                ),
              ),
            ),
          ],
        );
      }).toList(),

Does that accomplish what you're looking for?这是否实现了您正在寻找的东西?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM