Flutter Checkbox background [duplicate] - flutter

This question already has answers here:
Change checked mark color of checkbox in flutter
(7 answers)
Closed 22 days ago.
The main problem of Checkbox widget is that it has transparent background, and the way it takes the space is around checkbox itself, so even if you managed to wrap it with container it will change the space around checkbox also.
I'm looking for a best non-package solution if there is any, because making it like 'two pictures changing' with no animation feels weird.
To be more clear I'm adding code with wrapping it with Container, I want green color only inside checkbox
Widget build(BuildContext context) {
return Container(
color: Colors.green,
child: Checkbox(
value: false,
fillColor: MaterialStateProperty.all(Colors.white),
onChanged: (value) {},
),
);
}
I want make it inside color white or dark green for example.
Another example from Figma

you have to way to change checkColor
first used checkColor and fillColor property
Checkbox(
value: isCheck,
checkColor: Colors.green, // color of tick Mark
fillColor: MaterialStateProperty.all(Colors.red),
onChanged: (value) {
setState(() {
isCheck = value!;
});
},
),
Second used checkColor and fillColor property
Checkbox(
value: isCheck,
checkColor: Colors.green, // color of tick Mark
activeColor: Colors.red,
onChanged: (value) {
setState(() {
isCheck = value!;
});
},
),
the difference between fill Color and active color than fill Color is change unselect border color

Related

How to change the thickness of flutter checkbox

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!;
});
})

How to make the icon in TextFormField enabled in code?

I have set a color to an icon in a TextFormField, but the color only comes when the TextFormField is focused.
How do I set it so that upon a setState() the icon stays 'focused' or colored?
try this:
bool _textIsEmpty = true;
TextEditingController _controller= TextEditingController();
... // rest of your code
TextField(
controller: _controller,
decoration: InputDecoration(
icon: Icon(
Icons.abc,
color: _textIsEmpty ? Colors.transparent : Colors.pink,
),
),
onChanged: (value) {
setState(() {
_textIsEmpty = value.isEmpty;
});
},
),

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),
)

On web, how do I control the visibility icon that automatically appears on a focused TextFormField that has an obscureText property set to true?

Here's my code for the password field:
TextFormField(
obscureText: isObscure,
decoration: InputDecoration(
suffix: TextButton(
child: isPasswordObscure
? Text(
'Show',
style: TextStyle(color: Colors.grey),
)
: Text(
'Hide',
style: TextStyle(color: Colors.grey),
),
onPressed: () {
setState(() { isObscure = !isObscure; });
},
),
),
)
If I run it, the password field would look like this:
If you review my code, I only specified a text button and not an icon as the suffix. The visibility icon was added by Flutter Edge and when I click on it, it only changes its icon and does not unobscure or obscure the text field.
What I want to know is how do I change or remove the icon? And maybe also give it a callback so it knows what to do when I click on it.
The problem doesn't exist on mobile, only on browsers desktop Edge.
Edit:
I tried setting suffix and suffixIcon to null but the visibility icon is still showing.
Update: I've discovered that the problem only exists on MS Edge.
If you wants to turn off the visibility icon set onPressed: () {},
also if you want to remove the visibility icon form overview wrap it with opacity widget
Opacity(
opacity: 0.0,
child: textButton(),
Please find the below code sample to include the visibility option for the textField. by including a variable _isObscured in a stateful widget. we have implemented it with the auto obscure after 2 second delay.
Center(child: TextField(
obscureText: _isObscured,
decoration : InputDecoration(
suffix:InkWell(
onTap: (){
setState(() => this._isObscured =
!this._isObscured);
Future.delayed(Duration(seconds: 2), (){
setState(() => this._isObscured =
!this._isObscured);
});
},
child: Icon( Icons.visibility),
),
),
),
),
),
I found a solution:
// the magic function
void fixEdgePasswordRevealButton(FocusNode passwordFocusNode) {
passwordFocusNode.unfocus();
Future.microtask(() {
passwordFocusNode.requestFocus();
js.context.callMethod("fixPasswordCss", []);
});
}
// widget code
child: TextField(
onChanged: (_) async {
fixEdgePasswordRevealButton(passwordFocusNode);
},
focusNode: passwordFocusNode,
obscureText: true,
// end of index.html
window.fixPasswordCss = () => {
let style = document.createElement('style');
style.innerHTML = '::-ms-reveal { display: none; }';
document.head.appendChild(style);
}
</script>
</body>
Also posted on the relevant issue.

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
),