Flutter – CheckboxListTile - Override title style - flutter

In a CheckboxListTile, when we check the tile, the check box and the title change their color. What I need to achieve is to split the behavior and keep my title widget with the default color.
In this example, I'm trying to replace the title color but the default behavior is still in place and change the title color the same way as the checked icon:
https://dartpad.dev/?id=7f55df6ce3ba094cfe1db680ccc4b400
The only way I found to achieve the goal is to set directly in the widget the color.
Text(
'Select Example',
style: TextStyle(color: Colors.red),
)
I know I can do a custom item do reach this objective but my question is if possible to override the title widget color when the tile is selected in the CheckboxListTile Flutter widget?
Thanks for reading!

Its also weird for me that there are no separate properties for checkbox and text theme, but this is how you can achieve it.
return CheckboxListTile(
title: DefaultTextStyle(
style: const TextStyle(color: Colors.white),
child: widget.child,
),
tileColor: Colors.black,
selectedTileColor: Colors.black,
selected: isSelected,
dense: true,
value: isSelected,
onChanged: (v) => setState(() => isSelected = !isSelected),
controlAffinity: ListTileControlAffinity.leading,
);

Related

how I can change the distance between text and tile

So I have two SwitchListTiles on my modal Page, and the problem is that the text doesn't fit into it. (it takes 3 lines, but should 1)
How can I make it to be in one single line, without decreasing it's font size
child: SwitchListTile(
value: state.filter.flagId == null ? false : true,
onChanged: (newValue) =>
context.read<FilterBloc>().add(HotPressed(newValue)),
title: Text(
AppLocalizations.of(context)!.hotAdds.capitalize(),
style: FlutterFlowTheme.dark50016.copyWith(),
),
tileColor: FlutterFlowTheme.white,
activeColor: FlutterFlowTheme.primaryColor,
dense: true,
controlAffinity: ListTileControlAffinity.trailing,
),
I was thinking that the possible answer could be to decrease the distance between tile, but don't know how to do it
Just add "maxLines: 1" to Text widget
title: Text(
AppLocalizations.of(context)!.hotAdds.capitalize(),
style: FlutterFlowTheme.dark50016.copyWith(),
maxLines: 1,
),
There is default padding on the outer edges and also between the text and the switch. From what I can see the outer padding (the padding to the right of the switch and to the left of the text) defaults to 16. You can change this by adding contentPadding as an input to SwitchListTile and set the value to something smaller than 16:
contentPadding: EdgeInsets.symmetric(horizontal: 5),
But I'm not sure if you can change the padding between the text and the switch.
wrap your text widget to Flexible and trim() your text to avoid enter(\n) as string. maxLines =1.
title: Flexible(
child: Text(
AppLocalizations.of(context)!.hotAdds.capitalize().trim(),
style: FlutterFlowTheme.dark50016.copyWith(),
maxLines: 1,
),
),

Flutter ChoiceChip selected Text color

How to set a specific color to the text of the selected chip with a ChoiceChip widget ?
"selectedColor" is only setting the background color of the selected chip.
Use labelStyle property to set text-color of ChoiceChip.
ChoiceChip(
label: Text('Hi'),
selected: isSelected,
labelStyle: TextStyle(
color: isSelected ? Colors.blue : Colors.red,
),
),

Reduce space between checkbox and title in flutter

I need to remove the space between checkbox and title but i cant able to reduce the space
here is my code
Expanded(
//width: 30,
child: CheckboxListTile(
checkColor: Colors.white,
activeColor: PRIMARY_COLOR,
title: Text('Select All',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: Colors.black)),
value: _selectAll,
dense: true,
contentPadding: EdgeInsets.fromLTRB(0, 0, 0, 0),
controlAffinity: ListTileControlAffinity.leading,
onChanged: (bool value) {
}
}
setState(() {});
}),
),
here is the image how it is coming
How to reduce the space
You need to create a custom widget in order to resolve your problem, because if you're using the CheckboxListTile the way it is, you're gonna have to add more contentPadding to your widget in order to make the gap smaller and by default, I see that it has a default padding between the actual checkbox and the text widget ... I personally, don't know why you're trying to squeeze more space from this thing, because in documentation it says clearly that CheckboxListTile acts just like a ListTile, which in my knowledge it takes all the space available in a row and the padding between items in your widget will be a little bit bigger. Also your widget is wrapped in an Expanded widget which also take the whole space available. So in conclusion you either use it as the way it is intended, or you can create a custom widget that will bennefit your problems.

Flutter - CheckboxListTile - Border color white

I don't have any idea how to change the color of checkbox border and move the text closer. Below is my code and image of I want to achieve.
Thanks!
CheckboxListTile(
title: const Text('Remember Me'),
value: _isSelected,
onChanged: (bool value) {
setState(() {
_isSelected = value;
});
},
controlAffinity: ListTileControlAffinity.leading,
),
Try this solution:
Theme(
data: ThemeData(unselectedWidgetColor: Colors.white),
child: CheckboxListTile(....),
)
these two property check color and active color helps you to change the color of check box ..
for moving closer the text .You can use Container or Row :
you can use row with main axis alignment.
If someone tries to change the border of the Checkbox list tile, not the checkbox itself.
CheckboxListTile(
title: const Text('text'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0), // Optionally
side: const BorderSide(color: Colors.pink),
),
value: true,
onChanged: (_) {},
)
Makes it look like that.
You can use Checkbox inside a Row
Row(children:[Checkbox(...), Text(...)]);

Custom style or theme for Expansion Tile Header, Flutter

Is there any way to apply custom theme for ExpansionTile. In my case, I want to have the different background colour for Header and children of the expansion tile but When ever the ExpansionTile is expanded, Headers background color changes to that of children?
To apply a custom Theme to any widget, just wrap it with the Theme() widget.
and then specify your custom theme in the data field of the widget.
Theme(
data: ThemeData(/*specify your custom theme here*/),
child: ExpansionTile() /*or any other widget you want to apply the theme to.*/
)
In your case, to customise the header in ExpansionTile,
when ExpansionTile is closed
the style of header text i.e. title depends on
ThemeData.textTheme.subhead (in flutter 2 it's ThemeData.textTheme.subtitle1)
whereas, the style of the arrow icon depends
on ThemeData.unselectedWidget (in flutter 2 it's ThemeData.unselectedWidgetColor)
when ExpansionTile is open
the color of both the widgets depends on ThemeData.accentColor
In a similar fashion you can customise almost any part of the expansion tile by tweaking the theme. For more details check out this link.
Since flutter is built with flexibility in mind. You can do almost anything you want. Create almost any UI you want.
Following the idea of #user3315892, you can implement your own stateful widget for the ExpansionTile, so that you can control what colours you want the header and children to be when the header is expanded or collapsed.
The following example only changes the text foreground and background colours of the header when the expansion tile is expanded or collapsed, but you can do the same for the children widgets.
class CustomExpansionTile extends StatefulWidget {
#override
State createState() => CustomExpansionTileState();
}
class CustomExpansionTileState extends State<CustomExpansionTile> {
bool isExpanded = false;
#override
Widget build(BuildContext context) {
return ExpansionTile(
title: Container(
child: Text(
"HEADER HERE",
style: TextStyle(
color: isExpanded ? Colors.pink : Colors.teal,
),
),
// Change header (which is a Container widget in this case) background colour here.
color: isExpanded ? Colors.orange : Colors.green,
),
leading: Icon(
Icons.face,
size: 36.0,
),
children: <Widget>[
Text("Child Widget One"),
Text("Child Widget Two"),
],
onExpansionChanged: (bool expanding) => setState(() => this.isExpanded = expanding),
);
}
}
I found another way to do it. I don't know if this is the best way but it's simpler for me.
To change the ExpansionTile color, you can wrap it with a Container and give it a color. This will change the entire color of the ExpansionTile, both collapsed and expanded.
But if you want a different color for the children, you can also wrap them with a Container and set another color there. However, keep in mind that you have to "expand" that Container to occupy all the available space (you can do it by setting width and height parameters properly).
Btw, that doesn't change the arrow color.
Widget expansionTileTest(context) {
return Container(
color: Colors.grey,
child: ExpansionTile(
title: Text(
"Title",
style: TextStyle(color: Colors.black),
),
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height * 0.1,
width: MediaQuery.of(context).size.width,
color: Colors.yellow,
child: Center(child: Text("Hi")),
),
],
),
);
}
Collapsed
Expanded
You can set color of header and then body/children wrap in Container and set color to that container.
I used this code to make the trailing ion color Black.
Theme(
data: Theme.of(context).copyWith(accentColor: Colors.black),
child: ExpansionTile(
backgroundColor: Colors.white,
title: Text(
'Title Exmample',
style: TextStyle(color: Colors.black),
),
leading: CircleAvatar(
backgroundColor: Colors.black.withOpacity(.07),
child: Icon(
Icons.apps_outlined,
color: Theme.of(context).primaryColor,
)),
children: _buildTiles(Theme.of(context).primaryColor, Colors.black.withOpacity(.07), context),
),
);
If you want the ExpansionTile title to remain the same color whether closed or expanded, you can set it's style:
ExpansionTile(
title: Text('HEADER HERE', style: TextStyle(color: Colors.red)),
...
),
Additional styling for ExpansionTile is an open feature request:
Feature request: More styling of ExpansionTile #15427