How to change the thickness of flutter checkbox - flutter

After searching, I found a way to change the colors of the checkbox, but I did not find a way to change the thickness
Is there any way other than using custom checkbox
checkboxTheme: CheckboxThemeData(
checkColor: MaterialStateProperty.all(kWhiteColor),
fillColor: MaterialStateColor.resolveWith((states) {
if (states.contains(MaterialState.selected)) {
return kPrimaryColor; // the color when checkbox is selected;
}
return Colors.grey
.withOpacity(0.4); //the color when checkbox is unselected;
}),
thanks

In flutter, the checkBox widget comes with a property named side .
Using the side property, one can easily change the check box's style, color, width, etc.
Code snippet example:
Checkbox(
side: MaterialStateBorderSide.resolveWith(
(Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
return const BorderSide(width: 3, color: Colors.red);
}
return const BorderSide(width: 2, color: Colors.green);
},
),
value: checkBoxValue,
onChanged: (bool? updatedValue) {
setState(() {
checkBoxValue = updatedValue!;
});
})

Related

How do i put checks in my state-management in flutter?

i have a controller that's responsible for hiding and showing a checkbox when a button is pressed.
This is the Controller
class CustomersController extends GetxController {
var isCheboxVisible = false.obs;
void toggleCheckbarVisibility() {
isCheboxVisible.value = !isCheboxVisible.value;
}
}
This is the view
Visibility(
visible: controller.isCheboxVisible.value,
child: Transform.scale(
scale: 1.3,
child: Checkbox(
side: MaterialStateBorderSide.resolveWith(
(Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
return const BorderSide(
width: 2, color: Color(0xff34495E));
}
return const BorderSide(
width: 1, color: Color(0xffB0BEC1));
},
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5)),
activeColor: Color(0xff34495E),
//materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
//visualDensity: VisualDensity(horizontal: -4, vertical: -4),
value: isChecked,
onChanged: (value) {
setState(() {
this.isChecked = value;
});
},
),
),
)
This is the button that changes the state when clicked
onPressed: () {
customersController.toggleCheckbarVisibility();
},
The problem i have is, sometimes before clicking the button to show the checkbox, the checkbox is already visible and then clicking the button will hide the checkbox which is the opposite of what i want to do.
I want the checkbox to be hidden all the time, and only show when the button is clicked, and then hide when the button is clicked again and this should only be the case.
How do i put this checks in place? Thanks.
I have tried using if's statements in place but i don't seem to be getting it right.

How to change flutter button border color based on MaterialState?

Is there a way to change the flutter MateriaButton border color (or any other properties) based on the MaterialState?
ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color?>((states) => ...),
overlayColor: MaterialStateProperty.resolveWith<Color?>((states) => ...),
side: MaterialStateProperty.resolveWith<BorderSide?>((states) => {
if (states.contains(MaterialState.pressed)) {
return BorderSide(color: Colors.Blue);
}
return BorderSide(color: Colors.Red);
}),
)
Trying to set the border in this way doesn't seem to have any effect on the side, but it stays always red. Both backgroundColor and overlayColor seem to change based on the state, but the side property doesn't. Is there a way to achieve this?
Edit: Setting the side property this way seems to work after all. Also the way proposed by #nagendra nag works well too. The actual problem seems to be that the animation seems to be slow, so the change isn't visible unless the button is pressed for a short moment.
Try this
shape: MaterialStateProperty.resolveWith(
(states) {
if (states.contains(MaterialState.focused)) {
return const RoundedRectangleBorder(
side: BorderSide(color: Colors.red),
);
}
if (states.contains(MaterialState.pressed)) {
return const RoundedRectangleBorder(
side: BorderSide(color: Colors.green),
);
}
if (states.contains(MaterialState.hovered)) {
return const RoundedRectangleBorder(
side: BorderSide(color: Colors.blue),
);
}
},
),
),

Change the text color of an ElevatedButton in Flutter with ButtonStyle

I want a button that:
Changes its background color based on whether it's in the pressed, disabled or normal state
Changes its text color depending on whether it's in the disabled or normal state
I'm trying to achieve this with the ButtonStyle class.
ElevatedButton(
child: Text("Example"),
onPressed: onPressed, // onPressed is a function
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith((states) {
if (states.contains(MaterialState.disabled)) { return KPColors.offWhite; }
if (states.contains(MaterialState.pressed)) { return KPColors.primaryLight; }
return KPColors.primaryExtraLight;
}),
textStyle: MaterialStateProperty.resolveWith((states) {
Color textColor = states.contains(MaterialState.disabled) ? KPColors.gray : KPColors.primary;
return TextStyle(fontSize: 18, color: textColor);
}),
overlayColor: MaterialStateProperty.all(KPColors.clear), // prevents the shimmer effect when pressing the button
shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(16))), // Rounds the corners
elevation: MaterialStateProperty.all(0), // Prevents shadow around button
),
),
The code succeeds in changing the background color of the button, but not the text color, which appears white instead of my custom color. I think this is because ElevatedButton's child is a Text widget, which has a default text color which is overriding mine.
How can I solve this? I already know that I can change the text color of the button by using ElevatedButton.styleFrom(...) and setting the onPrimary property, instead of ButtonStyle, but this would make it much more difficult to have different colors depending on the pressed and disabled states of the button.
you need to set the foregroundColor in the ButtonStyle
For example:
foregroundColor: MaterialStateProperty.resolveWith((states) {
if (states.contains(MaterialState.disabled)) { return Colors.grey; }
if (states.contains(MaterialState.pressed)) { return Colors.green; }
return Colors.blue;
})
you can use ElevatedButton.styleFrom
ElevatedButton(
child: Text("Example",style:TextStyle(color:isActive ? Colors.white : Colors.black)),
onPressed: isActive ? (){print('do somthing');} : (){}, // onPressed is a function
style: ElevatedButton.styleFrom(primary: isActive ? Colors.blue : Colors.grey),
)

How to change border color in Flutter according to the value?

I have a container widget with a border of a certain color.
I want to be able to change the color of the border if the entered value in a TextField is greater or less than some value. What would be the best way to acheive this?
Define a _color variable in your class:
Color _color = Colors.purple;
Assign the _color variable to the Container's border:
Container(
height: 100,
width: 100,
decoration: BoxDecoration(
border: Border.all(
width: 5.0,
// assign the color to the border color
color: _color,
),
),
),
Test if your condition is met in the onChanged callback of the TextField and change the _color according to your needs:
TextField(
onChanged: (newValue) {
if (newValue.length > 5) { // test for your condition
setState(() {
_color = Colors.red; // change the color
});
} else {
setState(() {
_color = Colors.blue; // change the color if the condition is not met
});
}
},
),
This is your TextField COntroller;
final yourTextFieldControler = TextEditingController();
Just getValue From controller and change to int.
Container(
decoration: BoxDecoration(
border: Border.all(color: yourTextFieldControler.text.toInt > 5 ? Colors.red:Colors.blueAccent)
),
child: Text("My Awesome Border"),
)
This is your TextField
TextField(
controler : yourTextFieldControler,
onChanged: (newValue) {
setState((){})
}

How do I change the tick color for CheckboxListTile

I have my checkbox widget all set up and would like to change the tick color to green when selected (currently it is white). So I managed to change the color of the checkbox when it is un-selected to white by adding a theme. I want to change the selected tick color to green, however I cant seem to find the right option under theme to do so.
Code:
Widget buildResultTile(data) {
return Theme(
data: ThemeData(unselectedWidgetColor: white),
child:
CheckboxListTile(
activeColor: transparent,
title: AutoSizeText(
data,
maxLines: 1,
style: TextStyle(
color: white,
),
),
value: _serachSelectList.contains(data),
onChanged: (bool value) {
setState(() {
if (value) {
_serachSelectList.add(data);
} else {
_serachSelectList.remove(data);
}
});
},
secondary: const Icon(Icons.account_box, color: white),
)
);
}
Un-selected:
Selected (I want only the tick to be Colors.green):
To change the fill color of the checkbox in a CheckboxListTile, you can simply set the toggleableActiveColor property in your ThemeData:
ThemeData(
// ... other theme values ...
toggleableActiveColor: Colors.green
)
Changing the color of the tick mark is unfortunately impossible with CheckboxListTile since it doesn't expose the checkColor property of its Checkbox widget, so you are stuck with rolling your own list tile.
You can use the checkColor property of the CheckboxListTile-Widget to set the color of the checkmark:
CheckboxListTile(
checkColor: Colors.black54, //sets checkmark color
// ... other properties ...
)
(see https://github.com/flutter/flutter/pull/37636)
To change the fill color of the checkbox see the answer of Magnus https://stackoverflow.com/a/56941539/702478
You need to ditch the CheckboxListTile and instead use a Row with an icon, text, and a simple Checkbox widget.
Checkbox provides checkColor property - which is responsible for the check/tick color and is white by default. Set the color you desire for that property and it should work.
e.g.
Row(crossAxisAlignment: CrossAxisAlignment.center, children: [
Icon(Icons.account_box, color: Colors.white),
Expanded(
child: AutoSizeText(
data,
maxLines: 1,
style: TextStyle(
color: white,
),
)
),
Checkbox(
value: _serachSelectList.contains(data),
onChanged: (bool value) {
setState(() {
if (value) {
_serachSelectList.add(data);
setState((){ default_color = selected_color });
} else {
_serachSelectList.remove(data);
setState((){ default_color = unselected_color });
}
});
},
checkColor: Colors.green,
activeColor: Colors.transparent,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
]);
I did not test this code - you might encounter size & alignment issues that you would need to solve yourself. Nevertheless, the general idea is valid.
Let me know if this helped.
please try this pesudo code,
Color selected_color = Colors.green;
Color unselected_color = Colors.transparent;
Color default_color = unselected_color ;
Widget buildResultTile(data) {
return Theme(
data: ThemeData(unselectedWidgetColor: white),
child:
CheckboxListTile(
activeColor: default_color,
title: AutoSizeText(
data,
maxLines: 1,
style: TextStyle(
color: white,
),
),
value: _serachSelectList.contains(data),
onChanged: (bool value) {
setState(() {
if (value) {
_serachSelectList.add(data);
setState((){ default_color = selected_color });
} else {
_serachSelectList.remove(data);
setState((){ default_color = unselected_color });
}
});
},
secondary: const Icon(Icons.account_box, color: white),
)
);
}
i hope to help you
it's working code for checkbox color change for flutter
checkColor: Colors.white,
activeColor: Colors.red,
CheckboxListTile(
checkColor: Colors.white,
activeColor: Colors.red,
value: checkedValue,
onChanged: (newValue) {
setState(() {
checkedValue = newValue!;
});
},
controlAffinity:
ListTileControlAffinity.leading, // <-- leading Checkbox
),