From 2a649b16e24fae5d7815e27fdeb9adcf2b621721 Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Tue, 31 Mar 2020 16:16:25 -0700 Subject: [PATCH] =?UTF-8?q?Add=20an=20adaptive=20visual=20density=20static?= =?UTF-8?q?=20function,=20and=20add=20it=20to=E2=80=A6=20(#51921)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../flutter/lib/src/material/theme_data.dart | 18 ++++++++++++++++++ .../flutter/test/material/theme_data_test.dart | 15 +++++++++++++++ .../templates/app/lib/main.dart.tmpl | 4 ++++ 3 files changed, 37 insertions(+) diff --git a/packages/flutter/lib/src/material/theme_data.dart b/packages/flutter/lib/src/material/theme_data.dart index fc736edbc0..3d98277b6c 100644 --- a/packages/flutter/lib/src/material/theme_data.dart +++ b/packages/flutter/lib/src/material/theme_data.dart @@ -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({ diff --git a/packages/flutter/test/material/theme_data_test.dart b/packages/flutter/test/material/theme_data_test.dart index a96ef89c96..794da7eab9 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 @@ 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( diff --git a/packages/flutter_tools/templates/app/lib/main.dart.tmpl b/packages/flutter_tools/templates/app/lib/main.dart.tmpl index 5a82f64575..80691b7d03 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 @@ 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'), );