How to set textStyle of a Cupertino app in flutter - 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

Related

How to set multiple colors for text themes?

I try to make an app theme for the text style. I get from the flutter docs this example.
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(
displayLarge: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold),
titleLarge: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic),
bodyMedium: TextStyle(fontSize: 14.0, fontFamily: 'Hind'),
),
),
home: const MyHomePage(
title: appName,
),
);
and
Container(
color: Theme.of(context).colorScheme.secondary,
child: Text(
'Text with a background color',
style: Theme.of(context).textTheme.titleLarge,
),
),
I want to have the option to add multiple colors for the same text size. For example, the titleLarge can be red or white.
Adding color in the TextTheme->TextStyle only gives the option to add one color.
Is there a way for me to change it later in the Text widget? Maybe a way to override color from Theme.of(context).textTheme.titleLarge?
You can copy the theme, then give individual text styles like this
Theme.of(context).textTheme.apply(bodyColor: Colors.white).titleLarge
or you can also use copyWith using conditional ? like this
Theme.of(context).textTheme.titleLarge?.copyWith(color: Colors.white)
or null check ! like this
Theme.of(context).textTheme.titleLarge!.copyWith(color: Colors.white)
You can override at the usage place with copyWith as following
Theme.of(context).textTheme.titleLarge!.copyWith(color: Colors.white)

How do I set multiple color in theme data flutter?

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.

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

Best practice to propagate theme through flutter screens

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

Flutter How to use a custom word in textTheme

I know I can create a customized theme with MaterialApp, but is it possible for me to use a word of my own instead of the reserved words?
For exemple:
MaterialApp(
title: title,
theme: ThemeData(
// Define the default brightness and colors.
brightness: Brightness.dark,
primaryColor: Colors.lightBlue[800],
accentColor: Colors.cyan[600],
// 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: TextTheme(
nameReview: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold),
textReview: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic),
dateReview: TextStyle(fontSize: 14.0, fontFamily: 'Hind'),
),
)
);
for when I see in the construction of my review widget the code becomes more readable