Flutter:使用多个参数实例化对象
Flutter: Instantiate object with multiple parameters
我在尝试从我的预定义构造函数实例化对象时遇到语法(我猜?)问题。
Category(String kategorieName, Image cover){
kategorieTitel = kategorieName;
kategorieCover = cover;
}
我试图创建一个动态列表,稍后将用于在 ListView.builder 中自动填充构造函数。
List kategorien = [
{
'name' : 'Location1',
'pic' : Asset.Image('assets/img/Locations.jpg')
},
{
'name' : 'Location2',
...
}];
自动填充名称工作正常,但我无法使用相应的图像文件调用构造函数。
child: ListView.builder(
itemCount: kategorien.length,
itemBuilder: (context, index){
return Padding(
padding: EdgeInsets.all(5.0),
child: Category('${kategorien[index]['name']}'),
我尝试过像 Category('${kategorien[index]['name']}', '${kategorien[index][pic]}')
这样的连接
我不知道如何通过列表为构造函数提供图像。
非常感谢您的帮助!
您可以试试下面的代码:
/// Category needs to be a widget so it can be assigned
/// to the child property of the Padding widget.
class Category extends StatelessWidget {
final String kategorieName;
final ImageProvider cover; // AssetImage is part of the ImageProvider interface not Image.
const Category(this.kategorieName, this.cover);
// As Category is a widget you will need to override the build method.
@override
Widget build(BuildContext context) {
return Container(
height: 100, // You can adapt the height depending on your needs
decoration: BoxDecoration(
image: DecorationImage(image: cover),
),
);
}
}
final kategorien = <Map<String, dynamic>>[
{
'name': 'Location1',
'pic': const AssetImage('assets/img/Locations.jpg'), // AssetImage is the correct way of casting your image.
},
{
'name': 'Location2',
'pic': const AssetImage('assets/img/Locations.jpg'),
},
];
class MyImages extends StatelessWidget {
const MyImages({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: kategorien.length,
itemBuilder: (_, index) {
final element = kategorien.elementAt(index);
return Padding(
padding: const EdgeInsets.all(5.0),
child: Category(
'${element['name']}',
element['pic'] as ImageProvider, // You need to cast the pic field as ImageProvider.
),
);
},
);
}
}
我在尝试从我的预定义构造函数实例化对象时遇到语法(我猜?)问题。
Category(String kategorieName, Image cover){
kategorieTitel = kategorieName;
kategorieCover = cover;
}
我试图创建一个动态列表,稍后将用于在 ListView.builder 中自动填充构造函数。
List kategorien = [
{
'name' : 'Location1',
'pic' : Asset.Image('assets/img/Locations.jpg')
},
{
'name' : 'Location2',
...
}];
自动填充名称工作正常,但我无法使用相应的图像文件调用构造函数。
child: ListView.builder(
itemCount: kategorien.length,
itemBuilder: (context, index){
return Padding(
padding: EdgeInsets.all(5.0),
child: Category('${kategorien[index]['name']}'),
我尝试过像 Category('${kategorien[index]['name']}', '${kategorien[index][pic]}')
我不知道如何通过列表为构造函数提供图像。 非常感谢您的帮助!
您可以试试下面的代码:
/// Category needs to be a widget so it can be assigned
/// to the child property of the Padding widget.
class Category extends StatelessWidget {
final String kategorieName;
final ImageProvider cover; // AssetImage is part of the ImageProvider interface not Image.
const Category(this.kategorieName, this.cover);
// As Category is a widget you will need to override the build method.
@override
Widget build(BuildContext context) {
return Container(
height: 100, // You can adapt the height depending on your needs
decoration: BoxDecoration(
image: DecorationImage(image: cover),
),
);
}
}
final kategorien = <Map<String, dynamic>>[
{
'name': 'Location1',
'pic': const AssetImage('assets/img/Locations.jpg'), // AssetImage is the correct way of casting your image.
},
{
'name': 'Location2',
'pic': const AssetImage('assets/img/Locations.jpg'),
},
];
class MyImages extends StatelessWidget {
const MyImages({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: kategorien.length,
itemBuilder: (_, index) {
final element = kategorien.elementAt(index);
return Padding(
padding: const EdgeInsets.all(5.0),
child: Category(
'${element['name']}',
element['pic'] as ImageProvider, // You need to cast the pic field as ImageProvider.
),
);
},
);
}
}