您好,我是新手,我需要帮助,我有下一个代码,但我想从单击的项目中获取数据,我需要获取卡的featureString:变量,为此,我正在使用return InkWell(onTap:(){但我无法访问数据。如果有人可以帮助我!
先感谢您!
这是我的代码:
class Features extends StatelessWidget {
List<_FeaturePhoto> _feature() {
return [
_FeaturePhoto(imageURL: '',
subtitle: 'I love cat',
featureString: 'Cat'),
_FeaturePhoto(imageURL: '',
featureString: 'bird'),
_FeaturePhoto(imageURL: '',
title: 'Rabit',
featureString: 'rabit'),
];
}
@override
Widget build(BuildContext context) {
**return InkWell (
**onTap: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => NewScreen()));
//here I need to get the data from the card item that I clicked
},****
child: Container(
height: 320,
width: 220,
child: ListView(
scrollDirection: Axis.horizontal,
padding: EdgeInsets.all(10.0),
children: _feature().map<Widget> ( (photo) {
return _FeatureGridItem(featurePhoto: photo);
//Feature(photo);
}).toList()
)
));
}
}
class _FeaturePhoto{
_FeaturePhoto({
this.imageURL,
this.title,
this.subtitle,
this.featureString
});
final String imageURL;
final String title;
final String subtitle;
final String featureString;
}
class _FeatureGridItem extends StatelessWidget {
_FeatureGridItem({
Key key,
@required this.featurePhoto
}) : super ( key: key);
final _FeaturePhoto featurePhoto;
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget> [
Card(
semanticContainer: true,
clipBehavior: Clip.antiAliasWithSaveLayer,
child: Stack(
children: <Widget>[
Image.network(featurePhoto.imageURL,
width: 230,
height: 230,
fit: BoxFit.cover),
Positioned(
top: 175,
left: 10,
child: Container(
height: 25,
width: 100,
child: Center(
child: Text( featurePhoto.featureString,
style: TextStyle(color: Colors.white, fontSize: 18.0, fontWeight: FontWeight.bold),
),
),
)
)
],
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)
),
elevation: 5,
margin: EdgeInsets.all(10),
),
]
);
}
}
Listview具有onTap事件,它将准确地为您提供所需的内容。下面的示例代码:
I've taken the above code snippet from here. which has a complete code you might want to refer.