Add an adaptive visual density static function, and add it to… (#51921)
Adds a VisualDensity.adaptivePlatformDensity static function that returns different values for visual density based on the defaultTargetPlatform. Returns compact for desktop platforms, and a default visual density for other platforms.
diff --git a/packages/flutter/lib/src/material/theme_data.dart b/packages/flutter/lib/src/material/theme_data.dart
index fc736ed..3d98277 100644
--- a/packages/flutter/lib/src/material/theme_data.dart
+++ b/packages/flutter/lib/src/material/theme_data.dart
@@ -1762,6 +1762,24 @@
/// 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].
+ ///
+ /// For desktop platforms, this returns [compact], and for other platforms,
+ /// it returns a default-constructed [VisualDensity].
+ static VisualDensity get adaptivePlatformDensity {
+ switch (defaultTargetPlatform) {
+ case TargetPlatform.android:
+ case TargetPlatform.iOS:
+ case TargetPlatform.fuchsia:
+ break;
+ case TargetPlatform.linux:
+ case TargetPlatform.macOS:
+ case TargetPlatform.windows:
+ return compact;
+ }
+ return const VisualDensity();
+ }
+
/// Copy the current [VisualDensity] with the given values replacing the
/// current values.
VisualDensity copyWith({
diff --git a/packages/flutter/test/material/theme_data_test.dart b/packages/flutter/test/material/theme_data_test.dart
index a96ef89..794da7e 100644
--- a/packages/flutter/test/material/theme_data_test.dart
+++ b/packages/flutter/test/material/theme_data_test.dart
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
@@ -178,6 +179,20 @@
expect(theme.applyElevationOverlayColor, isTrue);
});
+ testWidgets('VisualDensity.adaptivePlatformDensity returns adaptive values', (WidgetTester tester) async {
+ switch (debugDefaultTargetPlatformOverride) {
+ case TargetPlatform.android:
+ case TargetPlatform.iOS:
+ case TargetPlatform.fuchsia:
+ expect(VisualDensity.adaptivePlatformDensity, equals(const VisualDensity()));
+ break;
+ case TargetPlatform.linux:
+ case TargetPlatform.macOS:
+ case TargetPlatform.windows:
+ expect(VisualDensity.adaptivePlatformDensity, equals(VisualDensity.compact));
+ }
+ }, variant: TargetPlatformVariant.all());
+
testWidgets('ThemeData.copyWith correctly creates new ThemeData with all copied arguments', (WidgetTester tester) async {
final SliderThemeData sliderTheme = SliderThemeData.fromPrimaryColors(
diff --git a/packages/flutter_tools/templates/app/lib/main.dart.tmpl b/packages/flutter_tools/templates/app/lib/main.dart.tmpl
index 5a82f64..80691b7 100644
--- a/packages/flutter_tools/templates/app/lib/main.dart.tmpl
+++ b/packages/flutter_tools/templates/app/lib/main.dart.tmpl
@@ -36,6 +36,10 @@
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
+ // This makes the visual density adapt to the platform that you run
+ // the app on. For desktop platforms, the controls will be smaller and
+ // closer together (more dense) than on mobile platforms.
+ visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);