Can you change flutter text theme? - flutter

If the theme is set in main.dart as
return MaterialApp(
title: 'MY APP',
theme: ThemeData(
primarySwatch: Colors.blue,
fontFamily: 'Cabin',
textTheme: TextTheme(
headline1: TextStyle(
fontFamily: 'Raleway',
color: Colors.black,
fontWeight: FontWeight.w600,
fontSize: 18,
),
subtitle1: TextStyle(
fontFamily: 'Raleway',
color: Colors.black54,
fontWeight: FontWeight.w600,
fontSize: 16,
),
),
),
and I'm using the theme as
Text('MY STRING',
style: Theme.of(context).textTheme.subtitle1),
How can I make 'MY STRING' be a different color then the subtitle1 theme color while keeping the other properties of theme data, such as font weight and family and size, etc.?

You can use de method copyWith(color: your_color) to change properties of a TextTheme.
Example:
Text('MY STRING',
style: Theme.of(context).textTheme.subtitle1
.copyWith(color: Colors.red),
)
Doc Reference: https://api.flutter.dev/flutter/material/TextTheme/copyWith.html

Related

The named parameter 'title' isn't defined

I get this error for the 'title' on the second line:
The named parameter 'title' isn't defined.
Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'title'.
textTheme: ThemeData.light().textTheme.copyWith(
title: TextStyle(
fontFamily: 'OpenSans',
fontSize: 20,
fontWeight: FontWeight.bold,
),
You need to use titleLarge, titleMedium and titleSmall in place of title, use .copyWith() constructor.
return MaterialApp(
title: 'MyApp',
theme: Theme.of(context).copyWith(
textTheme: ThemeData.light().textTheme.copyWith(
titleSmall: Theme.of(context).textTheme.titleLarge?.copyWith(
fontFamily: 'OpenSans',
fontSize: 20,
fontWeight: FontWeight.bold,
),
titleLarge: Theme.of(context).textTheme.titleLarge?.copyWith(...),
titleMedium: Theme.of(context).textTheme.titleLarge?.copyWith(...),
TextTheme does not include title /As you see in the below image:/
TextTheme properties
You can set theme like this
textTheme: TextTheme(
headline1: TextStyle(fontSize: 24, color: lightColors.PRIMARY_TEXT_COLOR, fontWeight: FontWeight.w600),
headline2: TextStyle(fontSize: 22, color: lightColors.PRIMARY_TEXT_COLOR, fontWeight: FontWeight.w600),
headline3: TextStyle(fontSize: 20, color: lightColors.PRIMARY_TEXT_COLOR, fontWeight: FontWeight.w600),
headline4: TextStyle(fontSize: 17, color: lightColors.PRIMARY_TEXT_COLOR, fontWeight: FontWeight.w600),
headline5: TextStyle(fontSize: 16, color: lightColors.PRIMARY_TEXT_COLOR, fontWeight: FontWeight.w600),
headline6: TextStyle(fontSize: 15, color: lightColors.PRIMARY_TEXT_COLOR, fontWeight: FontWeight.w600),
subtitle1: TextStyle(fontSize: 14, color: lightColors.PRIMARY_TEXT_COLOR, fontWeight: FontWeight.w500),
subtitle2: TextStyle(fontSize: 12, color: lightColors.PRIMARY_TEXT_COLOR, fontWeight: FontWeight.w500),
bodyText1: TextStyle(fontSize: 14, color: lightColors.PRIMARY_TEXT_COLOR, fontWeight: FontWeight.normal),
bodyText2: TextStyle(fontSize: 13, color: lightColors.SECONDARY_TEXT_COLOR, fontWeight: FontWeight.normal),
button: TextStyle(fontSize: 15, color: lightColors.GRAY_COLOR_100)
),
And use it in your App
Text("Text", style: Theme.of(context).textTheme.subtitle1!.copyWith())
With the latest version of Flutter, some theme identifiers changed.
display4 => headline1;
display3 => headline2;
display2 => headline3;
display1 => headline4;
headline => headline5;
title => headline6; // so use headline6 instead of title
subhead => subtitle1;
subtitle => subtitle2;
body2 => bodyText1;
body => bodyText2;
So you can define your Theme like this:
textTheme: ThemeData.light().textTheme.copyWith(
headline6: TextStyle(
fontFamily: 'OpenSans',
fontSize: 20,
fontWeight: FontWeight.bold,
),
and then use it e.g. like this:
Text(
'my text',
style: Theme.of(context).textTheme.headline6,
),
The "title" is now deprecated, use "label" property instead
Documentation:
https://docs.flutter.dev/release/breaking-changes/bottom-navigation-title-to-label

TextStyle doesn't identify accentColor and returns the wrong color

In MaterialApp in theme, I am setting heeadline6's color with already set accentColor. But when I try to display text with headline6 textTheme, it gives me the wrong color, although every other parameter is correct.
theme: ThemeData(
accentColor: const Color(0xffAEB8C4),
textTheme: TextTheme(
headline6: TextStyle(
color: Theme.of(context).accentColor,
fontFamily: 'Lato',
fontStyle: FontStyle.normal,
fontSize: 50,
fontWeight: FontWeight.w900,
),
),
),
This is the color
And here is the code, Where I use headline6:
child: Text(
"RLabs",
style: Theme.of(context).textTheme.headline6,
),
And yet somehow I get the text with another color.
I suggest storing the constant accent color in a separate palette class.
Example -
Palette{
static final accentColor = Color(0xffAEB8C4);
}
Then
theme: ThemeData(
accentColor: Palette.accentColor,
textTheme: TextTheme(
headline6: TextStyle(
color: Palette.accentColor,
fontFamily: 'Lato',
fontStyle: FontStyle.normal,
fontSize: 50,
fontWeight: FontWeight.w900,
),
),
),
Please look at this answer to get clarification on why Theme.of(context) doesn't work in your specific case
You can give direct color to TextStyle.
theme: ThemeData(
accentColor: const Color(0xffAEB8C4),
textTheme: TextTheme(
headline6: TextStyle(
color: const Color(0xffAEB8C4),
fontFamily: 'Lato',
fontStyle: FontStyle.normal,
fontSize: 50,
fontWeight: FontWeight.w900,
),
),
),

Appbar color, toolbar color, text, font and buttons are not changing by applying Theme Data in flutter App

#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Personal Expenses',
theme: ThemeData(
brightness: Brightness.light,
primaryColor: Colors.lightGreen[800],
primarySwatch: Colors.amber,
errorColor: Colors.red,
accentColor: Colors.cyan[600],
fontFamily: 'Quicksand',
textTheme: ThemeData.light().textTheme.copyWith(
title: TextStyle(
fontFamily: 'Quicksand',
backgroundColor: Colors.grey,
fontSize: 18,
),
),
appBarTheme: AppBarTheme(
textTheme: ThemeData.light().textTheme.copyWith(
title: TextStyle(
fontFamily: 'OpenSans',
fontSize: 20,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic),
button: TextStyle(color: Colors.white),
),
)
),
home: MyHomePage(),
);
}
For example, to apply theme on Button I am doing this but theme does not work on it
FlatButton(
color: Colors.white,
textColor: Theme.of(context).textTheme.button.color,
onPressed: _presentDatePicker,
child: Text(
'Choose Date',
style: TextStyle(fontWeight: FontWeight.bold),
),
)
You set button text color under AppBarTheme not in textTheme which you refer in your FlatButton.
This may fix your problem.
FlatButton(
color: Colors.white,
textColor: Theme.of(context).AppBarTheme.textTheme.button.color,
onPressed: _presentDatePicker,
child: Text(
'Choose Date',
style: TextStyle(fontWeight: FontWeight.bold),
),
)
Or you can set buttonColor like this
theme: ThemeData(
primarySwatch: Colors.purple,
accentColor: Colors.amber,
buttonColor: Colors.white,
),
And use that buttonColor in your FlatButton like this
FlatButton(
color: Colors.white,
textColor: Theme.of(context).buttonColor,
onPressed: _presentDatePicker,
child: Text(
'Choose Date',
style: TextStyle(fontWeight: FontWeight.bold),
),
)

How do I use the headlines and bodyText defined in my Material theme?

I have a MaterialTheme setup like the below
MaterialApp(
theme: ThemeData(
scaffoldBackgroundColor: Color(ColorPalettes.BG_COLOR),
primaryColor: Color(ColorPalettes.PRIMARY_BLUE),
accentColor: Color(ColorPalettes.SECONDARY_ORANGE),
textTheme: TextTheme(
headline1: TextStyle(color: Color(ColorPalettes.HEADING_COLOR), fontSize: 24.0),
headline2: TextStyle(color: Color(ColorPalettes.HEADING_COLOR), fontSize: 22.0),
headline3: TextStyle(color: Color(ColorPalettes.HEADING_COLOR), fontSize: 20.0),
bodyText1: TextStyle(color: Color(ColorPalettes.BODY_COLOR)),
bodyText2: TextStyle(color: Color(ColorPalettes.BODY_COLOR)),
button: TextStyle(color: Colors.white),
)
),
But I am not clear as how to use the headlines and the body when I style text?
You can access using theme data as following.
Text(
'Text with a background color',
style: Theme.of(context).textTheme.headline1,
),

Flutter: How to set line height property of text to ThemeData

How can I set the line-height globally inside ThemeData?
theme: ThemeData(
brightness: Brightness.light,
accentColor: myAccentColor,
primaryColor: myPrimaryColor,
fontFamily: 'Ubuntu',
buttonTheme: ThemeData.light().buttonTheme.copyWith(
buttonColor: myPrimaryColor,
textTheme: ButtonTextTheme.primary,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5)),
),
scaffoldBackgroundColor: myBackgroundWhite,
cardColor: myBackgroundWhite,
textSelectionColor: myGreyTextColor,
cursorColor: myAccentColor,
cupertinoOverrideTheme: CupertinoThemeData(
primaryColor: myAccentColor,
),
errorColor: myErrorColorRed,
)
We cant set the line-height (property name: height) directly from ThemeData.
The best practice for handling text style is by using material type system guidelines. See the section "Material Text Scale: Modernizing Flutter Text Theming" from here.
So then on global ThemeData, you can use
theme: ThemeData(
brightness: Brightness.light,
accentColor: flujoAccentColor,
primaryColor: flujoPrimaryColor,
fontFamily: 'FiraSans',
textTheme: TextTheme(
headline5: TextStyle(
fontSize: 24.0,
fontWeight: FontWeight.w700,
fontStyle: FontStyle.normal,
),
bodyText1: TextStyle(
fontSize: 16.0,
fontStyle: FontStyle.normal,
letterSpacing: 0.5,
),
bodyText2: TextStyle(
fontSize: 14.0,
fontStyle: FontStyle.normal,
letterSpacing: 0.25,
height: 1.5,
),
subtitle1: TextStyle(
fontSize: 16.0,
fontStyle: FontStyle.normal,
letterSpacing: 0.15,
),
subtitle2: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
fontStyle: FontStyle.normal,
),
caption: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w400,
fontStyle: FontStyle.normal,
),
overline: TextStyle(
fontSize: 10,
fontWeight: FontWeight.w400,
letterSpacing: 0.25,
height: 1.5,
fontStyle: FontStyle.normal,
)),
Like this and on a Text widget, apply it like:
Text(
"Hello world!",
style: Theme.of(context).textTheme.bodyText1.copyWith(color: Colors.teal,),
);