【发布时间】:2020-11-29 16:18:06
【问题描述】:
我是飞镖编程的新手。 我发现一个关于我的问题的错误,即“我创建了一个模型类和一个列表,模型类有一个 Color 类型的成员,然后在 main.dart ,我想在 ListView.builder 中显示我的模型数据列表,但是当在容器的单独小部件中时,其他一切正常但颜色属性给出错误,我试图更改类型索引参数,但错误仍然存在。"
代码如下:
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,并提供颜色的 int 类型值(如 0xFFFFFF),那么错误就解决了,但我的问题是我是否想使用类似的材料颜色我上面用过,怎么办。 谢谢
【问题讨论】: