【问题标题】:The argument type 'Color' can't be assigned to the parameter type 'int'参数类型“颜色”不能分配给参数类型“int”
【发布时间】:2020-11-29 16:18:06
【问题描述】:

我是飞镖编程的新手。 我发现一个关于我的问题的错误,即“我创建了一个模型类和一个列表,模型类有一个 Color 类型的成员,然后在 ma​​in.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),
),
]

ma​​in.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),那么错误就解决了,但我的问题是我是否想使用类似的材料颜色我上面用过,怎么办。 谢谢

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

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

    代码sn-p

    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,
    );
    

    【讨论】:

      【解决方案2】:

      试试下面的代码:

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

      【讨论】:

        【解决方案3】:

        以下对我有用

        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值

        【讨论】:

          猜你喜欢
          • 2021-08-15
          • 2021-12-14
          • 2021-08-27
          • 2021-06-05
          • 2022-01-16
          • 2021-08-18
          • 2020-12-03
          • 2021-03-23
          • 1970-01-01
          相关资源
          最近更新 更多