Unverified Commit 2a649b16 authored by Greg Spencer's avatar Greg Spencer Committed by GitHub

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.
parent b2cf4178
......@@ -1762,6 +1762,24 @@ 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].
///
/// 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({
......
......@@ -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 @@ void main() {
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(
......
......@@ -36,6 +36,10 @@ class MyApp extends StatelessWidget {
// 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'),
);
......
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