Flutter:无法同时切换的按钮

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

Flutter: buttons that can't both be toggle on

问题

我遇到了制作我想要的UI的困难。我想要将两个按钮水平放在一起,当其中一个按钮被按下时,另一个按钮会被取消按下(类似切换)。

ElevatedButton(
  style: ButtonStyle(
    foregroundColor: getColor(Colors.green, Colors.white),
    backgroundColor: getColor(Colors.white, Colors.green),
    side: getBorder(Colors.green, Colors.white),
  ),
  onPressed: () {
    isSafe = true;
    setState(() {
      safetyMessage = '当前安全状态:$isSafe';
    });
  },
  child: const Text('是'),
),

这是其中一个按钮,还有一个相同的按钮对应"否"。

英文:

I'm having trouble making the UI I intended. I want to make two buttons be next to each other horizontally, when one of the buttons is pressed the other is unpressed (like toggle).

ElevatedButton(
                style: ButtonStyle(
                  foregroundColor: getColor(Colors.green, Colors.white),
                  backgroundColor: getColor(Colors.white, Colors.green),
                  side: getBorder(Colors.green, Colors.white),
                ),
                onPressed: () {
                  isSafe = true;
                  setState(() {
                    safetyMessage = 'Currently Safe: $isSafe';
                  });
                },
                child: const Text('Yes'),
              ),

this is one of the buttons, there is an identical button for "no".

答案1

得分: 0

你可以尝试直接使用ChoiceChip WidgetSegmentedButton Widget,因为它们都直接使用了你想要的功能。

英文:

Instead of trying to implement the functionality directly you could use a ChoiceChip Widget or a SegmentedButton Widget since they both make use of the functionality you are wanting directly.

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

发表评论

匿名网友

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

确定