简体   繁体   English

如何更改 flutter Dart 的 ListView 中的默认文本颜色?

[英]how to change default text color in ListView in flutter Dart?

I want to change the text color of the ListView with along with the Checkbox but can't change the text color dynamically with the ListView's onselected item?我想与 Checkbox 一起更改ListView的文本颜色,但不能使用ListView's onselected 项动态更改文本颜色? I also add the color in the set State option but cant find the result I was lookin for.我还在设置 State 选项中添加了颜色,但找不到我正在寻找的结果。

here is my code:这是我的代码:

ListView(
        children: [
          Text(
            '''   Categories''',
            style: TextStyle(
              color: Color(0xff181725),
              fontSize: 24,
              fontFamily: "Gilroy",
              fontWeight: FontWeight.normal,
            ),
          ),
          SizedBox(height: 13),
          ...checkBoxListOne.map(
            (item) => ListTile(
              onTap: () => onAllClicked(item),
              leading: Checkbox(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(5),
                ),
                side: BorderSide(
                  color: Color(0xffC2C2C2),
                  width: 2,
                ),
                activeColor: Color(0xff53B175),
                value: item.value,
                onChanged: (value) => onAllClicked(item),
              ),
              title: Text(
                item.title!,
                style: TextStyle(
                  color: Color(0xff181725),
                ),
              ),
            ),
          ),
          SizedBox(height: 40),
          Text(
            '''   Brand''',
            style: TextStyle(
              color: Color(0xff181725),
              fontSize: 24,
              fontFamily: "Gilroy",
              fontWeight: FontWeight.normal,
            ),
          ),
          SizedBox(height: 13),
          ...checkBoxListTwo.map(
            (item) => ListTile(
              onTap: () => onAllClicked(item),
              leading: Checkbox(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(5),
                ),
                side: BorderSide(
                  color: Color(0xffC2C2C2),
                  width: 2,
                ),
                activeColor: Color(0xff53B175),
                value: item.value,
                onChanged: (value) => onAllClicked(item),
              ),
              title: Text(item.title!),
            ),
          ),
        ],
      ),

and here is the setState part:这是setState部分:

onAllClicked(CheckBoxModal ckbItem) { setState(() { ColoredBox( color: Color(0xff53B175), ); ckbItem.value =.ckbItem;value; }); }

You should add a field isSelected to the item objects in your checkBoxListOne list, and set/reset that field in the onTap of the item ( item.isSelected =.item.isSelected ), then call setState(){} .您应该在checkBoxListOne列表中的item对象中添加一个字段isSelected ,并在项目的onTap中设置/重置该字段( item.isSelected =.item.isSelected ),然后调用setState(){} When you render the item , instead of activeColor: Color(0xff53B175) do something like activeColor: item.isSelected? Color(0xff53B175): Color(0xffffffff)当您渲染item时,而不是activeColor: Color(0xff53B175)做类似activeColor: item.isSelected? Color(0xff53B175): Color(0xffffffff) activeColor: item.isSelected? Color(0xff53B175): Color(0xffffffff) . activeColor: item.isSelected? Color(0xff53B175): Color(0xffffffff)

As a result of this change, the list is re-rendered whenever an item is tapped, and the color of the item when rendered is dependent on its 'tapped or not' state.作为此更改的结果,每当点击项目时,都会重新渲染列表,并且渲染时项目的颜色取决于其“是否点击”state。

If you cannot (or don't want to) add a field, you can create a separate bool array of the same length as your checkBoxListOne list and use that to keep track of which item is selected.如果您不能(或不想)添加字段,则可以创建一个与checkBoxListOne列表长度相同的单独bool数组,并使用它来跟踪选择了哪个项目。

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

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