我想在单击 onTap 时更改作为 ListView 子级的自定义 ListTile 的颜色,并将其他子级颜色设置为默认颜色?

I want to change the color of CustomListTile which is child of ListView when onTap is clicked, and setting other children color into default one?

在 Drawer 中,在 listview 中想要在单击 onTap 时更改 CustomListTile 的颜色并将所有其他子项的颜色设置为默认值?

class CustomListTile extends StatelessWidget {

  final Color itemContainerColor;

  const CustomListTile({
    //deafult color is Colors.white
    this.itemContainerColor= Colors.white,
  });

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: (){},

    child: Container(
    margin: EdgeInsets.symmetric(vertical: 4),
    padding: EdgeInsets.symmetric(vertical: 5, horizontal: 16),
    width: 150,
    decoration: BoxDecoration(

        color: itemContainerColor,
       )
    child: 
        Text(
          "ListTile 1",
          style: TextStyle(
              fontSize: 20,
              color: Colors.green,
              fontWeight: FontWeight.w600),
        ),

     ),
   ));
  }
 }

Aakash 只需在选项卡上使用一个布尔值,例如 colorChange = true 当按钮被单击时,其他手在容器的子部件中...

     colorChange ? 
       Text(
         "ListTile 1",
         style: TextStyle(
             fontSize: 20,
             color: Colors.red, // which color you need to use
             fontWeight: FontWeight.w600),
       ): Text(
         "ListTile 2",
         style: TextStyle(
             fontSize: 20,
             color: Colors.green,
             fontWeight: FontWeight.w600),
       )

试试这个。

    class ChangeListViewBGColor extends StatefulWidget {
    _ChangeListViewBGColorState createState() => _ChangeListViewBGColorState();
    }

    class _ChangeListViewBGColorState extends State<ChangeListViewBGColor> {
    final List<String> _listViewData = [
        "Item 1",
        "Item 2",
        "Item 3",
        "Item 4",
        "Item 5",
        "Item 6",
        "Item 7",
        "Item 8",
    ];

    int _selectedIndex = 0;

    _onSelected(int index) {
        setState(() => _selectedIndex = index);
    }

    @override
    Widget build(BuildContext context) {
        return Scaffold(
        appBar: AppBar(
            title: Text('BG change'),
        ),
        body: ListView.builder(
            itemCount: _listViewData.length,
            itemBuilder: (context, index) => Container(
            color: _selectedIndex != null && _selectedIndex == index
                ? Colors.red
                : Colors.white,
            child: ListTile(
                title: CustomListTile(_listViewData[index]),
                onTap: () => _onSelected(index),
            ),
            ),
        ),
        );
    }
    }

    class CustomListTile extends StatelessWidget {

    var titleName;

    CustomListTile(this.titleName);

    @override
    Widget build(BuildContext context) {
        return InkWell(
        child: Container(
            child: Text(
                this.titleName,
                style: TextStyle(
                    fontSize: 20, color: Colors.green, fontWeight: FontWeight.w600),
            ),
            margin: EdgeInsets.symmetric(vertical: 4),
            padding: EdgeInsets.symmetric(vertical: 5, horizontal: 16),
            width: 150,

        )
        );
    }
    }

Note: One can go according to @Amit Prajapati 's solution/logic, but if your use-case is going to get complex over time then I would recommend going as per the below solution.

每当您需要更改元素列表中特定元素的 属性 时,请使用具有与 属性 接受的值相同数据类型的值列表长度与主列表中的元素数相同。

在您的情况下,您需要在用户单击特定 ListTile 时更改它的颜色。所以声明一个颜色列表。

List<Color> tileColors;

现在,在主小部件的 initState() 中使用默认值初始化列表(取决于主小部件的长度)。

for(int i=0;i<items.length;i++) tileColors.add(defaultColor);

现在,在使用构建器函数时,使用 tileColors[index]、

设置列表的每个项目 (ListTile)

(我将使用 ListView.builder)

ListView.builder(
itemCount: items.length,
 itemBuilder: (BuildContext context, int index) {
     return new Container(
       color: tileColors[index],
       child: ListTile([...]),
);
    }
)

现在只要用户点击 ListTile 的 属性(颜色)值就可以设置状态()。

ListView.builder(
  itemCount: items.length,
  itemBuilder: (BuildContext context, int index) {
     return new Container(
       color: tileColors[index],
       child: ListTile(
         onTap: (){
            setState((){
          for(int i=0;i<item.length;i++) tileColors[i] = defaultColor;

          tileColors[index] = selectedColor; // selectedColor is the color which you wish to change to.

         // Note: You can add your own conditions over. e.g. If you want the first tile to be of a specific color and the rest should be different color while the user taps.
});
}
),
   );
    }
)

Tip: You can use AnimatedContainer instead of Container widget to create a smooth transition when the user taps. Set the duration parameter to Duration(milliseconds: 300).