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.
This commit is contained in:
Greg Spencer
2020-03-31 16:16:25 -07:00
committed by GitHub
parent b2cf417863
commit 2a649b16e2
3 changed files with 37 additions and 0 deletions

View File

@@ -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({

View File

@@ -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(

View File

@@ -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'),
);