TextTheme support
ryan-sf opened this issue · 8 comments
It would be nice to have an API where you could apply styles directly from the theme.
Example:
Text("Some text").body1()
Text("Some text").caption()
I achieve this in my own app by creating a StyledTheme widget with a static global key that wraps theme. Then wrap the application in StyledTheme as the child of MaterialApp.
class StyledTheme extends StatefulWidget {
final Widget child;
StyledTheme({Key key, this.child}) : super(key: key);
@override
_StyledThemeState createState() => _StyledThemeState();
static ThemeData get theme {
return Theme.of(_StyledThemeState.styledThemeKey.currentContext);
}
}
class _StyledThemeState extends State<StyledTheme> {
static GlobalKey<_StyledThemeState> styledThemeKey = GlobalKey<_StyledThemeState>();
@override
Widget build(BuildContext context) {
return Theme(
key: styledThemeKey,
data: theme,
child: widget.child,
);
}
}
I have not been able to find a better way to acheive this.
This isn't directly supported, however there is a method called textStyle
which kinda does what you want.
Text('text')
.textStyle(Theme.of(context).textTheme.body1)
The pattern that I went with was to also add extensions to the context. So the above solution becomes even simpler.
Text('text')
.textStyle(context.body1)
I don't really agree that it makes the code simpler. While making the code a bit smaller, there is a sacrifice in the code structure.
The thought behind this package isn't to make extensions on everything but to make the code simpler without compromising code quality
I'm interested in understanding how there's a sacrifice to code quality and structure here?
what i meant by that is it makes the code less verbose. It removes crucial information which describes where the style is coming from.
// fetches the text theme from the current context and then selects the body1 theme
Theme.of(context).textTheme.body1
// fetches body1 from the context
context.body1
Just context.body1
doesnt describe what exactly what you are doing. This may be ok for smaller single person projects but for bigger projects this will actually be harder instead of simpler. You cant tell exactly what happens just by looking at the code.
I see your point, although it feels a bit subjective since devtools allow you to easily inspect the implementation/documentation of what is a fairly trivial abstraction. In both cases, there are assumptions made about the developer's understanding of the convention - context.textTheme.body1
would convey the same meaning.
I do think it's fair to say that extending BuildContext is outside of the scope of this library since it's purpose is simplifying Widget code.
I appreciate your understanding!
Here, try it https://pub.dev/packages/textless 😉