Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change border color of Radio widget in Flutter?

What I Currently Have

enter image description here

What I want to achieve

enter image description here

The Code that I have now

Radio(
   value: 2,
   groupValue: val,
   onChanged: (value) {
   setState(() {
      val = value;
      });
   },
  activeColor: secondaryColor,)
like image 590
Ahmed ibrahim Avatar asked Sep 30 '21 19:09

Ahmed ibrahim


People also ask

How do you customize radio button in Flutter?

Since the radio button contains a label, we can't use the Radio button. Instead, we are going to create a custom class that can be used the create the options called MyRadioOption . Inspired by Flutter's Radio widget, the class has value , groupValue , and onChanged properties.

How do you change the inactive color on a radio button Flutter?

ListTile( onTap: () => onChanged(template.id), leading: Radio( value: template.id, groupValue: checkedId, ) ... ) Doing that, the Radio becomes "inactive" and changes it's color to grey. The Radio has an activeColor property, but not for inactive.

How do you get the border color to ElevatedButton in Flutter?

We can change the border color using BorderSide class. to Change Border Color of ElevatedButton in Flutter ElevatedButton has style Property so we can use the styleFrom method should be used to change the default style of the elevated button. We can change the border color using BorderSide class.

How to change border color of radio button in flutter?

To change the border color of the radio button when it is unselected just wrap the radio button inside a Theme widget as a child and set ThemeData ‘s unselectedWidgetColor property to your desired color. That brings an end to flutter radio button example tutorial.

What is a radio button widget in flutter?

In this example tutorial, we will learn how to use a radio button widget in flutter and its properties with examples. Radio button in flutter is nothing but an option button where the user can select only one option from a group of options. The selected state is nothing but ON and unselected is OFF.

How to add border to other widgets in flutter?

Border widget in flutter is assigned a simple functionality to add borders to the other widgets. The first is by creating all borders using BorderSide. The second way is by using Border.all to create a uniform border having the same color and width. The third is by using Border.fromBorderSide to create a border whose sides are all same.

How to change the color and width of container border in flutter?

Flutter – Change Container Border’s Color & Width. You can change the color and width of Container widget’s border. To change the color and width of Container’s border, use its decoration property. Set decoration property with BoxDecoration () object. Set the border property of BoxDecoration () object, with required color and width as per your ...


2 Answers

It's not possible to customize that much the Radio button. The only color parameter for the button is fillColor. It will impact both the inner plain circle and the outer circle.

If you really want a custom look you'll need to build your own widget. Here is a simple example that you can customize and improve. You could also try to start from the source code of the flutter Radio widget.

class CustomRadio extends StatefulWidget {
  final int value;
  final int groupValue;
  final void Function(int) onChanged;
  const CustomRadio({Key? key, required this.value, required this.groupValue, required this.onChanged})
      : super(key: key);

  @override
  _CustomRadioState createState() => _CustomRadioState();
}

class _CustomRadioState extends State<CustomRadio> {
  @override
  Widget build(BuildContext context) {
    bool selected = (widget.value == widget.groupValue);

    return InkWell(
      onTap: () => widget.onChanged(widget.value),
      child: Container(
        margin: const EdgeInsets.all(4),
        padding: const EdgeInsets.all(4),
        decoration: BoxDecoration(shape: BoxShape.circle, color: selected ? Colors.white : Colors.grey[200]),
        child: Icon(
          Icons.circle,
          size: 30,
          color: selected ? Colors.deepPurple : Colors.grey[200],
        ),
      ),
    );
  }
}

Result :

enter image description here

like image 70
Tanguy Avatar answered Oct 16 '22 23:10

Tanguy


There is no way to provide the color of the outer circle in the material Radio class, what you can do is to create your own radio Button, either from scratch (Check the @Tanguy answer)or you copy the Radio class and change its design/behavior. If you decide to copy the Radio class you need to inherit from the Radio so it can work with the rest of the Material design elements. Here is a copy of it https://gist.github.com/karimkod/8f7c93da798670c8b6e9b85ff9fb5383 You can change the paint method of The _RadioPainter class to draw basically whatever you want. I have changed it in the gist to look purple.

like image 39
Abdelkrim Bournane Avatar answered Oct 16 '22 22:10

Abdelkrim Bournane