Unverified Commit 692a7efa authored by Greg Spencer's avatar Greg Spencer Committed by GitHub

Initialize `ThemeData.visualDensity` using `ThemeData.platform` instead of...

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

Initialize `ThemeData.visualDensity` using `ThemeData.platform` instead of `defaultTargetPlatform`
parent 77d06f10
...@@ -448,7 +448,7 @@ class ThemeData with Diagnosticable { ...@@ -448,7 +448,7 @@ class ThemeData with Diagnosticable {
} }
pageTransitionsTheme ??= const PageTransitionsTheme(); pageTransitionsTheme ??= const PageTransitionsTheme();
scrollbarTheme ??= const ScrollbarThemeData(); scrollbarTheme ??= const ScrollbarThemeData();
visualDensity ??= VisualDensity.adaptivePlatformDensity; visualDensity ??= VisualDensity.defaultDensityForPlatform(platform);
useMaterial3 ??= false; useMaterial3 ??= false;
final bool useInkSparkle = platform == TargetPlatform.android && !kIsWeb; final bool useInkSparkle = platform == TargetPlatform.android && !kIsWeb;
splashFactory ??= useMaterial3 splashFactory ??= useMaterial3
...@@ -2646,12 +2646,32 @@ class VisualDensity with Diagnosticable { ...@@ -2646,12 +2646,32 @@ class VisualDensity with Diagnosticable {
/// It corresponds to a density value of -2 in both axes. /// It corresponds to a density value of -2 in both axes.
static const VisualDensity compact = VisualDensity(horizontal: -2.0, vertical: -2.0); 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, /// When [defaultTargetPlatform] is a desktop platform, this returns
/// it returns a default-constructed [VisualDensity]. /// [compact], and for other platforms, it returns a default-constructed
static VisualDensity get adaptivePlatformDensity { /// [VisualDensity].
switch (defaultTargetPlatform) { ///
/// 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.android:
case TargetPlatform.iOS: case TargetPlatform.iOS:
case TargetPlatform.fuchsia: case TargetPlatform.fuchsia:
......
...@@ -468,6 +468,19 @@ void main() { ...@@ -468,6 +468,19 @@ void main() {
} }
}, variant: TargetPlatformVariant.all()); }, 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 { testWidgets('VisualDensity in ThemeData defaults to "compact" on desktop and "standard" on mobile', (WidgetTester tester) async {
final ThemeData themeData = ThemeData(); final ThemeData themeData = ThemeData();
switch (debugDefaultTargetPlatformOverride!) { switch (debugDefaultTargetPlatformOverride!) {
...@@ -482,6 +495,20 @@ void main() { ...@@ -482,6 +495,20 @@ void main() {
} }
}, variant: TargetPlatformVariant.all()); }, 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 { testWidgets('Ensure Visual Density effective constraints are clamped', (WidgetTester tester) async {
const BoxConstraints square = BoxConstraints.tightFor(width: 35, height: 35); const BoxConstraints square = BoxConstraints.tightFor(width: 35, height: 35);
BoxConstraints expanded = const VisualDensity(horizontal: 4.0, vertical: 4.0).effectiveConstraints(square); BoxConstraints expanded = const VisualDensity(horizontal: 4.0, vertical: 4.0).effectiveConstraints(square);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment