Skip to content

Commit 2a649b1

Browse files
authored
Add an adaptive visual density static function, and add it to… (flutter#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.
1 parent b2cf417 commit 2a649b1

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

packages/flutter/lib/src/material/theme_data.dart

+18
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,24 @@ class VisualDensity with Diagnosticable {
17621762
/// It corresponds to a density value of -2 in both axes.
17631763
static const VisualDensity compact = VisualDensity(horizontal: -2.0, vertical: -2.0);
17641764

1765+
/// Returns a visual density that is adaptive based on the [defaultTargetPlatform].
1766+
///
1767+
/// For desktop platforms, this returns [compact], and for other platforms,
1768+
/// it returns a default-constructed [VisualDensity].
1769+
static VisualDensity get adaptivePlatformDensity {
1770+
switch (defaultTargetPlatform) {
1771+
case TargetPlatform.android:
1772+
case TargetPlatform.iOS:
1773+
case TargetPlatform.fuchsia:
1774+
break;
1775+
case TargetPlatform.linux:
1776+
case TargetPlatform.macOS:
1777+
case TargetPlatform.windows:
1778+
return compact;
1779+
}
1780+
return const VisualDensity();
1781+
}
1782+
17651783
/// Copy the current [VisualDensity] with the given values replacing the
17661784
/// current values.
17671785
VisualDensity copyWith({

packages/flutter/test/material/theme_data_test.dart

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/foundation.dart';
56
import 'package:flutter/material.dart';
67
import 'package:flutter_test/flutter_test.dart';
78

@@ -178,6 +179,20 @@ void main() {
178179
expect(theme.applyElevationOverlayColor, isTrue);
179180
});
180181

182+
testWidgets('VisualDensity.adaptivePlatformDensity returns adaptive values', (WidgetTester tester) async {
183+
switch (debugDefaultTargetPlatformOverride) {
184+
case TargetPlatform.android:
185+
case TargetPlatform.iOS:
186+
case TargetPlatform.fuchsia:
187+
expect(VisualDensity.adaptivePlatformDensity, equals(const VisualDensity()));
188+
break;
189+
case TargetPlatform.linux:
190+
case TargetPlatform.macOS:
191+
case TargetPlatform.windows:
192+
expect(VisualDensity.adaptivePlatformDensity, equals(VisualDensity.compact));
193+
}
194+
}, variant: TargetPlatformVariant.all());
195+
181196
testWidgets('ThemeData.copyWith correctly creates new ThemeData with all copied arguments', (WidgetTester tester) async {
182197

183198
final SliderThemeData sliderTheme = SliderThemeData.fromPrimaryColors(

packages/flutter_tools/templates/app/lib/main.dart.tmpl

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class MyApp extends StatelessWidget {
3636
// Notice that the counter didn't reset back to zero; the application
3737
// is not restarted.
3838
primarySwatch: Colors.blue,
39+
// This makes the visual density adapt to the platform that you run
40+
// the app on. For desktop platforms, the controls will be smaller and
41+
// closer together (more dense) than on mobile platforms.
42+
visualDensity: VisualDensity.adaptivePlatformDensity,
3943
),
4044
home: MyHomePage(title: 'Flutter Demo Home Page'),
4145
);

0 commit comments

Comments
 (0)