轻按一次选择单选按钮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了轻按一次选择单选按钮相关的知识,希望对你有一定的参考价值。

如何像图片enter image description here中那样一次选择自定义单选按钮

答案
import 'package:flutter/material.dart';
class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
  }
   class _HomeScreenState extends State<HomeScreen> {
       int _selection = 0;

      selectTime(int timeSelected) {
       setState(() {
       _selection = timeSelected;
          });
         }

         @override
  Widget build(BuildContext context) {
    return SafeArea(
    child: Scaffold(
     appBar: AppBar(
      title: Text("Home"),
       ),

       body: Container(
      padding: EdgeInsets.all(30),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              InkWell(
                onTap: () {
                  setState(() {
                    _selection = 1;
                  });
                },
                child: Stack(
                  children: <Widget>[
                    Container(
                      height: 40,
                      width: 150,
                      color: _selection == 1 ? Colors.green : Colors.white,
                    ),
                    Row(
                      children: <Widget>[
                        Radio(
                          focusColor: Colors.white,
                          groupValue: _selection,
                          onChanged: selectTime,
                          value: 1,
                        ),
                        Text(
                          "11:00 - 12:00",
                          style: TextStyle(fontWeight: FontWeight.bold),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              InkWell(
                onTap: () {
                  setState(() {
                    _selection = 2;
                  });
                },
                child: Stack(
                  children: <Widget>[
                    Container(
                      height: 40,
                      width: 150,
                      color: _selection == 2 ? Colors.green : Colors.white,
                    ),
                    Row(
                      children: <Widget>[
                        Radio(
                          focusColor: Colors.white,
                          groupValue: _selection,
                          onChanged: selectTime,
                          value: 2,
                        ),
                        Text(
                          "12:00 - 13:00",
                          style: TextStyle(fontWeight: FontWeight.bold),
                        ),
                      ],
                    ),
                  ],
                ),
              )
            ],
          ),
        ],
      )),
));

}}

以上是关于轻按一次选择单选按钮的主要内容,如果未能解决你的问题,请参考以下文章

一次只能选择一个单选按钮

当数据来自数据库时,如何一次只选择一个单选按钮并在javascript中取消选择其他单选按钮?

带有 UIButton 单选按钮的 UICollectionViewCell - 一次选择一个单选按钮

如何用MFC制作一个按钮,第一次按下,再按一次弹起

如何在表格视图中一次选择单选按钮

单选按钮以片段形式传递数据