Flutter / Dart, widgets, and local ownership / Lesson 7
Theme inheritance and readable colors
Share a Material theme through the widget tree and verify readable presentation across supported settings.
On this page
Lesson goals
- Provide a theme at the application boundary.
- Read a matching color pair from context.
- Test readability and local overrides.
Provide application styling
This fragment creates a color scheme from a seed and installs it on MaterialApp. The host must include the WorkshopNotice class shown next and a normal Flutter entry point. Theme selection is a presentation decision; do not use color alone to convey an error or selection. This illustrative fragment is not claimed as a tested device build.
import "package:flutter/material.dart";
class WorkshopApp extends StatelessWidget {
const WorkshopApp({super.key});
@override
Widget build(BuildContext context) => MaterialApp(
theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo)),
home: const Scaffold(body: Center(child: WorkshopNotice())),
);
}
The host applies the seeded theme to the workshop screen.
The application theme is available to descendant widgets through their build context.
Read matching color roles
The notice reads the nearest theme and pairs primary background with onPrimary text. Place this class in the same library as the application fragment or import it explicitly. The pairing expresses the intended roles, but a real product still needs contrast and readability checks in its actual themes, text scaling and target environment.
import "package:flutter/material.dart";
class WorkshopNotice extends StatelessWidget {
const WorkshopNotice({super.key});
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
return ColoredBox(color: scheme.primary, child: Padding(padding: const EdgeInsets.all(12), child: Text("Workshop", style: TextStyle(color: scheme.onPrimary))));
}
}
The notice displays Workshop using the current theme.
Theme.of(context) retrieves the current scheme and the widget uses the paired foreground role.
Understand local scope
A nested Theme can replace or extend the surrounding theme for a subtree. Read it using a context below that provider; a context above it sees the outer theme. Document why a local override exists and check both light and dark configurations when supported. Avoid copying a fixed color that becomes unreadable after the surrounding theme changes.
Verify readable presentation
Render the same notice under each supported theme and larger text scaling, then inspect contrast, clipping and semantic meaning. Test a nested override and confirm that widgets outside it still use the intended outer theme. Verify the complete task on the target platform; a color scheme or a single screenshot does not by itself certify accessibility.
Common errors and fixes
A widget uses the outer theme instead of an override.
- Why it happens
- Its context is above the nested Theme.
- How to fix it
- Read the theme from a descendant context.
- Prevent it next time
- Test the override in its actual widget tree.
Text becomes hard to read in another theme.
- Why it happens
- A fixed foreground color does not match the background.
- How to fix it
- Use an appropriate paired color role and verify contrast.
- Prevent it next time
- Review supported brightness and text settings.
An error is communicated only by color.
- Why it happens
- Presentation has no text or semantic equivalent.
- How to fix it
- Add a meaningful message and appropriate semantics.
- Prevent it next time
- Test the task without relying on color.
2 practice exercises
1. Which theme does Theme.of(context) return?
Show worked solution
It resolves the nearest applicable Theme above that context. Place the context below a local override if the child should use it.
2. Why use onPrimary with a primary background?
Show worked solution
The roles express a foreground-background pairing in the color scheme. Verify actual contrast and readability for the supported themes and text sizes.
Key takeaways
- Themes are inherited through the widget tree.
- Use semantic color roles and meaningful text.
- Verify readability in the actual target configuration.