text
stringlengths
1
372
Text('Hello'),
Text('World'),
],
);
});
<code_end>
this widget can now be composed within a side panel,
dialog, or even a full-screen view,
and adapt its layout to whatever space is provided.
<topic_end>
<topic_start>
device segmentation
there are times when you want to make layout decisions
based on the actual platform you’re running on,
regardless of size. for example, when building a
custom title bar, you might need to check the operating
system type and tweak the layout of your title bar,
so it doesn’t get covered by the native window buttons.
to determine which combination of platforms you’re on,
you can use the platform API along with the kIsWeb value:
<code_start>
bool get isMobileDevice => !kisweb && (platform.isios || Platform.isAndroid);
bool get isDesktopDevice =>
!kisweb && (platform.ismacos || Platform.isWindows || Platform.isLinux);
bool get isMobileDeviceOrWeb => kIsWeb || isMobileDevice;
bool get isDesktopDeviceOrWeb => kIsWeb || isDesktopDevice;
<code_end>
the platform API can’t be accessed from web builds without
throwing an exception, because the dart.io package isn’t
supported on the web target. as a result, the above code checks
for web first, and because of short-circuiting,
dart never calls platform on web targets.
use Platform/kIsWeb when the logic absolutely must
run for a given platform. for example,
talking to a plugin that only works on iOS,
or displaying a widget that only conforms to
play store policy and not the app store’s.
<topic_end>
<topic_start>
single source of truth for styling
you’ll probably find it easier to maintain your views
if you create a single source of truth for styling values
like padding, spacing, corner shape, font sizes, and so on.
this can be done easily with some helper classes:
<code_start>
class insets {
static const double xsmall = 3;
static const double small = 4;
static const double medium = 5;
static const double large = 10;
static const double extraLarge = 20;
// etc
}
class fonts {
static const string raleway = 'raleway';
// etc
}
class TextStyles {
static const TextStyle raleway = TextStyle(
fontFamily: fonts.raleway,
);
static TextStyle buttonText1 =
const TextStyle(fontWeight: FontWeight.bold, fontSize: 14);
static TextStyle buttonText2 =
const TextStyle(fontWeight: FontWeight.normal, fontSize: 11);
static TextStyle h1 =
const TextStyle(fontWeight: FontWeight.bold, fontSize: 22);
static TextStyle h2 =
const TextStyle(fontWeight: FontWeight.bold, fontSize: 16);
static TextStyle body1 = raleway.copyWith(color: const Color(0xFF42A5F5));
// etc
}
<code_end>
these constants can then be used in place of hard-coded numeric values:
<code_start>
return padding(
padding: const EdgeInsets.all(Insets.small),
child: Text('Hello!', style: TextStyles.body1),
);
<code_end>
use theme.of(context).platform for theming and
design choices, like what kind of switches to show
and general Cupertino/Material adaptions.
with all views referencing the same shared-design system rules,
they tend to look better and more consistent.
making a change or adjusting a value for a specific platform
can be done in a single place, instead of using an error-prone
search and replace. using shared rules has the added benefit
of helping enforce consistency on the design side.
some common design system categories that can be represented
this way are:
like most rules, there are exceptions:
one-off values that are used nowhere else in the app.
there is little point in cluttering up the styling rules
with these values, but it’s worth considering if they
should be derived from an existing value (for example,
padding + 1.0). you should also watch for reuse or duplication
of the same semantic values. those values should likely be
added to the global styling ruleset.
<topic_end>