颤振-如何访问ListView.builder中的一个元素?
我有一个列表视图,其中包含从对象列表(提供)创建的元素。每个报价都有自己的详细信息。我想在点击后更改图标(颤振-如何访问ListView.builder中的一个元素?,listview,flutter,mobile,dart,element,Listview,Flutter,Mobile,Dart,Element,我有一个列表视图,其中包含从对象列表(提供)创建的元素。每个报价都有自己的详细信息。我想在点击后更改图标(\u isFavIcon)。但当我点击它时,它会改变列表中的每个元素(每个图标)。 同样在更改图标后,我想将它们添加到另一个列表中。但那是以后的事了 下面是它的外观: 也许我应该使用ListView而不是ListView.builder 这是我的密码: class OfferList extends StatefulWidget { final List<Offer> off
\u isFavIcon
)。但当我点击它时,它会改变列表中的每个元素(每个图标)。
同样在更改图标后,我想将它们添加到另一个列表中。但那是以后的事了
下面是它的外观:
也许我应该使用ListView
而不是ListView.builder
这是我的密码:
class OfferList extends StatefulWidget {
final List<Offer> offers;
OfferList(this.offers);
@override
_OfferListState createState() => _OfferListState();
}
class _OfferListState extends State<OfferList> {
bool isPressed = true;
Icon _isFavIcon = new Icon(
Icons.favorite_border,
color: Colors.red,
);
@override
Widget build(BuildContext context) {
final mediaQuery = MediaQuery.of(context);
_addToFavorites() {
_isFavIcon = new Icon(Icons.favorite, color: Colors.red);
}
return Container(
height: 700,
child: ListView.builder(
itemBuilder: (ctx, index) {
return Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
Colors.white,
Colors.lightBlueAccent.withOpacity(0.2)
])),
width: mediaQuery.size.width,
child: Card(
child: Row(
children: <Widget>[
Container(
margin: EdgeInsets.symmetric(
vertical: 15,
horizontal: 15,
),
child: Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.3),
spreadRadius: 1.2,
blurRadius: 7,
),
],
),
child: Image.asset(widget.offers[index].flag),
),
padding: EdgeInsets.all(5),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 150,
child: Text(
widget.offers[index].title,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
),
Text(
'From: ' +
DateFormat.yMMMd()
.format(widget.offers[index].dateFrom),
style: TextStyle(
color: Colors.grey,
),
),
Text(
'To: ' +
DateFormat.yMMMd()
.format(widget.offers[index].dateTo),
style: TextStyle(
color: Colors.grey,
),
),
],
),
Padding(
padding: EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
RaisedButton(
color: Colors.lightBlueAccent,
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0),
),
child: Text('Open'),
onPressed: () {},
),
Container(
padding: EdgeInsets.only(left: 10),
child: InkWell(
onTap: () {
setState(() {
_addToFavorites();
});
},
child: _isFavIcon,
),
),
],
),
),
],
),
),
),
],
);
},
itemCount: widget.offers.length,
),
);
}
}
类扩展了StatefulWidget{
最终报价清单;
OfferList(this.offers);
@凌驾
_OfferListState createState();
}
类_OfferListState扩展状态{
bool isPressed=true;
图标_isFavIcon=新图标(
图标。最喜爱的边框,
颜色:颜色,红色,
);
@凌驾
小部件构建(构建上下文){
final mediaQuery=mediaQuery.of(上下文);
_addToFavorites(){
_isFavIcon=新图标(Icons.favorite,颜色:Colors.red);
}
返回容器(
身高:700,
子项:ListView.builder(
itemBuilder:(ctx,索引){
返回堆栈(
儿童:[
容器(
装饰:盒子装饰(
梯度:线性梯度(
开始:Alignment.topRight,
结束:对齐。左下角,
颜色:[
颜色,白色,
颜色。浅蓝色调。不透明度(0.2)
])),
宽度:mediaQuery.size.width,
孩子:卡片(
孩子:排(
儿童:[
容器(
边距:边缘组。对称(
垂直:15,
横向:15,
),
子:容器(
装饰:盒子装饰(
boxShadow:[
箱形阴影(
颜色:颜色。灰色。不透明度(0.3),
扩展半径:1.2,
半径:7,
),
],
),
子项:Image.asset(widget.offers[index].flag),
),
填充:边缘设置。全部(5),
),
纵队(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
容器(
宽度:150,
子:文本(
widget.offers[index].title,
样式:TextStyle(
尺寸:16,
fontWeight:fontWeight.bold,
),
),
),
正文(
“发件人:”+
DateFormat.yMMMd()
.format(widget.offers[index].dateFrom),
样式:TextStyle(
颜色:颜色。灰色,
),
),
正文(
“致:”+
DateFormat.yMMMd()
.format(widget.offers[index].dateTo),
样式:TextStyle(
颜色:颜色。灰色,
),
),
],
),
填充物(
填充:边缘设置。全部(8.0),
孩子:排(
儿童:[
升起的按钮(
颜色:Colors.lightBlueAccent,
textColor:Colors.white,
形状:圆形矩形边框(
边界半径:新边界半径。圆形(30.0),
),
子项:文本('Open'),
按下:(){},
),
容器(
填充:仅限边缘设置(左:10),
孩子:InkWell(
onTap:(){
设置状态(){
_addToFavorites();
});
},
孩子:_isFavIcon,
),
),
],
),
),
],
),
),
),
],
);
},
itemCount:widget.offers.length,
),
);
}
}
您实际上正在覆盖所有项目的\u isFavIcon
您需要向优惠
模型添加名为isFavorite
的属性,并在onTap方法上切换isFavorite
属性
这里有一个完整的工作示例
提供型号
class Offer {
// other attributes
bool isFavorite = false;
}
提供列表小部件
class OfferList extends StatefulWidget {
final List<Offer> offers;
OfferList(this.offers);
@override
_OfferListState createState() => _OfferListState();
}
class _OfferListState extends State<OfferList> {
bool isPressed = true;
Icon _isNotFavIcon = new Icon(
Icons.favorite_border,
color: Colors.red,
);
Icon _isFavIcon = new Icon(
Icons.favorite,
color: Colors.red,
);
List<Offer> _offers;
@override
void initState() {
_offers = widget.offers;
super.initState();
}
@override
Widget build(BuildContext context) {
final mediaQuery = MediaQuery.of(context);
return Container(
height: 700,
child: ListView.builder(
itemBuilder: (ctx, index) {
return Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
Colors.white,
Colors.lightBlueAccent.withOpacity(0.2)
])),
width: mediaQuery.size.width,
child: Card(
child: Row(
children: <Widget>[
Container(
margin: EdgeInsets.symmetric(
vertical: 15,
horizontal: 15,
),
child: Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.3),
spreadRadius: 1.2,
blurRadius: 7,
),
],
),
child: Image.asset(_offers[index].flag),
),
padding: EdgeInsets.all(5),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 150,
child: Text(
_offers[index].title,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
),
Text(
'From: ' +
DateFormat.yMMMd()
.format(_offers[index].dateFrom),
style: TextStyle(
color: Colors.grey,
),
),
Text(
'To: ' +
DateFormat.yMMMd()
.format(_offers[index].dateTo),
style: TextStyle(
color: Colors.grey,
),
),
],
),
Padding(
padding: EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
RaisedButton(
color: Colors.lightBlueAccent,
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0),
),
child: Text('Open'),
onPressed: () {},
),
Container(
padding: EdgeInsets.only(left: 10),
child: InkWell(
onTap: () {
setState(() {
_offers[index].isFavorite =
!_offers[index].isFavorite;
});
},
child: _offers[index].isFavorite
? _isFavIcon
: _isNotFavIcon,
),
),
],
),
),
],
),
),
),
],
);
},
itemCount: _offers.length,
),
);
}
}
类扩展了StatefulWidget{
最终报价清单;
OfferList(this.offers);
@凌驾
_OfferListState createState();
}
类_OfferListState扩展状态{
bool isPressed=true;
图标_isNotFavIcon=新图标(
图标。最喜爱的边框,
颜色:颜色,红色,
);
图标_isFavIcon=新图标(
我的最爱,
颜色:颜色,红色,
class OfferList extends StatefulWidget {
final List<Offer> offers;
OfferList(this.offers);
@override
_OfferListState createState() => _OfferListState();
}
class _OfferListState extends State<OfferList> {
bool isPressed = true;
List<Offer> _favOffers = [];
@override
Widget build(BuildContext context) {
final mediaQuery = MediaQuery.of(context);
void _toggleFavorite(Offer offer) {
if (_favOffers.contains(offer)) {
_favOffers.remove(offer);
} else {
_favOffers.add(offer);
}
}
return Container(
height: 700,
child: ListView.builder(
itemBuilder: (ctx, index) {
return Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
Colors.white,
Colors.lightBlueAccent.withOpacity(0.2)
])),
width: mediaQuery.size.width,
child: Card(
child: Row(
children: <Widget>[
Container(
margin: EdgeInsets.symmetric(
vertical: 15,
horizontal: 15,
),
child: Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.3),
spreadRadius: 1.2,
blurRadius: 7,
),
],
),
child: Image.asset(widget.offers[index].flag),
),
padding: EdgeInsets.all(5),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 150,
child: Text(
widget.offers[index].title,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
),
Text(
'From: ' +
DateFormat.yMMMd()
.format(widget.offers[index].dateFrom),
style: TextStyle(
color: Colors.grey,
),
),
Text(
'To: ' +
DateFormat.yMMMd()
.format(widget.offers[index].dateTo),
style: TextStyle(
color: Colors.grey,
),
),
],
),
Padding(
padding: EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
RaisedButton(
color: Colors.lightBlueAccent,
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0),
),
child: Text('Open'),
onPressed: () {},
),
Container(
padding: EdgeInsets.only(left: 10),
child: InkWell(
onTap: () {
setState(() {
_toggleFavorite(widget.offers[index]);
});
},
child:
(_favOffers.contains(widget.offers[index]))
? Icon(
Icons.favorite,
color: Colors.red,
)
: Icon(
Icons.favorite_border,
color: Colors.black,
),
),
),
],
),
),
],
),
),
),
],
);
},
itemCount: widget.offers.length,
),
);
}
}