'_Type'类型不是'Color'类型的子类型

时间:2020-08-14 09:16:20

标签: android flutter dart

我试图将背景颜色设为黑色而不是白色,因此我创建了一个具有其他常用颜色的文件,以便我可以每次调用它们,而不是每次使用它们时都创建它们并称为通用。镖 这是我的代码:

import 'package:flutter/material.dart';
import '../commons.dart';
import '../widgets/title.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: white,
      body:SafeArea(
          child: ListView(
          children:<Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: CustomText(
                      text:"What would you like to order",
                      size:18,
                      ),
                  ),
                  Stack(
                    children:<Widget>[
                    IconButton(icon: Icon(Icons.notifications_none), onPressed: (){}),
                    Positioned(
                        top: 12,
                        right: 12,
                        child: Container(
                        height:10,
                        width:10,
                        decoration: BoxDecoration(
                          color: red,
                          borderRadius: BorderRadius.circular(20),
                           )
                      ),
                    )
                    ],),
                ],
              ),
    //===============my search box ===============
    ///==================start====================  
              SizedBox(height: 5,),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Container(
                  decoration: BoxDecoration(
                    color: white,
                    boxShadow: [
                      BoxShadow(
                        color: grey[300],
                        offset: Offset(1, 1),
                        blurRadius: 4,
                      )
                    ]
                  ) ,
                  child: ListTile(
                    leading: Icon(Icons.search, color: red,),
                    title: TextField(
                      decoration: InputDecoration(
                        hintText: "Find food or Restaurant",
                        border: InputBorder.none
                      )
                    ),
                    trailing: Icon(Icons.filter_list, color: red,),
                    
                  ),
                  ),
              ),
    ///==================end of search box==================== 
    ///
    ///=============The Horizontal scrollview=====================        
          SizedBox(
            height:5,
          ),
          Container(
            height:120,
            child:ListView.builder(
              scrollDirection: Axis.horizontal,
              itemCount: 5,
              itemBuilder:(context, index){
                return  Padding(
                padding: const EdgeInsets.all(8.0),
                child: Column(
                  children: <Widget>[
                    Container(
                      decoration: BoxDecoration(
                        color: white,
                        boxShadow: [
                          BoxShadow(
                            color: red[75],
                            offset: Offset(4, 6),
                            blurRadius: 20,
                          )
                        ]
                      ) ,
                      child: Padding(padding: EdgeInsets.all(4),
                      child:Image.asset("images/salad.png", width:50,),)
                      
                    ),
                    SizedBox(height: 10),
                    CustomText(text: "Salad", size: 13, color: black,),
                  ],
                  ),
              );
              }    
            )
          )
          ],
        ),
      )
    );
  }
}

这是title.dart的代码:

import 'package:flutter/material.dart';
import '../commons.dart';

class CustomText extends StatelessWidget {
  final String text;
  final double size;
  final Color color;
  final FontWeight weight;

  CustomText({@required this.text, this.size, this.color, this.weight});
  @override
  Widget build(BuildContext context) {
    return Text(
      text, style: TextStyle(fontSize: size ?? 16, color: Color ?? black, fontWeight: weight ?? FontWeight.normal),
    );
  }
}

这是调试时的错误信息:

Launching lib\main.dart on PRA LA1 in debug mode...
√ Built build\app\outputs\apk\debug\app-debug.apk.
Error: ADB exited with exit code 1
Performing Streamed Install

adb: failed to install C:\Users\user\FlutterProjects\kariomeals\build\app\outputs\apk\app.apk:
Uninstalling old version...
Connecting to VM Service at ws://127.0.0.1:10689/rREZwD9ZcXc=/ws
I/OpenGLRenderer(25991): Initialized EGL, version 1.4
D/OpenGLRenderer(25991): Swap behavior 2
D/mali_winsys(25991): EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, egl_color_buffer_format *, EGLBoolean) returns 0x3000
D/mali_winsys(25991): EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, egl_color_buffer_format *, EGLBoolean) returns 0x3000
W/InputMethodManager(25991): startInputReason = 1
W/InputMethodManager(25991): startInputReason = 5
I/flutter (25991): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (25991): The following _TypeError was thrown building CustomText(dirty):
I/flutter (25991): type '_Type' is not a subtype of type 'Color'
I/flutter (25991):
I/flutter (25991): The relevant error-causing widget was:
I/flutter (25991):   CustomText 
lib\…\screens\home.dart:23
I/flutter (25991):
I/flutter (25991): When the exception was thrown, this was the stack:
I/flutter (25991): #0      CustomText.build 
package:kariomeals/…/widgets/title.dart:14

1 个答案:

答案 0 :(得分:0)

您正在将 type 辅助颜色设置为文本的color属性,因为以大写字母C开头的Color是该类型。

将其更改为较低的空间应该可以。

return Text(
  text, style: TextStyle(fontSize: size ?? 16, color: color ?? black, fontWeight: weight ?? FontWeight.normal),
);