I want to set a color in my Theme to the primary color.
When the primary color changes, so should the other color.
I know how to assign a hardcoded color to any color in the Theme.
But I don’t know how to dynamically reference the primary color.
Here’s what I tried:
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
theme: ThemeData(
useMaterial3: true,
textTheme: const TextTheme(titleLarge: TextStyle(color: Colors.black)),
),
home: const MyHomePage(),
));
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Text('ok', style: Theme.of(context).textTheme.titleLarge),
);
}
}
Is it possible to refer primary color instead of setting black color?
2
Answers
From the docs: https://docs.flutter.dev/cookbook/design/themes
Remember: As of the Flutter 3.16 release, Material 3 is Flutter’s default theme.
You could create your own subclass of
ThemeData
and override the getter for the appropriate color to return the primary color instead.And use it when running your app