Flutter 如何在颤振中选择后更改ListTile的背景色
我在Flatter中创建了一个ListView,但是现在我在这个ListView中有一些可以选择的ListTile。选择后,我希望背景颜色更改为我选择的颜色。我不知道怎么做。在文档中,他们提到ListTile具有属性样式。但是,当我尝试添加该属性时(如下面代码的最后三行),该style属性下面会有一条弯曲的红线,编译器告诉我命名参数“style”没有定义。您可以用容器包装ListTile并更改其颜色。下面是一个使用该代码和更改颜色的方法的示例:Flutter 如何在颤振中选择后更改ListTile的背景色,flutter,Flutter,我在Flatter中创建了一个ListView,但是现在我在这个ListView中有一些可以选择的ListTile。选择后,我希望背景颜色更改为我选择的颜色。我不知道怎么做。在文档中,他们提到ListTile具有属性样式。但是,当我尝试添加该属性时(如下面代码的最后三行),该style属性下面会有一条弯曲的红线,编译器告诉我命名参数“style”没有定义。您可以用容器包装ListTile并更改其颜色。下面是一个使用该代码和更改颜色的方法的示例: import 'package:JAPANESE/
import 'package:JAPANESE/data/hiraganaall.dart';
import 'package:flutter/material.dart';
class HiraganaPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Hiragana",
style: TextStyle(
color: Colors.white,
),
),
centerTitle: true,
backgroundColor: Colors.blueAccent,
),
body: ListView.separated(
separatorBuilder: (context, index) {
return Divider(
thickness: 3.0,
height: 1.0,
);
},
itemCount: data.length,
itemBuilder: (context, index) {
return Container(
decoration: BoxDecoration(color: Colors.grey[300]),
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.white,
radius: 28,
child: Image.network(
data[index]["writing"],
),
),
title: Text(
"Text: " + data[index]["key"],
style: TextStyle(
fontFamily: "Merienda",
fontSize: 18.22,
),
),
subtitle: Text(
"Reading: " + data[index]["reading"],
style: TextStyle(
fontFamily: "Merienda",
fontSize: 18.22,
),
),
),
);
},
),
);
}
}
class Issue66349460扩展StatefulWidget{
@凌驾
_Issue66349460State createState()=>_Issue66349460State();
}
类_Issue66349460State扩展状态{
列表项=[
项目(标题:“项目1”,颜色:颜色。红色),
项目(标题:“项目2”,颜色:Colors.blue),
项目(标题:“项目3”,颜色:Colors.green),
];
@凌驾
小部件构建(构建上下文){
返回ListView.builder(
itemCount:items.length,
itemBuilder:(上下文,索引){
返回容器(
颜色:项目[索引]。颜色,
孩子:ListTile(
标题:文本(项目[索引].标题),
onTap:()=>changeListTileColor(项目[索引]),
),
);
}
);
}
作废更改列表TileColor(项目){
设置状态(){
item.color=Colors.deepPurple;
});
}
}
类项目{
字符串标题;
颜色;
项({@required this.title,this.color});
}
这是否回答了您的问题?
class Issue66349460 extends StatefulWidget {
@override
_Issue66349460State createState() => _Issue66349460State();
}
class _Issue66349460State extends State<Issue66349460> {
List<Item> items = [
Item(title: 'Item 1', color: Colors.red),
Item(title: 'Item 2', color: Colors.blue),
Item(title: 'Item 3', color: Colors.green),
];
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return Container(
color: items[index].color,
child: ListTile(
title: Text(items[index].title),
onTap: () => changeListTileColor(items[index]),
),
);
}
);
}
void changeListTileColor(Item item){
setState(() {
item.color = Colors.deepPurple;
});
}
}
class Item {
String title;
Color color;
Item({@required this.title, this.color});
}