参数类型"颜色"不能分配给参数类型'int'



我是飞镖编程的新手。我发现我的问题有一个错误,那就是关于";我创建了一个模型类和一个列表,模型类有一个类型为Color的成员,然后在main.dart中,我想在ListView.builder显示我的模型数据列表,但当在容器的单独小部件中时,其他一切都很好,但Color属性出现错误,我试图更改索引参数的类型,但错误仍然存在">

这是代码:

import 'package:flutter/material.dart';
class ProductModel {

ProductModel(
this.title,
this.name,
this.image,
this.color
);
final String title;
final String name;
final String image;
final Color color;
}
final modelProduct = <ProductModel>[
ProductModel(
"titile1",
"name1",
"https://image.freepik.com/free-vector/multitasking-concept-illustration-character_23-2148403716.jpg",
Colors.pink.withOpacity(0.4),
),
ProductModel(
"titile2",
"name2",
"https://image.freepik.com/free-vector/people-putting-puzzle-pieces-together_52683-28610.jpg",
Colors.blue.withOpacity(0.4),
),
ProductModel(
"titile3",
"name3",
"https://image.freepik.com/free-vector/people-using-online-apps-set_74855-4457.jpg",
Colors.yellow.withOpacity(0.4),
),
]

main.dart我跳过了第一次颤振的锅炉板代码,只是复制了我关心的主要问题

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
child: ListView.builder(
itemCount: modelProduct.length,
itemBuilder: (context, index) {
return createItem(context, index);
})),
);
}
}
Widget createItem(BuildContext context, index) {
return Container(
height: MediaQuery.of(context).size.height * 0.3,
width: MediaQuery.of(context).size.width,
child: Text(modelProduct[index].title),
color: Color(modelProduct[index].color),
);
}

问题在color:color(modelProduct[index].color(此行,错误为

The argument type 'Color' can't be assigned to the parameter type 'int'.

但我知道,如果我在模型类中将颜色的类型转换为int,并提供像0xFFFFFF这样的颜色的int类型值,那么错误就解决了,但我的问题是,如果我想使用像上面使用的材料颜色,如何处理它。谢谢

您可以直接使用color: modelProduct[index].color

代码段

return Container(
height: MediaQuery.of(context).size.height * 0.3,
width: MediaQuery.of(context).size.width,
child: Text(modelProduct[index].title),
color: modelProduct[index].color,
);

尝试以下代码:

Container( 
height: MediaQuery.of(context).size.height* 0.3,
width: MediaQuery.of(context).size.width,
child: Text(modelProduct[index].title),
color: modelProduct[index].color
);

以下适用于我

return Container(
height: MediaQuery.of(context).size.height * 0.3,
width: MediaQuery.of(context).size.width,
child: Text(modelProduct[index].title),
color: modelProduct[index].color.value,
);

它采用颜色的int值

相关内容

最新更新