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

如何在Flutter应用中更改弹窗颜色?以及如何修改share_plus包中无内置改色选项的白色分享弹窗菜单颜色?

嘿,刚好我之前处理过类似的问题,来给你详细讲讲怎么解决这两个弹窗颜色的需求!

一、Flutter通用弹窗的颜色修改

不同类型的弹窗修改方式略有不同,这里给你列两种最常用的:

1. Material风格的AlertDialog

直接通过backgroundColor属性就能设置背景色,文字颜色可以通过titleTextStyle、contentTextStyle单独调整:

showDialog(
  context: context,
  builder: (context) => AlertDialog(
    backgroundColor: Colors.deepPurple[100], // 自定义背景色
    title: Text("自定义颜色弹窗"),
    titleTextStyle: TextStyle(color: Colors.deepPurple[800]), // 标题文字颜色
    content: Text("这是修改背景色后的效果"),
    contentTextStyle: TextStyle(color: Colors.grey[700]), // 内容文字颜色
    actions: [
      TextButton(
        onPressed: () => Navigator.pop(context),
        child: Text("确认", style: TextStyle(color: Colors.blue)),
      ),
    ],
  ),
);

2. Cupertino风格的弹窗

和Material类似,通过backgroundColor设置背景,文字颜色用textStyle属性:

showCupertinoDialog(
  context: context,
  builder: (context) => CupertinoAlertDialog(
    backgroundColor: Colors.orange[50],
    title: Text("Cupertino弹窗", style: TextStyle(color: Colors.orange[800])),
    content: Text("修改背景色后的效果"),
    actions: [
      CupertinoDialogAction(
        onPressed: () => Navigator.pop(context),
        child: Text("取消", style: TextStyle(color: Colors.red)),
      ),
    ],
  ),
);
二、修改share_plus的分享弹窗颜色

share_plus调用的是系统原生的分享菜单,Flutter层没有直接的配置入口,得针对Android和iOS分别处理:

Android端修改方案

Android的分享菜单样式受App主题控制,你需要修改android/app/src/main/res/values/styles.xml(深色主题对应values-night/styles.xml):

  1. 找到你的App主题(一般名为Theme.XXX),添加或修改以下属性:
<style name="Theme.MyFlutterApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- 分享菜单的背景色 -->
    <item name="android:colorBackground">@color/share_menu_bg</item>
    <!-- 分享菜单的文字颜色 -->
    <item name="android:textColorPrimary">@color/share_menu_text</item>
</style>
  1. 在res/values/colors.xml里定义你要的颜色:
<color name="share_menu_bg">#E8F5E9</color>
<color name="share_menu_text">#2E7D32</color>

⚠️ 注意:这个修改会影响整个App的系统弹窗样式,如果只想单独改分享菜单,需要自定义分享Intent的主题,步骤会复杂很多,一般全局主题修改是更高效的方案。

iOS端修改方案

iOS的分享菜单(UIActivityViewController)的图标和文字颜色可以通过tintColor控制,背景色则跟随系统的浅色/深色模式,无法直接自定义(强行自定义会破坏原生体验,不推荐):

方式1:通过代码设置(Main.swift)

打开ios/Runner/Main.swift,在AppDelegate里添加全局tintColor配置:

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    // 设置分享菜单的图标和文字颜色
    UIView.appearance().tintColor = UIColor(red: 0.95, green: 0.32, blue: 0.11, alpha: 1.0)
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

方式2:通过Info.plist配置

打开ios/Runner/Info.plist,添加UIActivityViewControllerTintColor键:

<key>UIActivityViewControllerTintColor</key>
<string>#FF5722</string>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 22:54:05