【问题标题】:How do I create an accent button in Flutter?如何在 Flutter 中创建重音按钮?
【发布时间】:2019-02-25 08:48:14
【问题描述】:

我正在尝试在 Flutter 中创建一个凸起的“重音”按钮——也就是说,背景颜色与应用的重音主题相同。这是我的代码:

new RaisedButton(
  onPressed: _signInPressed,
  child: new Text('Sign in with Google'),
  color: Theme.of(context).accentColor,
)

问题是文本颜色仍然是黑色。如何将文本颜色设置为像 AppBar 中的白色?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    目前没有获得重音按钮的官方方法。 不过,你可以让自己拥有:

    class AccentButton extends StatelessWidget {
      final VoidCallback onPressed;
      final Widget child;
    
      const AccentButton({this.child, this.onPressed, Key key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        final theme = Theme.of(context);
    
        return RaisedButton(
          onPressed: onPressed,
          child: child,
          textColor: theme.accentTextTheme.button.color,
          highlightColor: theme.accentColor,
          color: theme.accentColor,
        );
      }
    }
    

    【讨论】:

    • 对我来说,它创建了一个灰色背景和蓝色文本的按钮(在具有默认材质主题的应用程序中)。我正在寻找的是一个蓝色背景和白色文本的按钮
    • 这就是你应该让你的按钮使用重音主题(或直接使用 textTheme` 属性)的方式。但似乎他们的实施是错误的。在他们的代码中,在任何情况下我们都不会有重音背景
    • 这里为您制作
    • 谢谢!这样就可以完成工作(尽管我个人使用了“highlightColor:theme.highlightColor”,以便可以看到突出显示像在浮动操作按钮中一样“扩散”)
    猜你喜欢
    • 2021-06-08
    • 1970-01-01
    • 2022-01-25
    • 2018-09-23
    • 1970-01-01
    • 2021-06-18
    • 2021-01-30
    • 2019-12-24
    • 2019-09-22
    相关资源
    最近更新 更多