mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Follow up of https://github.com/flutter/flutter/pull/174486 Migrate some files from `MaterialState` to `WidgetState`. This PR only focus on `WidgetState` for a subset of files. - This minimizes conflicts and reduces the size of the PR for easier reviews and follow up - I'll work on the other elements of `packages/flutter/lib/src/material/material_state.dart` into other PRs ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
253 lines
11 KiB
Dart
253 lines
11 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/rendering.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
void main() {
|
|
test('ButtonStyle copyWith, merge, ==, hashCode basics', () {
|
|
expect(const ButtonStyle(), const ButtonStyle().copyWith());
|
|
expect(const ButtonStyle().merge(const ButtonStyle()), const ButtonStyle());
|
|
expect(const ButtonStyle().hashCode, const ButtonStyle().copyWith().hashCode);
|
|
});
|
|
|
|
test('ButtonStyle lerp special cases', () {
|
|
expect(ButtonStyle.lerp(null, null, 0), null);
|
|
const ButtonStyle data = ButtonStyle();
|
|
expect(identical(ButtonStyle.lerp(data, data, 0.5), data), true);
|
|
});
|
|
|
|
test('ButtonStyle defaults', () {
|
|
const ButtonStyle style = ButtonStyle();
|
|
expect(style.textStyle, isNull);
|
|
expect(style.backgroundColor, isNull);
|
|
expect(style.foregroundColor, isNull);
|
|
expect(style.overlayColor, isNull);
|
|
expect(style.shadowColor, isNull);
|
|
expect(style.surfaceTintColor, isNull);
|
|
expect(style.elevation, isNull);
|
|
expect(style.padding, isNull);
|
|
expect(style.minimumSize, isNull);
|
|
expect(style.fixedSize, isNull);
|
|
expect(style.maximumSize, isNull);
|
|
expect(style.iconColor, isNull);
|
|
expect(style.iconSize, isNull);
|
|
expect(style.side, isNull);
|
|
expect(style.shape, isNull);
|
|
expect(style.mouseCursor, isNull);
|
|
expect(style.visualDensity, isNull);
|
|
expect(style.tapTargetSize, isNull);
|
|
expect(style.animationDuration, isNull);
|
|
expect(style.enableFeedback, isNull);
|
|
expect(style.alignment, isNull);
|
|
expect(style.splashFactory, isNull);
|
|
expect(style.backgroundBuilder, isNull);
|
|
expect(style.foregroundBuilder, isNull);
|
|
});
|
|
|
|
testWidgets('Default ButtonStyle debugFillProperties', (WidgetTester tester) async {
|
|
final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
|
|
const ButtonStyle().debugFillProperties(builder);
|
|
|
|
final List<String> description = builder.properties
|
|
.where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
|
|
.map((DiagnosticsNode node) => node.toString())
|
|
.toList();
|
|
|
|
expect(description, <String>[]);
|
|
});
|
|
|
|
testWidgets('ButtonStyle debugFillProperties', (WidgetTester tester) async {
|
|
final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
|
|
const ButtonStyle(
|
|
textStyle: MaterialStatePropertyAll<TextStyle>(TextStyle(fontSize: 10.0)),
|
|
backgroundColor: MaterialStatePropertyAll<Color>(Color(0xfffffff1)),
|
|
foregroundColor: MaterialStatePropertyAll<Color>(Color(0xfffffff2)),
|
|
overlayColor: MaterialStatePropertyAll<Color>(Color(0xfffffff3)),
|
|
shadowColor: MaterialStatePropertyAll<Color>(Color(0xfffffff4)),
|
|
surfaceTintColor: MaterialStatePropertyAll<Color>(Color(0xfffffff5)),
|
|
elevation: MaterialStatePropertyAll<double>(1.5),
|
|
padding: MaterialStatePropertyAll<EdgeInsets>(EdgeInsets.all(1.0)),
|
|
minimumSize: MaterialStatePropertyAll<Size>(Size(1.0, 2.0)),
|
|
side: MaterialStatePropertyAll<BorderSide>(BorderSide(width: 4.0, color: Color(0xfffffff6))),
|
|
maximumSize: MaterialStatePropertyAll<Size>(Size(100.0, 200.0)),
|
|
iconColor: MaterialStatePropertyAll<Color>(Color(0xfffffff6)),
|
|
iconSize: MaterialStatePropertyAll<double>(48.1),
|
|
shape: MaterialStatePropertyAll<OutlinedBorder>(StadiumBorder()),
|
|
mouseCursor: MaterialStatePropertyAll<MouseCursor>(SystemMouseCursors.forbidden),
|
|
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
|
animationDuration: Duration(seconds: 1),
|
|
enableFeedback: true,
|
|
).debugFillProperties(builder);
|
|
|
|
final List<String> description = builder.properties
|
|
.where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
|
|
.map((DiagnosticsNode node) => node.toString())
|
|
.toList();
|
|
|
|
expect(description, <String>[
|
|
'textStyle: WidgetStatePropertyAll(TextStyle(inherit: true, size: 10.0))',
|
|
'backgroundColor: WidgetStatePropertyAll(${const Color(0xfffffff1)})',
|
|
'foregroundColor: WidgetStatePropertyAll(${const Color(0xfffffff2)})',
|
|
'overlayColor: WidgetStatePropertyAll(${const Color(0xfffffff3)})',
|
|
'shadowColor: WidgetStatePropertyAll(${const Color(0xfffffff4)})',
|
|
'surfaceTintColor: WidgetStatePropertyAll(${const Color(0xfffffff5)})',
|
|
'elevation: WidgetStatePropertyAll(1.5)',
|
|
'padding: WidgetStatePropertyAll(EdgeInsets.all(1.0))',
|
|
'minimumSize: WidgetStatePropertyAll(Size(1.0, 2.0))',
|
|
'maximumSize: WidgetStatePropertyAll(Size(100.0, 200.0))',
|
|
'iconColor: WidgetStatePropertyAll(${const Color(0xfffffff6)})',
|
|
'iconSize: WidgetStatePropertyAll(48.1)',
|
|
'side: WidgetStatePropertyAll(BorderSide(color: ${const Color(0xfffffff6)}, width: 4.0))',
|
|
'shape: WidgetStatePropertyAll(StadiumBorder(BorderSide(width: 0.0, style: none)))',
|
|
'mouseCursor: WidgetStatePropertyAll(SystemMouseCursor(forbidden))',
|
|
'tapTargetSize: shrinkWrap',
|
|
'animationDuration: 0:00:01.000000',
|
|
'enableFeedback: true',
|
|
]);
|
|
});
|
|
|
|
testWidgets('ButtonStyle copyWith, merge', (WidgetTester tester) async {
|
|
const WidgetStateProperty<TextStyle> textStyle = MaterialStatePropertyAll<TextStyle>(
|
|
TextStyle(fontSize: 10),
|
|
);
|
|
const WidgetStateProperty<Color> backgroundColor = MaterialStatePropertyAll<Color>(
|
|
Color(0xfffffff1),
|
|
);
|
|
const WidgetStateProperty<Color> foregroundColor = MaterialStatePropertyAll<Color>(
|
|
Color(0xfffffff2),
|
|
);
|
|
const WidgetStateProperty<Color> overlayColor = MaterialStatePropertyAll<Color>(
|
|
Color(0xfffffff3),
|
|
);
|
|
const WidgetStateProperty<Color> shadowColor = MaterialStatePropertyAll<Color>(
|
|
Color(0xfffffff4),
|
|
);
|
|
const WidgetStateProperty<Color> surfaceTintColor = MaterialStatePropertyAll<Color>(
|
|
Color(0xfffffff5),
|
|
);
|
|
const WidgetStateProperty<double> elevation = MaterialStatePropertyAll<double>(1);
|
|
const WidgetStateProperty<EdgeInsets> padding = MaterialStatePropertyAll<EdgeInsets>(
|
|
EdgeInsets.all(1),
|
|
);
|
|
const WidgetStateProperty<Size> minimumSize = MaterialStatePropertyAll<Size>(Size(1, 2));
|
|
const WidgetStateProperty<Size> fixedSize = MaterialStatePropertyAll<Size>(Size(3, 4));
|
|
const WidgetStateProperty<Size> maximumSize = MaterialStatePropertyAll<Size>(Size(5, 6));
|
|
const WidgetStateProperty<Color> iconColor = MaterialStatePropertyAll<Color>(Color(0xfffffff6));
|
|
const WidgetStateProperty<double> iconSize = MaterialStatePropertyAll<double>(48.0);
|
|
const WidgetStateProperty<BorderSide> side = MaterialStatePropertyAll<BorderSide>(BorderSide());
|
|
const WidgetStateProperty<OutlinedBorder> shape = MaterialStatePropertyAll<OutlinedBorder>(
|
|
StadiumBorder(),
|
|
);
|
|
const WidgetStateProperty<MouseCursor> mouseCursor = MaterialStatePropertyAll<MouseCursor>(
|
|
SystemMouseCursors.forbidden,
|
|
);
|
|
const VisualDensity visualDensity = VisualDensity.compact;
|
|
const MaterialTapTargetSize tapTargetSize = MaterialTapTargetSize.shrinkWrap;
|
|
const Duration animationDuration = Duration(seconds: 1);
|
|
const bool enableFeedback = true;
|
|
|
|
const ButtonStyle style = ButtonStyle(
|
|
textStyle: textStyle,
|
|
backgroundColor: backgroundColor,
|
|
foregroundColor: foregroundColor,
|
|
overlayColor: overlayColor,
|
|
shadowColor: shadowColor,
|
|
surfaceTintColor: surfaceTintColor,
|
|
elevation: elevation,
|
|
padding: padding,
|
|
minimumSize: minimumSize,
|
|
fixedSize: fixedSize,
|
|
maximumSize: maximumSize,
|
|
iconColor: iconColor,
|
|
iconSize: iconSize,
|
|
side: side,
|
|
shape: shape,
|
|
mouseCursor: mouseCursor,
|
|
visualDensity: visualDensity,
|
|
tapTargetSize: tapTargetSize,
|
|
animationDuration: animationDuration,
|
|
enableFeedback: enableFeedback,
|
|
);
|
|
|
|
expect(
|
|
style,
|
|
const ButtonStyle().copyWith(
|
|
textStyle: textStyle,
|
|
backgroundColor: backgroundColor,
|
|
foregroundColor: foregroundColor,
|
|
overlayColor: overlayColor,
|
|
shadowColor: shadowColor,
|
|
surfaceTintColor: surfaceTintColor,
|
|
elevation: elevation,
|
|
padding: padding,
|
|
minimumSize: minimumSize,
|
|
fixedSize: fixedSize,
|
|
maximumSize: maximumSize,
|
|
iconColor: iconColor,
|
|
iconSize: iconSize,
|
|
side: side,
|
|
shape: shape,
|
|
mouseCursor: mouseCursor,
|
|
visualDensity: visualDensity,
|
|
tapTargetSize: tapTargetSize,
|
|
animationDuration: animationDuration,
|
|
enableFeedback: enableFeedback,
|
|
),
|
|
);
|
|
|
|
expect(style, const ButtonStyle().merge(style));
|
|
|
|
expect(style.copyWith(), style.merge(const ButtonStyle()));
|
|
});
|
|
|
|
test('ButtonStyle.lerp BorderSide', () {
|
|
// This is regression test for https://github.com/flutter/flutter/pull/78051
|
|
expect(ButtonStyle.lerp(null, null, 0), null);
|
|
expect(ButtonStyle.lerp(null, null, 0.5), null);
|
|
expect(ButtonStyle.lerp(null, null, 1), null);
|
|
|
|
const BorderSide blackSide = BorderSide();
|
|
const BorderSide whiteSide = BorderSide(color: Color(0xFFFFFFFF));
|
|
const BorderSide emptyBlackSide = BorderSide(width: 0, color: Color(0x00000000));
|
|
|
|
const ButtonStyle blackStyle = ButtonStyle(
|
|
side: MaterialStatePropertyAll<BorderSide>(blackSide),
|
|
);
|
|
const ButtonStyle whiteStyle = ButtonStyle(
|
|
side: MaterialStatePropertyAll<BorderSide>(whiteSide),
|
|
);
|
|
|
|
// WidgetState.all<Foo>(value) properties resolve to value
|
|
// for any set of MaterialStates.
|
|
const Set<WidgetState> states = <WidgetState>{};
|
|
|
|
expect(ButtonStyle.lerp(blackStyle, blackStyle, 0)?.side?.resolve(states), blackSide);
|
|
expect(ButtonStyle.lerp(blackStyle, blackStyle, 0.5)?.side?.resolve(states), blackSide);
|
|
expect(ButtonStyle.lerp(blackStyle, blackStyle, 1)?.side?.resolve(states), blackSide);
|
|
|
|
expect(ButtonStyle.lerp(blackStyle, null, 0)?.side?.resolve(states), blackSide);
|
|
expect(
|
|
ButtonStyle.lerp(blackStyle, null, 0.5)?.side?.resolve(states),
|
|
BorderSide.lerp(blackSide, emptyBlackSide, 0.5),
|
|
);
|
|
expect(ButtonStyle.lerp(blackStyle, null, 1)?.side?.resolve(states), emptyBlackSide);
|
|
|
|
expect(ButtonStyle.lerp(null, blackStyle, 0)?.side?.resolve(states), emptyBlackSide);
|
|
expect(
|
|
ButtonStyle.lerp(null, blackStyle, 0.5)?.side?.resolve(states),
|
|
BorderSide.lerp(emptyBlackSide, blackSide, 0.5),
|
|
);
|
|
expect(ButtonStyle.lerp(null, blackStyle, 1)?.side?.resolve(states), blackSide);
|
|
|
|
expect(ButtonStyle.lerp(blackStyle, whiteStyle, 0)?.side?.resolve(states), blackSide);
|
|
expect(
|
|
ButtonStyle.lerp(blackStyle, whiteStyle, 0.5)?.side?.resolve(states),
|
|
BorderSide.lerp(blackSide, whiteSide, 0.5),
|
|
);
|
|
expect(ButtonStyle.lerp(blackStyle, whiteStyle, 1)?.side?.resolve(states), whiteSide);
|
|
});
|
|
}
|