7

As far as I understand I'm supposed to access my style constants in flutter via Theme.of(). In Android I would have stored them in dimens.xml. None of the entries in ThemeData look to me like they are about padding amounts and it also seems impossible to add new ones. Where should I store the my constants for padding amounts?

nvoigt
  • 75,013
  • 26
  • 93
  • 142
Christian
  • 25,249
  • 40
  • 134
  • 225

2 Answers2

3

Create a contants.dart file and store the default padding there

const kDefaultPadding = EdgeInsets.all(16);
zavora
  • 353
  • 2
  • 9
  • 1
    While this seems to be a possible way to do that, it seems strange to me to do that instead of Theme.of(). Is this somewhere recommended as best practice? – Christian May 23 '20 at 09:14
  • 1
    Theme.of() is all about colors and font styles. Apart from the constant example above, another way you can do this is you can create a base Widget class which defines default padding values and then all your Widgets inherit from this base Widget - but that might be an overkill. – zavora May 23 '20 at 09:21
  • 1
    Has kDefaultPadding been deprecated? I ask because I can't find it in the documentation on api.flutter.dev For example I can find kControlChannelName on https://api.flutter.dev/flutter/dart-ui/ChannelBuffers/kControlChannelName-constant.html But no constants for Padding on https://api.flutter.dev/flutter/widgets/Padding-class.html Where can I find out more about it? – Dan1ell Jan 12 '22 at 19:01
  • @Dan1ell "kDefaultPadding" is user-defined here (and the "k" prefix is just a naming convention for constants). It does nothing in isolation, but you can plug it in wherever an EdgeInsets is expected. – thejeremyjohn Feb 15 '23 at 15:47
  • With this approach, you cannot encapsulate your UI in a separate package. For example I am making two apps that use some common UI elements, but they differ in color, text fonts, paddings, etc. With `Theme.of()` we can handle colors and text fonts, but not global numerical constants like "padding". – Ashkan Sarlak Aug 24 '23 at 23:11
3

I made a util function to keep paddings etc consistent

const kDefaultSpacingFactor = 8;
// this function will simple take a value and multiply with the constant scaling
// factor, to keep paddings etc consistent
double spacingFactor(double multiplier) {
  return multiplier * kDefaultSpacingFactor;
}
ravin001
  • 651
  • 6
  • 6