简体   繁体   English

Flutter 自定义单选按钮

[英]Flutter custom radiobuttons

Can anyone guide me how to achieve this in flutter?谁能指导我如何在 flutter 中实现这一目标? Thanks谢谢

在此处输入图像描述

Here you go bro..... Cheers !!!在这里你 go 兄弟.....干杯!

class _MyHomePageState extends State<MyHomePage> {



  double width;
  double height;

  int currentValue = 5;

  Map<int,int> minutesCostMap = {
    5: 20,
    10 : 25,
    15 : 30,
    20 : 35,
  },


  @override
  Widget build(BuildContext context) {

    height = MediaQuery.of(context).size.height;
    width = MediaQuery.of(context).size.width;

    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            Padding(
              padding: const EdgeInsets.symmetric(vertical: 15),
              child: Text("Chris Watson",style: TextStyle(fontWeight: FontWeight.bold,fontSize: 19),),
            ),
            Padding(
              padding: const EdgeInsets.only(bottom: 25),
              child: Text.rich(
                  TextSpan(children: [
                    TextSpan(text: "This "),
                    TextSpan(text: currentValue.toString() + " minutes ",style: TextStyle(fontWeight: FontWeight.bold)),
                    TextSpan(text: "session will cost you "),
                    TextSpan(text: minutesCostMap[currentValue].toString() + "\$",style: TextStyle(fontWeight: FontWeight.bold)),
                  ])
              ),
            ),
            Stack(
              alignment: Alignment.center,
              children: [
                Container(                                  /// GREY HORIZONTAL LINE
                  height: 10,width: width * 0.6,color: Colors.grey[350],
                ),
                Container(
                  width: width * 0.6 + 50,
                  height: 50,
                  child: Row(                            /// ROW OF 4 CIRCLE WIDGETS PLACED ABOVE THAT LNE
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      checkpointButton(5,currentValue == 5),
                      checkpointButton(10,currentValue == 10),
                      checkpointButton(15,currentValue == 15),
                      checkpointButton(20,currentValue == 20),
                    ],
                  ),
                ),
                //Positioned(left: 0,chil),
                //Positioned(left: ,child: Container(decoration: BoxDecoration(shape: BoxShape.circle,color: Colors.blue),width: 40,height: 40,)),
                //Positioned(left: ,child: Container(decoration: BoxDecoration(shape: BoxShape.circle,color: Colors.blue),width: 40,height: 40,)),
                //Positioned(left: ,child: Container(decoration: BoxDecoration(shape: BoxShape.circle,color: Colors.blue),width: 40,height: 40,)),
              ],
            ),
            Padding(
              padding: EdgeInsets.only(top: 25),
              child: Container(
                width: width * 0.75,
                height: 40,
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  children: [
                    Expanded(
                      flex: 1,
                      child: Container(
                        decoration: BoxDecoration(border: Border.all(color: Colors.blue,width: 1.5)),
                        child: Center(child: Text("CANCEL",style: TextStyle(color: Colors.blue),),),
                      ),
                    ),
                    Container(width: 10,height: 5,),
                    Expanded(
                      flex: 2,
                      child: Container(color: Colors.blue,child: Center(child: Text("ACCEPT",style: TextStyle(color: Colors.white),),),),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  Widget checkpointButton(int value,bool isActive){
    return GestureDetector(               /// THIS WILL DETECT A TAP EVENT ON THIS CIRCLE WIDGET
        onTap: (){
          setState(() {
            currentValue = value;         /// IF THE CURRENT VALUE IS SAME AS THE VALUE OF THE CIRCLE, IT WILL CHANGE ITS APPEARANCE TO SHOW THAT ITS ACTIVE
          });
        },
        child: Container(            /// CIRCLE PRESENT BEHIND PROVIDING THE BLUE BORDER
            height: 50,width: 50,
            decoration: BoxDecoration(shape: BoxShape.circle,border: Border.all(color: isActive ? Colors.blue : Colors.transparent,width: 1.5)),
            child: Center(
                child: Container(        /// INNER CIRCLE CONTAINING THE VALUE
                  decoration: BoxDecoration(shape: BoxShape.circle,color: isActive ? Colors.blue : Colors.grey[350]),
                  width: 40,height: 40,
                  child: Center(child: Text(value.toString(),style: TextStyle(fontWeight: FontWeight.bold,color: isActive ? Colors.white : Colors.black),)),
                  )
            )
        )
    );
  }

}

Result:结果:

在此处输入图像描述

Edit:编辑:

I have updated the code above to match your requirements.我已经更新了上面的代码以满足您的要求。 Basically what I did what I added a Map (minutesCostMap) which contains the list of the minutes and their corresponding cost.基本上我做了什么,我添加了一个 Map (minutesCostMap),其中包含分钟列表及其相应的成本。 Then moving on the the place where we are displaying the cost, you can see that I used the value from the map itself.然后移动到我们显示成本的地方,你可以看到我使用了 map 本身的值。 So the value we are printing will output the cost with corresponds to the provided minutes(ie present in the currentValue variable)所以我们正在打印的值将 output 与提供的分钟相对应的成本(即存在于 currentValue 变量中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM