Flutter ChoiceChip selected Text color - flutter

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

Related

Flutter – CheckboxListTile - Override title style

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

BottomNavigationBar // Cannot control item label color

Goal: I want to give the item label a specific font and color depending on if it is selected or not.
My approach: As the label cannot be styled directly, I'm using the properties "unselectedLabelStyle" and "selectedLabelStyle".
The Problem:
The properties work for font and fontweight, but I cannot directly control the text color
I can influence the label color of the selected item; but not with the "selectedLabelStyle" property, but with the color I specifiy under "seltectedItemColor".
"unselectedLabelStyle" also works for font and fontweight, but not for the color. I cannot find a property, that would allow me to change the color. > THIS IS THE PROBLEM
Pretty picture (code below):
The code:
BottomNavigationBar(
elevation: 0,
onTap: (index) => selectPage(index),
currentIndex: selectedPageIndex,
selectedItemColor:
Provider.of<CustomColors>(context).customColorScheme['Dark Teal'],
unselectedLabelStyle:
Provider.of<CustomTextStyle>(context, listen: false)
.customTextStyle('IconLabel'),
selectedLabelStyle:
Provider.of<CustomTextStyle>(context, listen: false)
.customTextStyle('IconLabel'),
backgroundColor: Colors.white,
type: BottomNavigationBarType.fixed,
items: [
//home
bottomNavIcon(
context: context,
icon: Image.asset(
"assets/icons/icon_home.png",
),
icon_active: Image.asset("assets/icons/icon_home.png",
color: Provider.of<CustomColors>(context)
.customColorScheme['Dark Teal']),
label: 'Home',
),
//favorite
bottomNavIcon(
context: context,
icon: Image.asset("assets/icons/icon_favorite.png"),
icon_active: Image.asset("assets/icons/icon_favorite.png",
color: Provider.of<CustomColors>(context)
.customColorScheme['Dark Teal']),
label: 'Favorite',
),
//loockback
bottomNavIcon(
context: context,
icon: Image.asset("assets/icons/icon_lookback.png"),
icon_active: Image.asset("assets/icons/icon_lookback.png",
color: Provider.of<CustomColors>(context)
.customColorScheme['Dark Teal']),
label: 'Lookback',
),
//info & support
bottomNavIcon(
context: context,
icon: Image.asset("assets/icons/icon_info.png"),
icon_active: Image.asset("assets/icons/icon_info.png",
color: Provider.of<CustomColors>(context)
.customColorScheme['Dark Teal']),
label: 'Info & Support',
),
],
),
Code for the icons
BottomNavigationBarItem bottomNavIcon(
{required BuildContext context,
required Image icon,
required Image icon_active,
required String label}) {
return BottomNavigationBarItem(
icon: Padding(
padding: EdgeInsets.only(top: 6.h, bottom: 3.h),
child: icon,
),
activeIcon: Padding(
padding: EdgeInsets.only(top: 6.h, bottom: 3.h),
child: icon_active,
),
label: label,
);
}
If you want the unselected items to have a certain color, use:
unselectedItemColor: Colors.red,
This will change the color of label and icon both for unselected items.
Example:
Unfortunately, unselectedLabelStyle property works for changing font weight, font size etc but not for color.
Also check this answer for unselectedLabelstyle don't work in Flutter
As the docmumentation say's, you can change the text color using the color property of TextStyle : https://api.flutter.dev/flutter/painting/TextStyle-class.html
Once this say's perharps BottomNavigationBar overide the color when setting selectedItemColor and/or unselectedItemColor
After giving a try, effectively color is overide even if you don't provide selectedItemColor and/or unselectedItemColor

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(...)]);

How do I change the check icon color in flutter filterChip

I need a way to change the check (✔) color to white. How do I achieve that
You can change the color of the checkmark by using checkMarkColor property
FilterChip(
label: Text('Pizza'),
shape: StadiumBorder(side: BorderSide()),
checkmarkColor: Colors.red,
backgroundColor: Colors.transparent,
),
Black or white checkmark
You can change the color to either black or white based on a light or dark theme. You can change the theme globally or wrap the specific widget within a Theme widget.
Theme(
data: ThemeData(
brightness: Brightness.dark
), // or shorthand => ThemeData.dark()
child: FilterChip(
label: Text('My chip'),
onSelected: (value) {
// ...
},
),
);
Other colors
There is currently no way to change the color of the checkmark in the FilterChip provided by the Material package to an arbitrary color. The way the checkmark is drawn can be found in the source code for the Chip classes here.
For future reference, this is the part of code that draws the checkmark:
void _paintCheck(Canvas canvas, Offset origin, double size) {
Color paintColor;
switch (theme.brightness) {
case Brightness.light:
paintColor = theme.showAvatar ? Colors.white : Colors.black.withAlpha(_kCheckmarkAlpha);
break;
case Brightness.dark:
paintColor = theme.showAvatar ? Colors.black : Colors.white.withAlpha(_kCheckmarkAlpha);
break;
}
...
So it is only able to show either as black or white right now. If you want it colored, you'll have to resort to a custom widget.
You could also chime in on the already opened issue on Flutters Github project.
You can simply wrap your FilterChip in a Theme widget, and set the desired ThemeData without changing your general app style.
Theme(
data: ThemeData.dark(), // White color. Select light for black.
child: FilterChip(
label: Text('Test'),
selected: _selected,
onSelected: (val) {
setState(() => _selected = val);
},
backgroundColor: Colors.blue,
selectedColor: Colors.pink,
labelStyle: TextStyle(
color: Colors.white,
),
),
),
You can work around this by using an Avatar and customising this to represent the tick mark, e.g.
FilterChip(
label: Text(
"label text",
style: TextStyle(color: Colors.red),
),
avatar: isSelected ? Icon(Icons.check, color: contrastColor(Colors.red)) : null,
// selected: isSelected,
},
)
The function contrastColor just looks at the supplied colour and chooses white or black depending upon its luminescence.
The animation effects when toggling ruin this slightly, so you may want to keep an 'empty' avatar rather than null if it looks bad.
Wrapping the FilterChip with a Theme sort of works but, for me at least, it rendered the background colours incorrectly (too dark) when flipping between light and dark themes.

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