Best practice to propagate theme through flutter screens - flutter

I've made a basic theme for my app and set it in the MaterialApp widget
ThemeData(
primaryColor: Colors.black,
backgroundColor: Colors.grey.shade50,
scaffoldBackgroundColor: Colors.grey.shade50,
buttonColor: Colors.purple,
);
but it seems the theme is not being updated on MaterialAppshome
home: LoginScreen()
. the button nor the background (supposed to be black and gray) aint updating...
What is the correct way of doing this?

As per the docs:
Using a Theme
Now that you’ve defined a theme, use it within the widgets’ build() methods by using the Theme.of(context) method.
The Theme.of(context) method looks up the widget tree and returns the nearest Theme in the tree. If you have a standalone Theme defined above your widget, that’s returned. If not, the app’s theme is returned.
In fact, the FloatingActionButton uses this technique to find the accentColor.
Container(
color: Theme.of(context).accentColor,
child: Text(
'Text with a background color',
style: Theme.of(context).textTheme.headline6,
),
),

Related

How to make `FloatingActionButton`s, `TextButton`s and `IconButton`s look the same in means of size, shape and color, using a theme in Flutter?

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;

Primary custom color not working in flutter

I have a problem here I want to change color of textbutton from themedata but its not working. Here is my code :
darkTheme: ThemeData(
primaryColor:Colors.white,
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(primary: Colors.white),
)
),
and my button code :
TextButton(
style: TextButton.styleFrom(
primary: Theme.of(context).primaryColor,
textStyle: TextStyle(fontSize: 16),),
onPressed: (){}, child: Text("Hellosir",))
I can think of two problems why this is not working.
First, you want to access ThemeData defined in darkTheme, but your themeMode is not dark. So in MaterialApp add themeMode: ThemeMode.dark parameter as well.
Second, your button where you call Theme.of(context).primaryColor is inside same widget as your definition of Theme, and your context still doesn't have that data. So only context of children of current widget have this data. Solution would be to make a new widget with your button inside it, or wrap your button with Builder widget which have context inside its builder.
Your problem can be first, second or both.

When Should one use style: TextStyle() and Theme.of()

I am learning Flutter. Sometimes I see coders use
text: Text("text", style: TextStyle(color: kTextColor)
and sometimes they use
child: Text("text",
style: Theme.of(context).textTheme.bodyText1,
What is the difference between these two code snippets? And what does context mean?
in my opinion: in a big project it's good practice to define your theme first. Then, you implement it in your app. Therefore, I'd go with the second approach to define headlines and body texts and button texts.
Instead, when you want to personalize some text in your app a little you can use:
style: Theme.of(context).textTheme.headline5?.copywith(
// personalize even more, with exact the same properties of TextStyle
...
)
which copies everything in that textTheme besides the stuff you define in that block.
OF COURSE it makes sense to use TextStyle in some contexts, like when you want to quickly format some text that you know that it doesn't need refactoring at the moment you're writing it, i.e. you just want to style some text regardless of theming.
If you take a look at MainApp(), that used by main method. You can find something like this,
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
buttonTheme: ButtonThemeData(
textTheme: ButtonTextTheme.primary,
),
textTheme: Theme.of(context).textTheme.copyWith(
headline1: TextStyle(fontSize: 33, color: Colors.orange),
headline2: TextStyle(
fontSize: 12,
color: Colors.pink,
),
),
),
home: DropDOOO(),
);
}
So you can see there is a parameter call ThemeData. This will be used by default and when you will call Theme.of(). You can find textTheme here too.
Next one TextStyle(), you can override Texttheme using TextStyle() using it.
For more, visit
themes
TextTheme

How to change color of the bubble (under cursor) on EditText in Flutter

How can I change the color of the bubble that appears upon text selection in a Text or TextFormField or .. in Flutter?
Here is the same question but for native code.
According to this flutter documentation, textSelectionHandleColor is deprecated. You should use selectionHandleColor instead inside TextSelectionThemeData widget like the code below.
theme: ThemeData(
textSelectionTheme: TextSelectionThemeData(
cursorColor: Colors.red,
selectionColor: Colors.green,
selectionHandleColor: Colors.black,
),
)
You may use the textSelectionHandleColor property.
Theme(
data: Theme.of(context).copyWith(
textSelectionHandleColor: Colors.green,
),
child: TextField(),
);
In case of iOS TextField, i've found no other solution than adding the snippet below to the top level MaterialApp like this:
MaterialApp => theme: ThemeData( snippet )
snippet:
cupertinoOverrideTheme: CupertinoThemeData( primaryColor: Color(0xff554270), ),
i couldn't apply this snippet by wrapping the TextField with Theme(data: Theme.of(context).copyWith( ... did not work.. dunno why ]: (maybe this one would be my fault in somewhere but adding it to the app lv definitely worked tho)
Hope this answer help future iOS wanderers [:

How to set textStyle of a Cupertino app in flutter

I have a CupertinoApp and I would like to apply a custom TextStyle to all the screen/object of my app. For example I would lie to set a font family to all Text widget & Dialog widget and use that font across all my app. I was hoping to set it once in CupertinoThemeData or CupertinoTextThemeData but so far I did not have joy.
Note: I am able to set style for each Text however I would like to set it once for all
I've just run into this at the moment.
All I'm trying to do is colour text white, with general black backgrounds across the app (not font work).
The following has brought me some success:
return CupertinoApp(
theme: new CupertinoThemeData(
brightness: Brightness.dark,
primaryColor: CupertinoColors.dark,
barBackgroundColor: CupertinoColors.black,
scaffoldBackgroundColor: CupertinoColors.black,
textTheme: new CupertinoTextThemeData(
primaryColor: CupertinoColors.white,
brightness: Brightness.light,
textStyle: TextStyle(color: CupertinoColors.white),
// ... here I actually utilised all possible parameters in the constructor
// as you can see in the link underneath
),
),
// ...
)
Ref: CupertinoTextThemeData Constructor
I think you could extend my TextStyle(color: CupertinoColors.white) to apply fonts too. I intend to extract the TextStyle and ...ThemeData into separate classes to create a single place to edit them.
Hopefully this advances your position
Use this example of theme in your CupertinoApp .
theme: CupertinoThemeData(
textTheme: CupertinoTextThemeData(
textStyle: TextStyle(
fontSize: 14,
fontStyle: FontStyle.italic,
backgroundColor: CupertinoColors.black)),
),
Reminder : For the colors, use a CupertinoColor instead of a simple
Color.
My code is here