首页 > 解决方案 > Flutter 全局定义按钮样式

问题描述

所以我想全局定义我的按钮样式,以便我可以一次访问它们。我已经为 TextStyle 启用了这个功能,如下所示:

abstract class ThemeText {
  static const TextStyle exampleStyle= TextStyle(
      fontFamily: 'Montserrat',
      color: Colors.black,
      fontSize: 40,
      height: 0.5,
      fontWeight: FontWeight.w600);
}

当我像这样在我的文本中访问它时:

            child: TextButton(
                onPressed: (){},
                child: Text("Test",
                style: ThemeText.progressHeader,),
)

它就像一个魅力。

现在我想对我的 ButtonStyle 做同样的事情:

TextButton(
        style: TextButton.styleFrom(
            primary: Colors.white,
            backgroundColor: Colors.green[900],
            textStyle: TextStyle(fontSize: 18, fontStyle: FontStyle.italic)),
        child: Text("Test!"),
)

但是,如果我尝试相同的方法,无论我如何编写它,我总是会收到消息“const variables must be initialized with a constant value”。如何为按钮设置它的格式,以便它像 TextStyle 一样工作?

非常感谢您!

标签: flutterbuttonstyles

解决方案


推荐阅读