当 useMaterial3 为 true 时 PrimarySwatch 不工作

みずま*_*まんま 6 dart flutter flutter-design

当我将 Flutter 更新到 Flutter 3.7.6 时,突然不再反映“primarySwatch”。如果我将“useMaterial3”设置为 false,它就会起作用,但我不知道为什么。

return MaterialApp(
 title: 'Demo',
 theme: ThemeData(
 primarySwatch: Colors.pink,
 appBarTheme: const AppBarTheme(elevation: 0),
 useMaterial3: true,
 ),
home: const HomeScreen(),
);
Run Code Online (Sandbox Code Playgroud)

我尝试将 useMaterial3 设置为 false,但想不出任何其他解决方案。

Fel*_*uco 3

在 Flutter 3 中,当您通过设置为 启用 Material 3 Theming 时useMaterial3trueprimarySwatch属性不再用于颜色主题。

相反,您应该使用colorSchemeSeed来指定种子颜色。替换primarySwatch: Colors.pinkcolorSchemeSeed: Colors.pink以反映您的颜色偏好。

return MaterialApp(
  title: 'Demo',
  theme: ThemeData(
    colorSchemeSeed: Colors.pink,
    appBarTheme: const AppBarTheme(elevation: 0),
    useMaterial3: true,
  ),
  home: const HomeScreen(),
);
Run Code Online (Sandbox Code Playgroud)