When I have 2 radio buttons, where one is selected and the other isn't selected, one of them would be the active color, while the other is grey, so is there any way to change the unselected color from grey to white?
Set unselectedWidgetColor prop of theme in MaterialApp like below:
return new MaterialApp(
title: 'xyz',
home: xyz(),
theme: ThemeData(
brightness: Brightness.dark,
unselectedWidgetColor:Colors.white
),
);
A useful tip that read the source code of radio.dart and you will get everything!
Sure, you have to change the ThemeData of the container of your Radio buttons.
Assuming that you are using a Row Container:
Theme(
data: ThemeData.dark(), //set the dark theme or write your own theme
child: Row(
children: <Widget>[
Radio(
//your attributes here...
),
Radio(
//your attributes here...
)
],
),
)
How it works?
Because if you check the code of the ThemeData, you'll see this validation
unselectedWidgetColor ??= isDark ? Colors.white70 : Colors.black54;
then dark theme is using white70
In case you want only to change Border Color for Widget when it's unselected
Just Customize Fill Color property
Radio(
//here -----
fillColor:
MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
return (isSelected)
? CustomColors.green
: CustomColors.greyMedium;
}),
// ----
value: 'en',
groupValue: langValue,
activeColor: Theme.of(context).primaryColor,
onChanged: (value) {},
),
Wrap your Radio Widgets with Theme widget and add theme data with unselectedWidgetColor as color you want
Ex:
Theme(
data: ThemeData(
//here change to your color
unselectedWidgetColor: Colors.White,
),
child: Row(
children: <Widget>[
Radio(),
Radio()
],
),
);
Color getColor(Set<MaterialState> states) {
return Colors.white;
}
Radio(value: 1,groupValue: numTypeRadio,onChanged: onChangeRadio,fillColor: MaterialStateProperty.resolveWith(getColor),)
For global setting: Setting for an unselected color can be set in the theme with unselectedWidgetColor. However, using fillColor in RadioThemeData affects unselectedWidgetColor. The best practice is to use unselectedWidgetColor in theme and activeColor in the custom widget.
Related
Is there some way to disable the original text background color when selecting text in SelectableText.rich.
Otherwise the original background color(amber for example) is still there, which is different from the selected text background color (blue), and thus making it look like it is not selected.
Wrap it with theme and give it color i hope it helps
Theme(
data: ThemeData(textSelectionColor: Colors.green),
child: SelectableText.rich(
),
),
You need to set your select color first in ThemeData like this:
MaterialApp(
theme: ThemeData(
// use textSelectionTheme and set your needed color instead
textSelectionTheme: TextSelectionThemeData(
selectionColor: Colors.amber,
),
),
),
if need to use just this SelectableText.rich with specific color just wrap your SelectableText.rich with theme:
Theme(
data: ThemeData(
textSelectionTheme: const TextSelectionThemeData(
selectionColor: Colors.amber,
// selectionColor: Colors.transparent, // it can make select color transparent that you need or make that backgroundColor: Color.transparent
),
),
child: const SelectableText.rich(
TextSpan(
children: [
TextSpan(
text: 'test ',
style:
TextStyle(color: Colors.black, backgroundColor: Colors.amber
// this backgroundColor make the white backcolor into amber
),
),
],
),
toolbarOptions: ToolbarOptions(cut: true), // tools like copy, paste
enableInteractiveSelection: false, // you can enable select color
cursorColor: Colors.red, // this is an option if you want change
),
),
When I wrote the following code, I expected the text to be red:
Theme(
data: Theme.of(context).copyWith(
textTheme: Theme.of(context).textTheme.copyWith(
bodyText2: TextStyle(color: Colors.red),
),
buttonTheme: Theme.of(context)
.buttonTheme
.copyWith(buttonColor: Colors.yellow),
),
child: Builder(
builder: (context) {
return Column(
children: [
Text('Flutter'),
Text('is awesome!'),
RaisedButton(
onPressed: () {},
child: Text('OK'),
)
],
);
},
),
)
but the text was black while the button was yellow as you can see:
here
As you can see the Text widgets ignored the style defined in the theme while the RaisedButton didn't. Why?
I know that I can use DefaultTextStyle instead but I'm trying to understand why is that not working like I expected.
Text widget has style property. As we can see from docs:
If the [style] argument is null, the text will use the style from the
closest enclosing [DefaultTextStyle].
It is clear, Text uses style from DefaultTextStyle widget (not from Theme) if you didn't specify it as you did. If you want to use style from theme you should specify it explicitly:
Text('Flutter', style: Theme.of(context).textTheme.bodyText2)
As for buttons - any MaterialButton child (RaisedButton too) use ButtonTheme.of(context).textTheme as default value of textTheme property.
Is there any way to change the colour of a Textfield's highlight in Flutter?
So when I highlight it does not look like this:
Thank you!
Solution 1. You can change the textSelectionColor in the theme of your app:
theme: ThemeData.dark().copyWith(
textSelectionColor: Colors.black
),
Solution 2. You can change the textSelectionColor only for a specific TextField by wrapping it with a Theme widget:
Theme(
data: ThemeData(
textSelectionColor: Colors.black,
),
child: TextField(
),
),
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.
When the user select a text from inside a TextField, the default highlight color is blue. How to change it to green for example ?
2021 answer
Wrap with Theme and use copyWith to preserve other theme data.
Theme(data: Theme.of(context).copyWith(
textSelectionTheme: TextSelectionThemeData(
selectionColor: Colors.green)),
child: TextFormField()
)
Wrap your text widget with theme and assign the color to the textSelectionColor property inside ThemeData
refer below code for same:- I have changed the text selection color to green
Theme(
data: ThemeData(textSelectionColor: Colors.green),
child: TextField(
controller: _inputController,
decoration: InputDecoration(hintText: "Input"),
),
),
change the value of textSelectionColor of your ThemeData and it will give you the result you are looking for.
please use this code.
Widget build(BuildContex contex){
return MaterialApp{
debugShowCheckedModeBanner: false,
theme: ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.green,
iconTheme: IconThemeData(
color: kGreenColor,
),
hoverColor: kPrimaryColor,
indicatorColor: kPrimaryColor,
),
}
}