当标准flutter小部件具有硬编码主题(例如图标的颜色)时,扩展标准小部件的正确方法是什么?

我想更改标准颤动小部件 ExpansionPanel()中使用的V形符号的颜色。这可以在 packages / flutter / lib / src / material / expansion_panel.dart 中找到。

但是我发现此人字形的图标颜色比 ExpandIcon()中的图标颜色低一级。这在 packages / flutter / lib / src / material / expand_icon.dart

将expand_icon.dart拉入我的应用程序并更新/覆盖该硬编码颜色的正确方法是什么?

颜色定义如下:

  /// Default icon colors and opacities for when [Theme.brightness] is set to
  /// [Brightness.light] are based on the
  /// [Material Design system icon specifications](https://material.io/design/iconography/system-icons.html#color).
  /// Icon colors and opacities for [Brightness.dark] are based on the
  /// [Material Design dark theme specifications](https://material.io/design/color/dark-theme.html#ui-application)
  Color get _iconColor {
    if (widget.isExpanded && widget.expandedColor != null) {
      return widget.expandedColor;
    }

    if (widget.color != null) {
      return widget.color;
    }

    switch(Theme.of(context).brightness) {
      case Brightness.light:
        return Colors.black54;   // want to change this!!!
      case Brightness.dark:
        return Colors.white60;   // want to change this!!!

    }

    assert(false);
    return null;
  }
aiai0525 回答:当标准flutter小部件具有硬编码主题(例如图标的颜色)时,扩展标准小部件的正确方法是什么?

您可以使用mixin,它是“ class,其中包含供其他类使用的方法,而不必成为其他类的父类。”这是一个示例实现,允许上面示例中的类Color使用新方法getNewIconColor,您可以在其中指定要使用的颜色。

mixin MyNewColors {
  get newIconColor {
    //...

    switch (Theme.of(context).brightness) {
      case Brightness.light:
        return Colors.orange; // or the colors you want to change!!!
      case Brightness.dark:
        return Colors.blue; // or colors you want to change!!!

    }

    // ...
  }
}

class MyColor extends Color with MyNewColors {
  // ···
}
本文链接:https://www.f2er.com/3141315.html

大家都在问