flutter - 如何使 Radio Widget 看起来像一个按钮

标签 flutter radio-button flutter-layout

我正在重建一个旧应用,需要让单选按钮看起来像实际按钮。 执行此操作的最佳方法是什么?

旧应用引用:

最佳答案

您可以使用有状态小部件并跟踪该小部件中的选定值。该小部件将是一个容器,带有子文本。点击时,您会更改所选值。然后根据所选的值,为您的容器返回一个不同的 BoxDecoration。代码见下方:

class ButtonStyleRadioButton extends StatefulWidget {
 // Function to call when tapped
 Function(bool) onChanged;

 ButtonStyleRadioButton({this.onChanged});

_ButtonStyleRadioButtonState createState() => _ButtonStyleRadioButtonState();
}

class _ButtonStyleRadioButtonState extends State<ButtonStyleRadioButton> {
  bool _selected = false;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){ setState(() {
       _selected = !_selected; 
       // Callback to passed in function
       widget.onChanged(_selected);
      });},
      child: Container(
      padding: EdgeInsets.all(10.0),
      child:  Text('Consumer'),
       decoration: BoxDecoration(
         borderRadius: BorderRadius.circular(50.0),
         border: Border.all(color: Colors.grey[400]),
         color: _selected ? Colors.red : Colors.white
       )));
  }
}

关于flutter - 如何使 Radio Widget 看起来像一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54665473/

相关文章:

ios - 使用 XCode 在物理设备上运行 Flutter 时出现未处理的异常 iOS 12.5.5

c# - Asp.net MVC 多个单选按钮组

android - 微调器中的图像+ TextView +单选按钮

android - flutter : RenderBox was not laid out: RenderRepaintBoundary#58c65 relayoutBoundary=up1 NEEDS-PAINT

gridview - Flutter 中的动态 GridView 列表 UI

Flutter Push 与 State 相关的 View

flutter - 固定小部件在一行中的位置

android - 必须从Maven或自定义位置提供aapt2之一

javascript - Radio + Checkbox 值,但 Radio 按钮无限期地对 onclick 求和。为什么? (带一个片段)

flutter - 在 Flutter 中,什么时候你更喜欢 `Widget` 继承而不是组合?