首页 > 解决方案 > 如何在颤动中为自定义背景颜色创建一个全局类

问题描述

我有这样的 LinearBackground 颜色

Container(
        decoration: BoxDecoration(
            gradient: LinearGradient(
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
                colors: [Colors.red, Colors.blue])));

就我而言,我有 3 个屏幕,我想将我的自定义背景应用到我拥有的所有屏幕上,有没有办法将上面的代码声明为一个类,以便我可以在我的屏幕上调用它需要它

标签: flutterclasscolorsbackgroundbackground-color

解决方案


像这样创建一个带有类的 dart 文件

class AppConstants {

  static const BoxDecoration myBoxDecoration = BoxDecoration(
  gradient: LinearGradient(
  begin: Alignment.topLeft,
  end: Alignment.bottomRight,
  colors: [Colors.red, Colors.blue]));

}

并像这样使用它

child: Container(decoration: AppConstants.myBoxDecoration),

推荐阅读