【问题标题】:Is there a way to change the Text color inside of a FlatButton when highlightColor of the button is changed?当按钮的 highlightColor 改变时,有没有办法改变 FlatButton 内的文本颜色?
【发布时间】:2020-09-18 21:08:08
【问题描述】:

当平面按钮突出显示颜色发生变化时,我想更改平面按钮内的文本小部件文本的颜色。当用户长时间按下按钮时,FlatButton 的高亮颜色会发生变化。这是我拥有的按钮的示例代码:

 class DefaultButton extends StatelessWidget {
  const DefaultButton({
    Key key,
    this.text,
    this.press,
    this.buttonColor = primaryColor,
  }) : super(key: key);
  final String text;
  final Function press;
  final Color buttonColor;
 

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      height: 50,
      child: FlatButton(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(40)),
        highlightColor: Colors.yellow,
        color: buttonColor,
        onPressed: press,
        child: Text(
          text,
          style: TextStyle(
            fontFamily: primaryFontFamily,
            fontWeight: FontWeight.w400,
            fontSize: 20,
            color: primaryColor,
          ),
        ),

      ),
    );
  }
}

实现这一目标的最佳方法是什么? 谢谢!

【问题讨论】:

  • 你能展示一些示例源代码吗,到目前为止你已经取得了什么成就?
  • 到目前为止,我已经添加了按钮小部件的源代码。

标签: flutter dart flatbutton


【解决方案1】:

不知道你是如何使用这个 FlatButton 作为 Muhammad 正确指出的,但最直接的解决方案是使用布尔值

FlatButton(
          color: !pressed ? Colors.blue : Colors.amber,
          onPressed: null,
          onLongPress: () {
            setState(() {
              pressed = !pressed;
            });
          },
          child: Text(
            'text',
            textAlign: TextAlign.left,
            style: TextStyle(
              color: pressed ? Colors.blue : Colors.amber,
              fontWeight: FontWeight.w600,
              fontSize: 14.0,
              height: 1.2,
            ),
          ),
        ),

然后按照你的意愿管理你的状态 Bloc,StreamBuilder 选择你的毒药

【讨论】:

    猜你喜欢
    • 2016-11-13
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    相关资源
    最近更新 更多