如何使用MaterialStateTextStyle?

huangapple go评论55阅读模式
英文:

How to use MaterialStateTextStyle?

问题

Flutter已经放弃了它的易用性,尤其是对于TextButtons。

我想要一个透明的TextButton,文本颜色为红色。我该如何在这里实现,你能解释一下为什么以及如何在这里使用MaterialStateTextStyle吗?

TextButton.icon(
    style: ButtonStyle(
        backgroundColor: MaterialStateProperty.all<Color>(Colors.transparent),
        textStyle: MaterialStateTextStyle(color: Colors.red) // 错误
    ),
    onPressed: () {},
    icon: Icon(Icons.search),
    label: Text('Search'),
);
英文:

Flutter has abandonded its easy applicability, in particular for TextButtons.

I would like to have a transparent TextButton with a red text color. How do I implement that here and could you explain, why and how you use MaterialStateTextStyle here?

TextButton.icon(
    style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all&lt;Color&gt;(Colors.transparent),
              textStyle:  MaterialStateTextStyle(color : Colors.red) // wrong
                        ),
                        onPressed: () {},
                        icon: Icon(Icons.search),
                        label: Text(&#39;Search&#39;),
                      );

答案1

得分: 2

这段代码可以用于创建一个具有红色文字颜色的透明 TextButton。

TextButton.icon(
  style: ButtonStyle(
    backgroundColor: MaterialStateProperty.all<Color>(Colors.transparent),
    foregroundColor: MaterialStateProperty.all<Color>(Colors.red),
  ),
  onPressed: () {},
  icon: const Icon(Icons.search),
  label: const Text('Search'),
);
英文:

This code can be used to make a transparent TextButton with red text color.

TextButton.icon(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all&lt;Color&gt;(Colors.transparent),
foregroundColor: MaterialStateProperty.all&lt;Color&gt;(Colors.red),
),
onPressed: () {},
icon: const Icon(Icons.search),
label: const Text(&#39;Search&#39;),
);

答案2

得分: 2

希望这有所帮助

TextButton(
  onPressed: () {
    // 处理按钮点击事件
  },
  style: ButtonStyle(
    foregroundColor: MaterialStateColor.resolveWith(
      (states) => Colors.red),
    overlayColor: MaterialStateColor.resolveWith(
      (states) => Colors.transparent),
  ),
  child: Text('搜索'),
),
英文:
I hope this helps     



         TextButton(
                  onPressed: () {
                    // Handle button press
                  },
                  style: ButtonStyle(
                    foregroundColor: MaterialStateColor.resolveWith(
                        (states) =&gt; Colors.red),
                    overlayColor: MaterialStateColor.resolveWith(
                        (states) =&gt; Colors.transparent),
                  ),
                  child: Text(&#39;Search&#39;),
                ),

答案3

得分: 2

你可以使用 Text 和 Icon 的参数来添加颜色,就像这样:

TextButton.icon(
      style: ButtonStyle(
        backgroundColor:
            MaterialStateProperty.all<Color>(Colors.transparent),
      ),
      onPressed: () {},
      icon: Icon(
        Icons.search,
        color: Colors.red,
      ),
      label: Text(
        'Search',
        style: TextStyle(color: Colors.red),
      ),
    );
英文:

You can add color using parameter in Text and Icon like this:

TextButton.icon(
      style: ButtonStyle(
        backgroundColor:
            MaterialStateProperty.all&lt;Color&gt;(Colors.transparent),
      ),
      onPressed: () {},
      icon: Icon(
        Icons.search,
        color: Colors.red,
      ),
      label: Text(
        &#39;Search&#39;,
        style: TextStyle(color: Colors.red),
      ),
    );

huangapple
  • 本文由 发表于 2023年5月30日 04:09:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定