I've been struggling with attempting to modify the accent colour in FlutterFire UI.
Namely, I'd like to change the blue accent colour here to a different material colour, such as purple. I've messed around with the app theming to no avail, as none of the ThemeData parameters seem to influence this colour so far. I was wondering if this was possible? Thanks!
accent color is deprecated so now u can user Colorscheme instead like this
MaterialApp(
theme: ThemeData(
primarySwatch: Colors.orange,
colorScheme: ColorScheme.fromSwatch(accentColor: Colors.green),
),
home: NextScreen(),
);
You can use this in theme data
theme: ThemeData(
outlinedButtonTheme: OutlinedButtonThemeData(
style: ButtonStyle(
padding: MaterialStateProperty.all<EdgeInsets>(
const EdgeInsets.all(24),
),
backgroundColor: MaterialStateProperty.all<Color>(Colors.purple),
foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
),
),
textButtonTheme: TextButtonThemeData(
style: ButtonStyle(
foregroundColor:
MaterialStateProperty.all<Color>(Colors.purple)
)
)
),
I ended up finding the answer to my own question. Turns out the theming is part of the colour scheme property, and I ended up defining the following:
colorScheme: ColorScheme.fromSwatch().copyWith(
primary: Colors.deepPurpleAccent
)
This set them all to deepPurpleAccent!
Related
I've got this code snippet below trying to apply a title theme to my appBar and I am not quite sure I am understanding the copyWith function for the theme. My understanding would be that all properties of my theme are copied over, except those that I change. I am not changing appBarTheme when I use the copyWith, so why is it not applying to the app?
I've uncommented the line that does't work, and commented out the one that does.
class MyApp extends StatelessWidget {
final ThemeData theme = ThemeData(
primarySwatch: Colors.purple,
fontFamily: 'Quicksand',
// This doesn't work
appBarTheme: AppBarTheme(
titleTextStyle: TextStyle(fontFamily: 'OpenSans', fontSize: 40)),
);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Personal Expenses',
theme: theme.copyWith(
colorScheme: theme.colorScheme.copyWith(secondary: Colors.amber),
// This does work
// appBarTheme: AppBarTheme(
// titleTextStyle: TextStyle(fontFamily: 'OpenSans', fontSize: 40)),
),
home: MyHomePage(),
);
}
}
I am not changing appBarTheme when I use the copyWith, so why is it
not applying to the app?
What do you mean? If you want to simply apply your theme then all you have to do is this.
Essentially,
MaterialApp(
...
theme: theme
...
);
If you want to change the color of the AppBar, then you would want to do this.
Essentially,
MaterialApp(
...
theme: theme.copyWith(
colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.amber),
),
...
);
This would allow you to change your AppBar color while leaving your AppBar's titleTextStyle as it is.
Please note that your code theme.colorScheme.copyWith(secondary: Colors.amber) will only set the secondary color, which will not be visible depending on how you setup your AppBar. I am not sure if you did this intentionally.
Finally, if you want to change the appBarTheme as well, then you would want to do this.
Essentially,
MaterialApp(
...
theme: theme.copyWith(
colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.amber),
appBarTheme: const AppBarTheme(
titleTextStyle: TextStyle(fontFamily: 'OpenSans', fontSize: 10),
),
),
...
);
Note that I only changed the font size from 40 to 10.
All these should work, and it should show very big differences in the UI. In other words, your code seems fine. I am not very sure why you think that it is "not applying to the app". Please clarify why you think so.
Your code looks fine, I tested and it's working fine
theme: theme.copyWith(
appBarTheme: AppBarTheme(color: Colors.redAccent,titleTextStyle: TextStyle(fontFamily: "Amiri", fontSize: 40))
change the App bar theme but for
theme.copyWith(
colorScheme: theme.colorScheme.copyWith(secondary: Colors.amber),
it change secondary color not App bar theme
Here is my code:
static final darkTheme = ThemeData(
textTheme: GoogleFonts.rubikTextTheme()
.apply(bodyColor: Color(0xFFf2f2f2), displayColor: Color(0xFFf2f2f2))
.copyWith(bodyText1: TextStyle(color: Colors.greenAccent)),
scaffoldBackgroundColor: Color(0xFF1f1f1f),
primaryColor: Color(0xFF474747),
colorScheme: ColorScheme.dark(
primaryContainer: Color(0xFF282828),
secondaryContainer: Color(0xFF3D3D3D)),
iconTheme: IconThemeData(color: Color(0xFFf2f2f2)),
primaryIconTheme: IconThemeData(color: Color(0xFFf2f2f2)),
hintColor: Color(0xFFf2f2f2),
backgroundColor: Color(0xFFd9d9d9),
dividerColor: Color(0xFFf2f2f2));
I want set multiple color in textTheme, but how do i know which TextStyle property should i use? in this case i have text said "open" and i want it to be greenAccent in dark mode and green in light mode, and here i use bodyText1, but the green color apply on other text instead on the "Open" text.
should i try every property from displayLarge until overline to find property colored which text. And im not sure if i use .copyWith the right way or not. even if i manage to find it using this method, i think it is wrong. i use the .apply to color most text in white
check it
MaterialApp(
title: appName,
theme: ThemeData(
// Define the default brightness and colors.
brightness: Brightness.dark,
primaryColor: Colors.lightBlue[800],
// Define the default font family.
fontFamily: 'Georgia',
// Define the default `TextTheme`. Use this to specify the default
// text styling for headlines, titles, bodies of text, and more.
textTheme: const TextTheme(
headline1: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold),
headline6: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic),
bodyText2: TextStyle(fontSize: 14.0, fontFamily: 'Hind'),
),
),
home: const MyHomePage(
title: appName,
),
);
Instead of using Text widget use RichText widget.It can make your life really easy.Just look up RichText.
I want that the FloatingActionButtons, TextButtons and IconButtons will be visually consistent, thus look the same in means of size, shape and color, using a theme in Flutter. how can I achieve that?
For defining a theme, MaterialApp has a property named theme. There you have to provide the instance of ThemeData.
MaterialApp(
title: title,
theme: ThemeData(
brightness: Brightness.light,
)
);
For "FloatingActionButton", "IconButton" and "TextButton" to look like same you have to define the theme (all the properties which you want to be consistent) for all of them such as.
For IconButton
iconTheme: IconThemeData(
color: Colors.red,
),
For FloatingActionButton
floatingActionButtonTheme: FloatingActionButtonThemeData(
foregroundColor: Colors.red,
),
For TextButton
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
primary: Colors.red,
),
),
My Suggestion:
Use the primarySwatch property for the consistent color scheme.
primarySwatch: Colors.red;
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,
),
}
}
I tried changing the color of all my RaisedButtons from the themeData but it refused to work. All other properties, such as fontSize and fontWeight changed successfully. The color of the text only changes from black to white when the brightness property of themeData is changed to Brightness.dark.
Is there a way I can solve this issue? What could I be doing wrong?
Here is my sample code:
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primaryColor: Color(0XFF212845),
scaffoldBackgroundColor: Color(0XFF212845),
primarySwatch: Colors.yellow,
buttonColor: Color(0XFFF8D320),
textTheme: TextTheme(
button: TextStyle(
color: Colors.green, // This is not working.
fontSize: 30.0,
fontWeight: FontWeight.bold
)
)
),
home:MenuPage(),
);
For other people coming to this question, one reason that a button may not change colors is that it is disabled, which happens when you don't have the onPressed method set.
RaisedButton(
color: Theme.of(context).accentColor,
onPressed: () {}, // <-- need to add this
child: Text(...),
),
if you are giving a color to the color property and it doesn't show ,then probably you haven't implemented the onPressed property, because in this state the button will show it's disabled color , which is no color at all.
set it like this:
onPressed: () {},
giving it an anonymous function like that while not implementing anythig ( or something if you wish) will give it color
Add buttonTheme and accentColor to your ThemeData , like this:
ThemeData(
primaryColor: Color(0XFF212845),
scaffoldBackgroundColor: Color(0XFF212845),
primarySwatch: Colors.yellow,
buttonColor: Color(0XFFF8D320),
buttonTheme: ButtonThemeData(textTheme: ButtonTextTheme.accent),
accentColor: Colors.green,
Even though primarySwatch might have been added, you still need to add buttonColor to add the color to the buttons, like this:
child: MaterialApp(
home: Wrapper(),
theme: ThemeData(
primarySwatch: Colors.blue,
buttonColor: Colors.blue // this is needed
),
),
primarySwatch - used to configure default values for several fields,
including: primaryColor, primaryColorBrightness, primaryColorLight,
primaryColorDark, toggleableActiveColor, accentColor, colorScheme,
secondaryHeaderColor, textSelectionColor, backgroundColor, and
buttonColor.
Also, make sure onPressed in your RaisedButton is set:
onPressed: () {},
Make sure you haven't customize the RaisedButton() itself, else it will override the ThemeData. If you have customized color property in RaisedButton it will override the properties set in ThemeData.
I believe the correct way is to declare a buttonColor property in your ThemeData Widget.
MaterialApp(
theme: ThemeData(
fontFamily: 'Pirata',
primaryColor: Color.fromRGBO(71, 86, 87, 1),
accentColor: Color.fromRGBO(71, 86, 87, 1),
buttonColor: Color.fromRGBO(238, 238, 238, 1),
),
home: App()))