Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4b902c7

Browse files
authored
CupertinoButton: Add clickable cursor on web (#96863)
1 parent d61caaa commit 4b902c7

File tree

2 files changed

+69
-37
lines changed

2 files changed

+69
-37
lines changed

packages/flutter/lib/src/cupertino/button.dart

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -245,43 +245,46 @@ class _CupertinoButtonState extends State<CupertinoButton> with SingleTickerProv
245245

246246
final TextStyle textStyle = themeData.textTheme.textStyle.copyWith(color: foregroundColor);
247247

248-
return GestureDetector(
249-
behavior: HitTestBehavior.opaque,
250-
onTapDown: enabled ? _handleTapDown : null,
251-
onTapUp: enabled ? _handleTapUp : null,
252-
onTapCancel: enabled ? _handleTapCancel : null,
253-
onTap: widget.onPressed,
254-
child: Semantics(
255-
button: true,
256-
child: ConstrainedBox(
257-
constraints: widget.minSize == null
258-
? const BoxConstraints()
259-
: BoxConstraints(
260-
minWidth: widget.minSize!,
261-
minHeight: widget.minSize!,
262-
),
263-
child: FadeTransition(
264-
opacity: _opacityAnimation,
265-
child: DecoratedBox(
266-
decoration: BoxDecoration(
267-
borderRadius: widget.borderRadius,
268-
color: backgroundColor != null && !enabled
269-
? CupertinoDynamicColor.resolve(widget.disabledColor, context)
270-
: backgroundColor,
271-
),
272-
child: Padding(
273-
padding: widget.padding ?? (backgroundColor != null
274-
? _kBackgroundButtonPadding
275-
: _kButtonPadding),
276-
child: Align(
277-
alignment: widget.alignment,
278-
widthFactor: 1.0,
279-
heightFactor: 1.0,
280-
child: DefaultTextStyle(
281-
style: textStyle,
282-
child: IconTheme(
283-
data: IconThemeData(color: foregroundColor),
284-
child: widget.child,
248+
return MouseRegion(
249+
cursor: enabled && kIsWeb ? SystemMouseCursors.click : MouseCursor.defer,
250+
child: GestureDetector(
251+
behavior: HitTestBehavior.opaque,
252+
onTapDown: enabled ? _handleTapDown : null,
253+
onTapUp: enabled ? _handleTapUp : null,
254+
onTapCancel: enabled ? _handleTapCancel : null,
255+
onTap: widget.onPressed,
256+
child: Semantics(
257+
button: true,
258+
child: ConstrainedBox(
259+
constraints: widget.minSize == null
260+
? const BoxConstraints()
261+
: BoxConstraints(
262+
minWidth: widget.minSize!,
263+
minHeight: widget.minSize!,
264+
),
265+
child: FadeTransition(
266+
opacity: _opacityAnimation,
267+
child: DecoratedBox(
268+
decoration: BoxDecoration(
269+
borderRadius: widget.borderRadius,
270+
color: backgroundColor != null && !enabled
271+
? CupertinoDynamicColor.resolve(widget.disabledColor, context)
272+
: backgroundColor,
273+
),
274+
child: Padding(
275+
padding: widget.padding ?? (backgroundColor != null
276+
? _kBackgroundButtonPadding
277+
: _kButtonPadding),
278+
child: Align(
279+
alignment: widget.alignment,
280+
widthFactor: 1.0,
281+
heightFactor: 1.0,
282+
child: DefaultTextStyle(
283+
style: textStyle,
284+
child: IconTheme(
285+
data: IconThemeData(color: foregroundColor),
286+
child: widget.child,
287+
),
285288
),
286289
),
287290
),

packages/flutter/test/cupertino/button_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// found in the LICENSE file.
44

55
import 'package:flutter/cupertino.dart';
6+
import 'package:flutter/foundation.dart';
7+
import 'package:flutter/gestures.dart';
68
import 'package:flutter/rendering.dart';
79
import 'package:flutter/scheduler.dart';
810
import 'package:flutter_test/flutter_test.dart';
@@ -451,6 +453,33 @@ void main() {
451453
).decoration as BoxDecoration;
452454
expect(decoration.color, isSameColorAs(CupertinoColors.systemBlue.darkColor));
453455
});
456+
457+
testWidgets('Hovering over Cupertino button updates cursor to clickable on Web', (WidgetTester tester) async {
458+
await tester.pumpWidget(
459+
CupertinoApp(
460+
home: Center(
461+
child: CupertinoButton.filled(
462+
onPressed: () { },
463+
child: const Text('Tap me'),
464+
),
465+
),
466+
),
467+
);
468+
469+
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
470+
await gesture.addPointer(location: const Offset(10, 10));
471+
await tester.pumpAndSettle();
472+
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);
473+
474+
final Offset button = tester.getCenter(find.byType(CupertinoButton));
475+
await gesture.moveTo(button);
476+
addTearDown(gesture.removePointer);
477+
await tester.pumpAndSettle();
478+
expect(
479+
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
480+
kIsWeb ? SystemMouseCursors.click : SystemMouseCursors.basic,
481+
);
482+
});
454483
}
455484

456485
Widget boilerplate({ required Widget child }) {

0 commit comments

Comments
 (0)