gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 09:12:35 26 4
gpt4 key购买 nike

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

),
));
}
}

最佳答案

试试这个。

    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,

)
);
}
}

enter image description here

关于flutter - 我想在单击 onTap 时更改作为 ListView 子级的自定义 ListTile 的颜色,并将其他子级颜色设置为默认颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57753198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com