32

How to change checkbox border-color in flutter? By default, it is showing black but I want it in grey.

Aishwarya
  • 509
  • 1
  • 7
  • 14
  • The default color of `unselectedWidget` is always grey. Still, you can prefer the solution given below to change the color. – Ayush Bherwani Jan 08 '20 at 18:26

10 Answers10

55

Use this:

Checkbox(
  shape: RoundedRectangleBorder(
    borderRadius: BorderRadius.circular(2.0),
  ),
  side: MaterialStateBorderSide.resolveWith(
      (states) => BorderSide(width: 1.0, color: Colors.red),
  ),
),
chuxyz
  • 567
  • 5
  • 2
43

CheckBox's border color comes from unselectedWidgetColor of your ThemeData.

Add following ThemeData to your MaterialApp

MaterialApp(
  title: 'Flutter Demo',
  debugShowCheckedModeBanner: false,
  theme: ThemeData(
    primarySwatch: Colors.blue,
    unselectedWidgetColor: Colors.red, // <-- your color
  ),
  home: MyHomePage(title: 'Flutter Demo Home Page'),
);

If you don't want to add color to the MaterialApp's ThemeData then you can wrap your CheckBox widget with Theme widget, following is the code for your reference:

Theme(
    child: Checkbox(
      value: false,
      onChanged: (_) {},
    ),
    data: ThemeData(
      primarySwatch: Colors.blue,
      unselectedWidgetColor: Colors.red, // Your color
    ),
  ),
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Kalpesh Kundanani
  • 5,413
  • 4
  • 22
  • 32
  • 2
    Great! Also do we have option to change the border of checkbox when it is selected? I want it this way -> For ex : tick color is green and checkbox border color is green with checkbox background as white. – Tanmay Pandharipande Aug 07 '20 at 08:53
  • how can we change checkbox width to 1 pixel? Thanks. – Kamlesh Jul 16 '21 at 07:53
11

You can also simply set the side property of your Checkbox:

Checkbox(
      value: true,
      onChanged: (_) {},
      // Background color of your checkbox if selected
      activeColor: Colors.deepOrange,
      // Color of your check mark
      checkColor: Colors.black,
      shape: hasCircleShape
      // diplay checkbox with circle shape
          ? CircleBorder()
      // or make the border slightly rounded
          : RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(5),
            ),
      side: BorderSide(
        // ======> CHANGE THE BORDER COLOR HERE <====== 
        color: Colors.grey,
        // Give your checkbox border a custom width
        width: 1.5,
      ),
    ),
hnnngwdlch
  • 2,761
  • 1
  • 13
  • 20
10

You can use Theme to change the unselected widget color like given below

Theme(
  data: ThemeData(unselectedWidgetColor: Colors.blue),
  child: Checkbox(
   ... 
   )
 )
Ayush Bherwani
  • 2,409
  • 1
  • 15
  • 21
7

set color in BorderSide ⬇️

Checkbox(
    value: _value,
    onChanged: (_){},
    // YOUR COLOR HERE
    side: BorderSide(color: Colors.green)),
Tarish
  • 468
  • 8
  • 8
4

This code will help for border color change in onChanged Function.

Preview

Checkbox(
                    fillColor: MaterialStateProperty.all(Colors.transparent),
                    side: MaterialStateBorderSide.resolveWith((states) {
                      if(states.contains(MaterialState.pressed)){
                        return BorderSide(color: loginfontcolor);
                      }
                      else{
                        return BorderSide(color: loginfontcolor);
                      }
                    }),
                    checkColor: loginfontcolor,
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
                    value: snapshot.tapval, onChanged: (val){
                      snapshot.changval= val!;
                    },
                    
                    )
3

another dynamic way

 Checkbox(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(6.0).r,
            ),
            side: MaterialStateBorderSide.resolveWith(
              (states) => BorderSide(
                  width: 1.0,
                  color: valuefirst
                      ? context.resources.colors.primary600
                      : Color.fromRGBO(179, 172, 172, 1)),
            ),
            checkColor: context.resources.colors.primary600,
            activeColor: Colors.white,
            value: valuefirst,
            onChanged: (bool? value) {
              setState(() {
                valuefirst = value!;
                print('valuefirst$valuefirst');
              });
            },
          ),
Niaj Mahmud
  • 399
  • 3
  • 10
2

Simply like that:

        Checkbox(
          side: const BorderSide(
            // set border color here
            color: Colors.red,
          ),
          value: isChecked,
          onChanged: (bool? value) {},
        ),
NayMak
  • 160
  • 1
  • 16
2
Checkbox(
            fillColor: MaterialStateProperty.all(consts.darkWhiteColor),
                side: MaterialStateBorderSide.resolveWith((states) {
                  if(states.contains(MaterialState.selected)){
                    return const BorderSide(color: consts.navyColor);
                  } else {
                    return const BorderSide(color: consts.greyColor);
                  }
                }),
                shape: const RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(3.0))),
                value: checkValue.value,
                onChanged: widget.isDisabled
                    ? null
                    : (value) {
                        checkValue.value = value ?? false;
                        widget.onChanged!(value ?? false);
                      },
                materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                checkColor: consts.navyColor,
                activeColor: consts.darkWhiteColor,
              )
1
Checkbox(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(3),
      ),
      materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
      value: true,
      onChanged: (newValue) {},

      /// Set your color here
      fillColor: MaterialStateProperty.all(Colors.grey),
    )