【问题标题】:Set default color for a specific widget in Flutter在 Flutter 中为特定小部件设置默认颜色
【发布时间】:2020-02-13 18:05:23
【问题描述】:

是否可以在 Flutter 中指定一种颜色作为单个小部件中的默认颜色?

比如我有这种情况:

Widget _widget1 = Scaffold(
    body: Center(
        child: new Container(
            width: 300.0,
            height: 300.0,
            decoration: new BoxDecoration(
                color: Colors.orange, //TODO set orange as default for BoxDecoration
                shape: BoxShape.circle,
            )
        )
    )
);

Widget _widget2 = Scaffold(
    body: Center(
        child: new Container(
            width: 120.0,
            height: 150.0,
            decoration: new BoxDecoration(
                color: Colors.orange, //TODO set orange as default for BoxDecoration
                shape: BoxShape.rectangle,
            )
        )
    )
);

我不想每次都指定 BoxDecoration 需要color: Colors.orange。

【问题讨论】:

  • 只使用全局变量,可以指定final Color orangeColor = Colors.orange;对我来说,我总是创建一个名为 globals.dart 的文件,其中包含我需要的所有全局变量,并将其导入到需要使用它的每个文件中

标签: user-interface flutter layout colors flutter-layout


【解决方案1】:

您可以通过多种不同的方式来做到这一点。在某处设置一个 const BoxDecoration,然后用于装饰某些小部件并使用 copyWith() 复制它的颜色,同时覆盖您添加的任何其他内容。或者您可以使用 Theme 继承的小部件。

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2016-04-28
    相关资源
    最近更新 更多