我在Android Studio上的Flutter中迈出的第一步。我的步骤:
这是演示应用程序:
在此应用程序代码的注释中,我们有:
// This is the theme of your application. // Try running your application with "flutter run". You'll see the // application has a blue toolbar. Then, without quitting the app, try // changing the primarySwatch below to Colors.green and then invoke // "hot reload"
我将Colors.blue
更改为Colors.green
,并且热重装工作正常,应用程序更改为绿色。但是当我尝试更改为Colors.black
时出现错误:
“颜色”类型不是“材料颜色”类型的子类型。
演示应用程序的完整代码:
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or press Run > Flutter Hot Reload in IntelliJ). Notice that the
// counter didn't reset back to zero; the application is not restarted.
primarySwatch: Colors.black,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return new Scaffold(
appBar: new AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: new Text(widget.title),
),
body: new Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: new Column(
// Column is also layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug paint" (press "p" in the console where you ran
// "flutter run", or select "Toggle Debug Paint" from the Flutter tool
// window in IntelliJ) to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'You have pushed the button this many times:',
),
new Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: new Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
答案 0 :(得分:6)
您不能使用Colors.black
,因为它不是MaterialColor
,并且primarySwatch
期望使用实质性的调色板。
如果转到ThemeData
的定义,则会看到以下内容:
/// * The primary color palette (the [primarySwatch]), chosen from
/// one of the swatches defined by the material design spec. This
/// should be one of the maps from the [Colors] class that do not
/// have "accent" in their name.
例如,Colors.blue
的定义是:
static const MaterialColor blue = MaterialColor(
_bluePrimaryValue,
<int, Color>{
50: Color(0xFFE3F2FD),
100: Color(0xFFBBDEFB),
200: Color(0xFF90CAF9),
300: Color(0xFF64B5F6),
400: Color(0xFF42A5F5),
500: Color(_bluePrimaryValue),
600: Color(0xFF1E88E5),
700: Color(0xFF1976D2),
800: Color(0xFF1565C0),
900: Color(0xFF0D47A1),
},
);
static const int _bluePrimaryValue = 0xFF2196F3;
Colors.black的定义是:
static const Color black = Color(0xFF000000);
这就是为什么您不能在那里使用黑色的原因。出于同样的原因,您不能使用Colors.white
。这两个是上述使用没有重音词的颜色的解释的例外。
如果您想要黑色,则可以创建自己的调色板:
const MaterialColor primaryBlack = MaterialColor(
_blackPrimaryValue,
<int, Color>{
50: Color(0xFF000000),
100: Color(0xFF000000),
200: Color(0xFF000000),
300: Color(0xFF000000),
400: Color(0xFF000000),
500: Color(_blackPrimaryValue),
600: Color(0xFF000000),
700: Color(0xFF000000),
800: Color(0xFF000000),
900: Color(0xFF000000),
},
);
const int _blackPrimaryValue = 0xFF000000;
然后使用primaryBlack
代替Colors.black。
您可以调整调色板中的不同颜色。
答案 1 :(得分:4)
只需使用primaryColor: Colors.black
我没有得到整个primarySwatch cr#^ ......
答案 2 :(得分:0)
您也可以简单地使用主题:ThemeData.dark()将应用程序主题更改为Dark
答案 3 :(得分:0)
我已经使用以下代码来获取Black Color
主题。
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primaryColor: Colors.black,
//primarySwatch: Colors.black,
),
home: MyHomePage(title: 'Flutter First Home Page' ),
);
}
}
颜色代码:
内部主题数据:(theme_data.dart)
........
......
brightness ??= Brightness.light;
final bool isDark = brightness == Brightness.dark;
primarySwatch ??= Colors.blue;
primaryColor ??= isDark ? Colors.grey[900] : primarySwatch;
primaryColorBrightness ??= estimateBrightnessForColor(primaryColor);
primaryColorLight ??= isDark ? Colors.grey[500] : primarySwatch[100];
primaryColorDark ??= isDark ? Colors.black : primarySwatch[700];
final bool primaryIsDark = primaryColorBrightness == Brightness.dark;
toggleableActiveColor ??= isDark ? Colors.tealAccent[200] : (accentColor ?? primarySwatch[600]);
accentColor ??= isDark ? Colors.tealAccent[200] : primarySwatch[500];
accentColorBrightness ??= estimateBrightnessForColor(accentColor);
final bool accentIsDark = accentColorBrightness == Brightness.dark;
canvasColor ??= isDark ? Colors.grey[850] : Colors.grey[50];
scaffoldBackgroundColor ??= canvasColor;
bottomAppBarColor ??= isDark ? Colors.grey[800] : Colors.white;
cardColor ??= isDark ? Colors.grey[800] : Colors.white;
dividerColor ??= isDark ? const Color(0x1FFFFFFF) : const Color(0x1F000000);
.....
.......
输出
答案 4 :(得分:0)
This is how I fixed it
class MainApp extends StatelessWidget {
static Map<int, Color> color = {
50: Color.fromRGBO(136, 14, 79, .1),
100: Color.fromRGBO(136, 14, 79, .2),
200: Color.fromRGBO(136, 14, 79, .3),
300: Color.fromRGBO(136, 14, 79, .4),
400: Color.fromRGBO(136, 14, 79, .5),
500: Color.fromRGBO(136, 14, 79, .6),
600: Color.fromRGBO(136, 14, 79, .7),
700: Color.fromRGBO(136, 14, 79, .8),
800: Color.fromRGBO(136, 14, 79, .9),
900: Color.fromRGBO(136, 14, 79, 1),
};
MaterialColor primeColor = MaterialColor(0xFF337C36, color);
MaterialColor accentColor = MaterialColor(0xFF337C36, color);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: primeColor,
accentColor: accentColor,
),
);
}
}
答案 5 :(得分:0)
对此我感到抱歉,但是我感到@chemamolins答案的答案不应该被接受。
也许有必要澄清一些关于颜色的关键点,导致对这些概念的误解会导致不良的设计。
关于颜色:
问题的事实是黑白不是颜色。 参见:https://adobe.com/creativecloud/design/discover/is-black-a-color.html
从物理上讲,当您的眼睛看到白色时,是因为表面反射了光谱中所有颜色的叠加。
所以你不能说白色是“一种颜色”,因为它是“某些颜色”。
另一方面,当没有颜色反射时,黑色就是您所看到的。
“没有颜色”的意思是……“没有颜色”对吗?
返回Flutter:
此颜色的值应与索引500和shade500的值相同。
请参阅:https://api.flutter.dev/flutter/material/MaterialColor-class.html
因此,对于所有大于500(600 ... 900)的值,都不存在,因为没有任何事物可以比黑色更深。
这就是为什么为这样的示例创建样本是无稽之谈。
正确的解决方案:
在我看来,@ MacDonal_11提出了更清洁的解决方案
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
//primarySwatch: Colors.blue,
primaryColor: Colors.black,
/**
* ... [The rest of your config here] ...
*/
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}