简体   繁体   中英

error: The argument type 'Color' can't be assigned to the parameter type 'MaterialColor'?

 primaryColor: const Color(0xff090C22),
        colorScheme: ColorScheme.fromSwatch(
          primarySwatch: Color(0xff090C22),
        ),
      ),

I want that specific color to change the appbar color and to be the primary color but instead it showed this error

error: The argument type 'Color' can't be assigned to the parameter type 'MaterialColor'. (argument_type_not_assignable at [bmi_calculator] lib\main.dart:12)

You can use your custom color like this,

First creating your file name where all you color is defined .

import 'package:flutter/material.dart'; 
class Palette { 
  static const MaterialColor kToDark = const MaterialColor( 
    0xffe55f48, // 0% comes in here, this will be color picked if no shade is selected when defining a Color property which doesn’t require a swatch. 
    const <int, Color>{ 
      50: const Color(0xffce5641 ),//10% 
      100: const Color(0xffb74c3a),//20% 
      200: const Color(0xffa04332),//30% 
      300: const Color(0xff89392b),//40% 
      400: const Color(0xff733024),//50% 
      500: const Color(0xff5c261d),//60% 
      600: const Color(0xff451c16),//70% 
      700: const Color(0xff2e130e),//80% 
      800: const Color(0xff170907),//90% 
      900: const Color(0xff000000),//100% 
    }, 
  ); 
} 

Here , we can import and use it as follows,

import 'package:flutter/material.dart'; 
import 'config/palette.dart'; 
void main() { 
runApp(App()); 
} 
class App extends StatelessWidget { 
 @override 
 Widget build(BuildContext context) { 
     return MaterialApp( 
       title: 'Demo for swatch', 
       theme: ThemeData(   
            primarySwatch: Palette.kToDark, //our palette goes here by tapping into the class 
 ), 
 home: YourFirstPage(), 
 ); 
 } 
} 

primarySwatch takes MaterialColor . You can check different between primaryColor and primarySwatch

MaterialColor Defines a single color as well a color swatch with ten shades of the color.
The color's shades are referred to by index. The greater the index, the darker the color. There are 10 valid indices: 50, 100, 200, ..., 900. The value of this color should the same the value of index 500 and shade500.

We need to provide primarySwatch: Colors.black, where Colors.black can use different shades.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM