You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter应用编译Android SDK 34后默认颜色变更问题求助

问题原因

当你将编译目标升级到Android 34时,你的Flutter版本大概率默认启用了Material 3设计系统。Material 3不再沿用Material 2中primaryColor的配置逻辑,而是通过ColorScheme统一管理主题色彩,所以你原本设置的primaryColor不会生效,TextField会使用Material 3默认的紫色主题色。

解决方案

方案1:关闭Material 3,沿用Material 2逻辑

在ThemeData中添加useMaterial3: false,恢复Material 2的主题机制,原primaryColor设置会正常生效:

ThemeData theme = ThemeData(
  useMaterial3: false, // 显式关闭Material3
  primaryColor: Colors.blue,
  buttonTheme: const ButtonThemeData(
    textTheme: ButtonTextTheme.primary,
  ),
  inputDecorationTheme: const InputDecorationTheme(
    filled: true,
    fillColor: Colors.white,
  ),
);

方案2:适配Material 3,使用ColorScheme配置主题

如果想保留Material 3的设计风格,通过ColorScheme定义主色即可:

方式A:使用种子色生成主题

ThemeData theme = ThemeData(
  colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue), // 以蓝色为种子色生成主题
  inputDecorationTheme: const InputDecorationTheme(
    filled: true,
    fillColor: Colors.white,
  ),
  // Material3中buttonTheme已被替代,可按需配置elevatedButtonTheme
  elevatedButtonTheme: ElevatedButtonThemeData(
    style: ElevatedButton.styleFrom(
      foregroundColor: Colors.white,
    ),
  ),
);

方式B:直接定义ColorScheme

ThemeData theme = ThemeData(
  colorScheme: const ColorScheme.light(
    primary: Colors.blue, // 设置主色为蓝色
    onPrimary: Colors.white, // 主色背景上的文字颜色
  ),
  inputDecorationTheme: const InputDecorationTheme(
    filled: true,
    fillColor: Colors.white,
  ),
);

方案3:单独定制TextField的样式

如果只想修改TextField的颜色,不需要全局调整主题,可以直接在inputDecorationTheme中定制边框、标签等颜色:

inputDecorationTheme: const InputDecorationTheme(
  filled: true,
  fillColor: Colors.white,
  focusedBorder: OutlineInputBorder(
    borderSide: BorderSide(color: Colors.blue), // 聚焦时边框为蓝色
  ),
  enabledBorder: OutlineInputBorder(
    borderSide: BorderSide(color: Colors.grey), // 默认状态边框为灰色
  ),
  labelStyle: TextStyle(color: Colors.blue), // 标签文字为蓝色
),

内容的提问来源于stack exchange,提问作者Gustavo Schwarz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 02:20:26