flutter_flutter/dev/tools/gen_defaults/lib/button_template.dart
Camille Simon 7fd228f31d
[Material] Change default mouse cursor of buttons to basic arrow instead of click (except on web) (#171796)
Changes default mouse cursor of Material buttons to basic arrow instead
of click as per updated Android guidance.

For each Material button, I did the following:

1. Changed the default mouse cursor to the basic arrow.
2. Added a way to configure the mouse cursor of the button if a method
did not exist before.
3. Added a test to ensure that the default mouse cursor is now the basic
arrow (or modified an existing one if applicable).
4. Added a test to ensure that the customization of button mouse cursors
still works (if currently untested).

The list of Material buttons I modified (supposed to be all of them):

- RawMaterialButton
- DropdownButton
- FloatingActionButton
- ToggleButtons
- ElevatedButton
- IconButton
- FilledButton
- OutlinedButton
- PopupMenuItem
- InkWell
- TextButton
- MaterialButton
- DropdownMenuItem
- DropdownButtonFormField
- BackButton
- CloseButton
- DrawerButton
- EndDrawerButton
- PopupMenuButton
- MenuItemButton
- CheckboxMenuButton
- RadioMenuButton
- MenuBar
- SubmenuButton
- SegmentedButton
- FilterChip
- ChoiceChip
- ActionChip
- InputChip

Fixes https://github.com/flutter/flutter/issues/170296.

## 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].
- [x] 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].
- [ ] 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].

<!-- 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

---------

Co-authored-by: Tong Mu <dkwingsmt@users.noreply.github.com>
2025-10-23 22:24:00 +00:00

192 lines
6.0 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 'template.dart';
class ButtonTemplate extends TokenTemplate {
const ButtonTemplate(
this.tokenGroup,
super.blockName,
super.fileName,
super.tokens, {
super.colorSchemePrefix = '_colors.',
});
final String tokenGroup;
String _backgroundColor() {
if (tokenAvailable('$tokenGroup.container.color')) {
return '''
WidgetStateProperty.resolveWith((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return ${componentColor('$tokenGroup.disabled.container')};
}
return ${componentColor('$tokenGroup.container')};
})''';
}
return '''
const MaterialStatePropertyAll<Color>(Colors.transparent)''';
}
String _elevation() {
if (tokenAvailable('$tokenGroup.container.elevation')) {
return '''
WidgetStateProperty.resolveWith((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return ${elevation("$tokenGroup.disabled.container")};
}
if (states.contains(WidgetState.pressed)) {
return ${elevation("$tokenGroup.pressed.container")};
}
if (states.contains(WidgetState.hovered)) {
return ${elevation("$tokenGroup.hover.container")};
}
if (states.contains(WidgetState.focused)) {
return ${elevation("$tokenGroup.focus.container")};
}
return ${elevation("$tokenGroup.container")};
})''';
}
return '''
const MaterialStatePropertyAll<double>(0.0)''';
}
String _elevationColor(String token) {
if (tokenAvailable(token)) {
return 'MaterialStatePropertyAll<Color>(${color(token)})';
} else {
return 'const MaterialStatePropertyAll<Color>(Colors.transparent)';
}
}
@override
String generate() =>
'''
class _${blockName}DefaultsM3 extends ButtonStyle {
_${blockName}DefaultsM3(this.context)
: super(
animationDuration: kThemeChangeDuration,
enableFeedback: true,
alignment: Alignment.center,
);
final BuildContext context;
late final ColorScheme _colors = Theme.of(context).colorScheme;
@override
WidgetStateProperty<TextStyle?> get textStyle =>
MaterialStatePropertyAll<TextStyle?>(${textStyle("$tokenGroup.label-text")});
@override
WidgetStateProperty<Color?>? get backgroundColor =>${_backgroundColor()};
@override
WidgetStateProperty<Color?>? get foregroundColor =>
WidgetStateProperty.resolveWith((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return ${componentColor('$tokenGroup.disabled.label-text')};
}
return ${componentColor('$tokenGroup.label-text')};
});
@override
WidgetStateProperty<Color?>? get overlayColor =>
WidgetStateProperty.resolveWith((Set<WidgetState> states) {
if (states.contains(WidgetState.pressed)) {
return ${componentColor('$tokenGroup.pressed.state-layer')};
}
if (states.contains(WidgetState.hovered)) {
return ${componentColor('$tokenGroup.hover.state-layer')};
}
if (states.contains(WidgetState.focused)) {
return ${componentColor('$tokenGroup.focus.state-layer')};
}
return null;
});
@override
WidgetStateProperty<Color>? get shadowColor =>
${_elevationColor("$tokenGroup.container.shadow-color")};
@override
WidgetStateProperty<Color>? get surfaceTintColor =>
${_elevationColor("$tokenGroup.container.surface-tint-layer.color")};
@override
WidgetStateProperty<double>? get elevation =>${_elevation()};
@override
WidgetStateProperty<EdgeInsetsGeometry>? get padding =>
MaterialStatePropertyAll<EdgeInsetsGeometry>(_scaledPadding(context));
@override
WidgetStateProperty<Size>? get minimumSize =>
const MaterialStatePropertyAll<Size>(Size(64.0, ${getToken("$tokenGroup.container.height")}));
// No default fixedSize
@override
WidgetStateProperty<double>? get iconSize =>
const MaterialStatePropertyAll<double>(${getToken("$tokenGroup.with-icon.icon.size")});
@override
WidgetStateProperty<Color>? get iconColor {
return WidgetStateProperty.resolveWith((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return ${color('$tokenGroup.with-icon.disabled.icon.color')}.withOpacity(${opacity("$tokenGroup.with-icon.disabled.icon.opacity")});
}
if (states.contains(WidgetState.pressed)) {
return ${color('$tokenGroup.with-icon.pressed.icon.color')};
}
if (states.contains(WidgetState.hovered)) {
return ${color('$tokenGroup.with-icon.hover.icon.color')};
}
if (states.contains(WidgetState.focused)) {
return ${color('$tokenGroup.with-icon.focus.icon.color')};
}
return ${color('$tokenGroup.with-icon.icon.color')};
});
}
@override
WidgetStateProperty<Size>? get maximumSize =>
const MaterialStatePropertyAll<Size>(Size.infinite);
${tokenAvailable("$tokenGroup.outline.color") ? '''
@override
WidgetStateProperty<BorderSide>? get side =>
WidgetStateProperty.resolveWith((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return ${border("$tokenGroup.disabled.outline")};
}
if (states.contains(WidgetState.focused)) {
return ${border('$tokenGroup.focus.outline')};
}
return ${border("$tokenGroup.outline")};
});''' : '''
// No default side'''}
@override
WidgetStateProperty<OutlinedBorder>? get shape =>
const MaterialStatePropertyAll<OutlinedBorder>(${shape("$tokenGroup.container", '')});
@override
WidgetStateProperty<MouseCursor?>? get mouseCursor => WidgetStateMouseCursor.adaptiveClickable;
@override
VisualDensity? get visualDensity => Theme.of(context).visualDensity;
@override
MaterialTapTargetSize? get tapTargetSize => Theme.of(context).materialTapTargetSize;
@override
InteractiveInkFeatureFactory? get splashFactory => Theme.of(context).splashFactory;
}
''';
}