简体   繁体   中英

Can you change flutter text theme?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM