颤振:未定义命名参数'foregroundColor'



我正在通过"编写您的第一个Flutter应用程序"教程,并已获得第2部分,步骤7没有问题。然而,在这一行中:

foregroundColor: Colors.black,

VSCode强调foregroundColor并说:

The named parameter 'foregroundColor' isn't defined

它在热重新加载后没有改变颜色,并且它不会与错误一起构建,调试控制台说:

lib/main.dart:17
foregroundColor: Colors.black,
^^^^^^^^^^^^^^^
: Context: Found this candidate, but the arguments don't match.
../…/material/theme_data.dart:219
factory ThemeData({
^

下面是完整的buildWidget(在MyApp类中):

Widget build(BuildContext context) {
return MaterialApp(
title: 'Startup Name Generator',
theme: ThemeData(
backgroundColor: Colors.white,
foregroundColor: Colors.black,
),
home: RandomWords(),
);
}

:我没有

appBarTheme: const AppBarTheme(
//themes here
),

ThemeData中,如教程所示。添加后,它工作!

Flutter主题数据不提供前景属性。但appBarThemeData提供了这个。这样的

theme: 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 simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
backgroundColor: Colors.red,
appBarTheme: const AppBarTheme(foregroundColor: Colors.red),
primarySwatch: Colors.blue,
),

我想你指的是这个代码实验室。https://codelabs.developers.google.com/codelabs/first-flutter-app-pt2 # 6

别担心,这是个错误。我已经报道过了。当前ThemeData类中没有名为foregroundColor的属性。

现在flutter团队建议使用ColorScheme来定义应用程序的颜色。下面是一个相同的例子。

theme: ThemeData(
colorScheme: ColorScheme.light(
primary: Colors.blue,
secondary: Colors.red,
background: Colors.white,
),
appBarTheme: AppBarTheme(
backgroundColor: Colors.white,
elevation: 0,
iconTheme: IconThemeData(color: AppColor.primaryColor)),
scaffoldBackgroundColor: Colors.white);

最新更新