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

Flutter使用Fluttertoast修改背景与文字颜色不生效如何解决

Fluttertoast 颜色设置不生效解决方案

问题代码

你当前使用的调用代码如下:

Fluttertoast.showToast(
              msg: text,
              toastLength: Toast.LENGTH_SHORT,
              gravity: ToastGravity.CENTER,
              timeInSecForIosWeb: 1,
              backgroundColor: Colors.red,
              textColor: Colors.white,
              fontSize: 16.0
          );

可行解决方法

  • 先升级fluttertoast到最新稳定版本,旧版本存在部分安卓机型自定义样式不生效的已知bug,修改pubspec.yaml中的依赖版本后执行flutter pub get,重启项目再测试
  • 安卓平台调用时新增参数androidShowGravity: ToastGravity.CENTER,部分安卓系统原生Toast会强制走系统默认样式,覆盖传入的自定义颜色参数,新增该参数可强制使用自定义配置
  • 完全规避原生系统样式限制的方案:使用包内提供的FToast类自定义Toast布局,示例代码如下:
// 全局封装自定义Toast方法
void showCustomToast(String msg, BuildContext context) {
  FToast fToast = FToast();
  fToast.init(context);
  Widget toast = Container(
    padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
    decoration: BoxDecoration(
      borderRadius: BorderRadius.circular(25.0),
      color: Colors.red, // 自定义背景色
    ),
    child: Text(
      msg,
      style: const TextStyle(
        color: Colors.white, // 自定义文字颜色
        fontSize: 16
      ),
    ),
  );
  fToast.showToast(
    child: toast,
    gravity: ToastGravity.CENTER,
    toastDuration: const Duration(seconds: 1),
  );
}
// 业务层调用
showCustomToast("提示内容", context);
  • 确认设备没有开启强制深色模式,部分系统的强制深色模式会自动修改应用弹窗的背景和文字颜色,覆盖代码中的配置

验证注意事项

修改代码后先执行flutter clean清除旧构建缓存,再重新运行项目测试,避免缓存导致修改不生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 15:06:06