So I started working with Flutter again and back in the day there was no way to actually globally style your controls, there is a sort of messy way I guess you could say where you create const
styles something like below:
static ButtonStyle elevatedButtonStyle = ElevatedButton.styleFrom(
textStyle: TextStyle(fontSize: 20),
backgroundColor: Constants.jaffaOrange,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10))));
And then manually apply it wherever you need it.
I was wondering if there is a way to apply this to all controls without having to actually manually assign it to each and every one of them kind of like how Xamarin has Global styling which kind of implicitly applies the styles to every single control of a particular type.
If not inbuilt is there a dependency I could import maybe?
If this is not the case what are other ways to actually maintain styles in flutter?
I don’t need an end-to-end solution for each and every control obviously as long as you can even guide me in the right direction it would immensely help!
3
Answers
You could do this by setting a button theme in the app theme right in your material app. The button theme defines the default configuration of button widgets.
Register the theme inside the material app and define the buttonTheme and pass the properties here. Should take effect on all buttons without having to style those buttons "inline".
In Flutter, theming depends on MaterialApp’s
theme:
which takes ThemeData, and it pass down to the widget tree. ForelevatedButtonTheme
. it will beFind more about theme in flutter.
This is the full example code you can try it here:
Example to try
It looks like this