Initialize ThemeData.visualDensity using ThemeData.platform instead of defaultTargetPlatform (#124357)

Initialize `ThemeData.visualDensity` using `ThemeData.platform` instead of `defaultTargetPlatform`
This commit is contained in:
Greg Spencer
2023-04-06 18:44:23 -07:00
committed by GitHub
parent 77d06f107a
commit 692a7efa3f
2 changed files with 53 additions and 6 deletions

View File

@@ -448,7 +448,7 @@ class ThemeData with Diagnosticable {
}
pageTransitionsTheme ??= const PageTransitionsTheme();
scrollbarTheme ??= const ScrollbarThemeData();
visualDensity ??= VisualDensity.adaptivePlatformDensity;
visualDensity ??= VisualDensity.defaultDensityForPlatform(platform);
useMaterial3 ??= false;
final bool useInkSparkle = platform == TargetPlatform.android && !kIsWeb;
splashFactory ??= useMaterial3
@@ -2646,12 +2646,32 @@ class VisualDensity with Diagnosticable {
/// It corresponds to a density value of -2 in both axes.
static const VisualDensity compact = VisualDensity(horizontal: -2.0, vertical: -2.0);
/// Returns a visual density that is adaptive based on the [defaultTargetPlatform].
/// Returns a [VisualDensity] that is adaptive based on the current platform
/// on which the framework is executing, from [defaultTargetPlatform].
///
/// For desktop platforms, this returns [compact], and for other platforms,
/// it returns a default-constructed [VisualDensity].
static VisualDensity get adaptivePlatformDensity {
switch (defaultTargetPlatform) {
/// When [defaultTargetPlatform] is a desktop platform, this returns
/// [compact], and for other platforms, it returns a default-constructed
/// [VisualDensity].
///
/// See also:
///
/// * [defaultDensityForPlatform] which returns a [VisualDensity] that is
/// adaptive based on the platform given to it.
/// * [defaultTargetPlatform] which returns the platform on which the
/// framework is currently executing.
static VisualDensity get adaptivePlatformDensity => defaultDensityForPlatform(defaultTargetPlatform);
/// Returns a [VisualDensity] that is adaptive based on the given [platform].
///
/// For desktop platforms, this returns [compact], and for other platforms, it
/// returns a default-constructed [VisualDensity].
///
/// See also:
///
/// * [adaptivePlatformDensity] which returns a [VisualDensity] that is
/// adaptive based on [defaultTargetPlatform].
static VisualDensity defaultDensityForPlatform(TargetPlatform platform) {
switch (platform) {
case TargetPlatform.android:
case TargetPlatform.iOS:
case TargetPlatform.fuchsia:

View File

@@ -468,6 +468,19 @@ void main() {
}
}, variant: TargetPlatformVariant.all());
testWidgets('VisualDensity.getDensityForPlatform returns adaptive values', (WidgetTester tester) async {
switch (debugDefaultTargetPlatformOverride!) {
case TargetPlatform.android:
case TargetPlatform.iOS:
case TargetPlatform.fuchsia:
expect(VisualDensity.defaultDensityForPlatform(debugDefaultTargetPlatformOverride!), equals(VisualDensity.standard));
case TargetPlatform.linux:
case TargetPlatform.macOS:
case TargetPlatform.windows:
expect(VisualDensity.defaultDensityForPlatform(debugDefaultTargetPlatformOverride!), equals(VisualDensity.compact));
}
}, variant: TargetPlatformVariant.all());
testWidgets('VisualDensity in ThemeData defaults to "compact" on desktop and "standard" on mobile', (WidgetTester tester) async {
final ThemeData themeData = ThemeData();
switch (debugDefaultTargetPlatformOverride!) {
@@ -482,6 +495,20 @@ void main() {
}
}, variant: TargetPlatformVariant.all());
testWidgets('VisualDensity in ThemeData defaults to the right thing when a platform is supplied to it', (WidgetTester tester) async {
final ThemeData themeData = ThemeData(platform: debugDefaultTargetPlatformOverride! == TargetPlatform.android ? TargetPlatform.linux : TargetPlatform.android);
switch (debugDefaultTargetPlatformOverride!) {
case TargetPlatform.iOS:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.macOS:
case TargetPlatform.windows:
expect(themeData.visualDensity, equals(VisualDensity.standard));
case TargetPlatform.android:
expect(themeData.visualDensity, equals(VisualDensity.compact));
}
}, variant: TargetPlatformVariant.all());
testWidgets('Ensure Visual Density effective constraints are clamped', (WidgetTester tester) async {
const BoxConstraints square = BoxConstraints.tightFor(width: 35, height: 35);
BoxConstraints expanded = const VisualDensity(horizontal: 4.0, vertical: 4.0).effectiveConstraints(square);