【问题标题】:Flutter - Change color of items on selectFlutter - 在选择时更改项目的颜色
【发布时间】:2022-01-18 14:44:51
【问题描述】:

我正在尝试更改映射列表中所选项目的颜色。以下代码仅更改最后一个选定项目的颜色,但我想更改所有选定项目的颜色。谢谢。

String selectedSport = '';

Wrap(
       
        children: List1.entries.map<Widget>((entry) {
          return FittedBox(
                fit: BoxFit.fill,
                child: Container(
                    margin: const EdgeInsets.all(8.0),
                    padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 7),
                    decoration: BoxDecoration(
                      color: selectedSport == entry.key? AppColor.blueColor.withOpacity(0.3): Colors.white ,
                        borderRadius: BorderRadius.all(Radius.circular(20)),
                        border: Border.all(color: AppColor.blueColor)),
                    child: Center(
                        child: GestureDetector(
                            onTap: () {

                              setState(() {
                                selectedSport = entry.key;  
                              }); 
                            },
                            child: Text(entry.key, style: GoogleFonts.inter(
                            fontSize: 14,
                            fontWeight: FontWeight.w600,
                            color: AppColor.blueColor),
                             ),
                             ),
                             ),
                             ),
              );
        
        }).toList(),
      ),

 Map<String, bool> List1 = {
    'Bubble Football ⚽': false,
    'Futsal ????': false,
    'Beach Volleyball ????': false,
    'Volleyball ????': false,
    'Dodgeball ????': false,
    'Rugby ????': false,
    'American Footbal ????': false,
    'Korftbal ????': false,
    'Netbal ⚾': false,
     'Padel ⚒': false,
    'Table Tennis ????': false,
}

这是我想要的结果:

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    尝试使用列表,因为要选择多张地图,而不仅仅是一张。

    列出选定的运动=[];

        color: selectedSport.contains(entry.key)? 
        AppColor.blueColor.withOpacity(0.3): Colors.white ,
            setState(() {
                   selectedSport.add( entry.key);  
            });
    

    如果有帮助请点赞

    【讨论】:

    • 谢谢伙计。这行得通。上帝保佑你
    猜你喜欢
    • 2022-01-18
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多