Flutter TextFormField focused border color - flutter

This is my theme:
final ThemeData theme = ThemeData();
return MaterialApp(
title: 'Notes',
home: SignInPage(),
debugShowCheckedModeBanner: false,
theme: theme.copyWith(
primaryColor: Colors.green[800],
colorScheme: theme.colorScheme
.copyWith(secondary: Colors.green, secondaryVariant: Colors.green),
floatingActionButtonTheme: FloatingActionButtonThemeData(
backgroundColor: Colors.blue[900],
),
inputDecorationTheme: InputDecorationTheme(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
);
In the old days of flutter if you'd put in the primary color to green, the border of the focused text fields would turn green too. Now, I want all of my text fields, when in focus, to have a green border, green prefix icon and green label text, all from the root Theme. But this is the result I get with the code above:
I want the lock, the "password" label, and border to be all green when focused, and grey when not focused. How can I do this from the root Theme of the app. I have the primaryColor set up to green and even the colorScheme secondary color set up to green, but still everything is blue, instead of green.

Add this worked for me
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Color.fromRGBO(126, 132, 138, 1),),
),

The actual way now to change the whole text field colors is by changing the colorScheme.
colorScheme: theme.colorScheme.copyWith(
primary: Colors.green,
),

Related

how to disable the original text background color when selecting text in SelectableText.rich

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

Flutter 2: Color issues

I recently switched my code basis to Flutter 2.
Now I face certain problems with theming (colors):
The color of the device's status bar icons are black
The color of the TextField's context menu items are black
Previously they were white, so something seems to be changed in the new Flutter version.
I am using ThemaData.dark() and some specific colors. Especially I am setting cardColor to a dark grey to color the background of the TextField's context menu:
ThemeData buildTheme() {
final ThemeColors colors = themeColors();
final ThemeData base = ThemeData.dark();
return ThemeData(
scaffoldBackgroundColor: colors.primaryBackground(), // == dark grey
primaryColor: colors.primaryBackground(), // == dark grey
accentColor: colors.accent(), // == orange
textTheme: base.textTheme,
buttonTheme: base.buttonTheme.copyWith(
buttonColor: colors.accent(), // == orange
textTheme: ButtonTextTheme.primary
canvasColor: colors.inputBackground(), // == dark grey
inputDecorationTheme: base.inputDecorationTheme.copyWith(
hintStyle: TextStyle(color: colors.textFieldHintText()), // == grey
fillColor: colors.inputBackground(), // == dark grey
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: colors.focused())), // == green
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: colors.accent())), // == orange
),
unselectedWidgetColor: colors.accent(), // == orange
cardColor: colors.messageBackground()); // == dark grey
}
Do you know why the described colors don't turn into white when using the new Flutter version?
Do you know what I can do to fix that? Maybe change another color field of ThemeData?
My only idea is to remove the cardColor change, which yields a white context menu. But this does not fix the text color problem of the status bar.
For the statusBar only, you can set the brightness of the icons when defining the AppBar:
AppBar(
backwardsCompatibility: false,
backgroundColor: Colors.grey.shade800,
systemOverlayStyle: SystemUiOverlayStyle(
statusBarColor: Colors.grey.shade800,
statusBarIconBrightness: Brightness.light,
),
),
But with this settings your appbar wouldn't get the color from your theme.
You'll have to set background color and statusBarColor from there.
I did set Colors.grey.shade800 since I didn't have acces to your colors.
For the sistem UI, like the copy/paste, you can solve your problem by adding brightness: Brightness.dark inside your ThemeData.
I wouldn't modify the status bar colour in AppBar because then when you use a different app bar in a different part of the app, you might change it there.
I find it nicer to change the theme brightness, which might fix the issue with the text color theming too:
...
theme: ThemeData(
brightness: Brightness.dark,
...
)
...
or alternatively:
...
theme: ThemeData.dark().copyWith(
...
)
...
Otherwise you might just be using the wrong text theme.
I think your are just missing a tiny part.
You create a dark theme with ThemeData.dark() but then you create a light theme with return ThemeData(...)
Instead create the ThemeData directly with the intended brightness, all the values
that you do not specify will be defaulted depending on the brightness value in
the same way ThemeData.dark() does create them.
ThemeData buildTheme() {
final ThemeColors colors = themeColors();
return ThemeData(
brightness: Brightness.dark,
scaffoldBackgroundColor: colors.primaryBackground(), // == dark grey
primaryColor: colors.primaryBackground(), // == dark grey
accentColor: colors.accent(), // == orange
buttonTheme: ButtonThemeData(
buttonColor: colors.accent(), // == orange
textTheme: ButtonTextTheme.primary,
),
canvasColor: colors.inputBackground(), // == dark grey
inputDecorationTheme: InputDecorationTheme(
hintStyle: TextStyle(color: colors.textFieldHintText()), // == grey
fillColor: colors.inputBackground(), // == dark grey
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: colors.focused())), // == green
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: colors.accent())), // == orange
),
unselectedWidgetColor: colors.accent(), // == orange
cardColor: colors.messageBackground(), // == dark grey
);
}
If you look in the implementation of ThemeData here:
You find this: factory ThemeData.dark() => ThemeData(brightness: Brightness.dark);
And if you look here you can see that the default brightness is Brightness.light and in the following lines you can find out which defaults are created depending on the brightness value.
Be careful about using ThemeData.copyWith as there are no defaults set when calling this.

Change text color based on the surrounding color in flutter using ThemeData

I have a ThemeData defined as follow:
class VendorThemeData {
static ThemeData get themeData {
return ThemeData(
primaryColor: Colors.purple,
accentColor: Colors.orange,
textTheme: ThemeData.light().textTheme.copyWith(
bodyText1: TextStyle(
color: Color.fromRGBO(20, 51, 51, 1),
),
bodyText2: TextStyle(
color: Color.fromRGBO(20, 51, 51, 1),
),
headline6: TextStyle(fontSize: 20),
),
);
}
}
And I will use the static get in my MaterialApp widget:
MaterialApp(
home: ...,
theme: isVendorMode ? VendorThemeData.themeData : ClientThemeData.themeData,
....)
My question is how do I have flutter change the text color based on the surrounding color? For example:
return Scaffold(
appBar: AppBar(
title: Text(
"Menu overview",
style: Theme.of(context).textTheme.headline6,
),
backgroundColor: Colors.blueGrey,
),
I am using the headline6 text theme here for the title text in my appbar, I can't change the text color if I want to use my custom theme, the only way is to declare it white in my ThemeData, but then all my other headline6 will have a white color.
If I want the "Menu Overview" text to have a white color to contrast the background, is it possible for flutter to detect this is automatically change the text color for me?
Can copyWith solve your problem? It can overwrite the property locally.
Theme.of(context).textTheme.headline6.copyWith(color: Colors.white),

Weird Black Background in Flutter's OutlineButton Widget

I'm using Flutter's OutlineButton Widget, and I can't figure out how to remove that weird black background highlight when the button is clicked / pressed.
CLICK FOR VIDEO OF ISSUE
This is the button:
OutlineButton(
highlightElevation: 1.0,
onPressed: () => onRequestAllowLocation(context),
child: Text(
"ALLOW LOCATION",
style: TextStyle(fontWeight: FontWeight.w600, fontSize: 16),
),
borderSide: BorderSide(color: MyApp.accentColor, width: 2.0),
textColor: MyApp.accentColor,
shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(16.0))),
)
And here's the app's theme:
ThemeData(
fontFamily: 'Dosis',
brightness: Brightness.dark,
primarySwatch: Colors.blue,
accentColor: accentColor,
highlightColor: highlightColor,
buttonColor: accentColor,
indicatorColor: accentColor,
backgroundColor: primaryColor,
scaffoldBackgroundColor: primaryColor,
primaryColor: primaryColor,
)
P.S. None of the const colors I provide above are black.
It's the shadow. Stop setting highlightElevation and it will go away. From OutlineButton class docs:
The button's highlightElevation, which defines the size of the drop shadow when the button is pressed, is 0.0 (no shadow) by default. If highlightElevation is given a value greater than 0.0 then the button becomes a cross between RaisedButton and FlatButton: a bordered button whose elevation increases and whose background becomes opaque when the button is pressed.

Change the Highlight Color of selected text

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