Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions lib/widgets/button.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

import 'color.dart';
Expand Down Expand Up @@ -159,7 +160,7 @@ class ZulipWebUiKitButton extends StatelessWidget {

final labelColor = _labelColor(designVariables);

return AnimatedScaleOnTap(
return AnimatedScaleOnPrimaryPointerDown(
scaleEnd: 0.96,
duration: Duration(milliseconds: 100),
child: TextButton.icon(
Expand Down Expand Up @@ -270,10 +271,10 @@ class ZulipIconButton extends StatelessWidget {
}
}

/// Apply [Transform.scale] to the child widget when tapped, and reset its scale
/// when released, while animating the transitions.
class AnimatedScaleOnTap extends StatefulWidget {
const AnimatedScaleOnTap({
/// Apply [Transform.scale] to the child widget on primary pointer-down,
/// and reset its scale on -up or -cancel, with animated transitions.
class AnimatedScaleOnPrimaryPointerDown extends StatefulWidget {
const AnimatedScaleOnPrimaryPointerDown({
super.key,
required this.scaleEnd,
required this.duration,
Expand All @@ -289,11 +290,12 @@ class AnimatedScaleOnTap extends StatefulWidget {
final Widget child;

@override
State<AnimatedScaleOnTap> createState() => _AnimatedScaleOnTapState();
State<AnimatedScaleOnPrimaryPointerDown> createState() => _AnimatedScaleOnPrimaryPointerDownState();
}

class _AnimatedScaleOnTapState extends State<AnimatedScaleOnTap> {
class _AnimatedScaleOnPrimaryPointerDownState extends State<AnimatedScaleOnPrimaryPointerDown> {
double _scale = 1;
int _pressedButton = 0;

void _changeScale(double scale) {
setState(() {
Expand All @@ -303,11 +305,20 @@ class _AnimatedScaleOnTapState extends State<AnimatedScaleOnTap> {

@override
Widget build(BuildContext context) {
return GestureDetector(
return Listener(
behavior: HitTestBehavior.translucent,
onTapDown: (_) => _changeScale(widget.scaleEnd),
onTapUp: (_) => _changeScale(1),
onTapCancel: () => _changeScale(1),
onPointerDown: (PointerDownEvent pointerDownEvent) {
_pressedButton = pointerDownEvent.buttons;
if(_pressedButton & kPrimaryButton != 0) _changeScale(widget.scaleEnd);
},
onPointerUp: (_) {
if(_pressedButton & kPrimaryButton != 0) _changeScale(1);
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _pressedButton state should be reset to 0 in onPointerUp as well, not just in onPointerCancel. Without this, if the user presses the primary button and releases it, then presses a secondary button, the widget may incorrectly maintain the previous button state. Add _pressedButton = 0; after the scale change.

Suggested change
if(_pressedButton & kPrimaryButton != 0) _changeScale(1);
if(_pressedButton & kPrimaryButton != 0) _changeScale(1);
_pressedButton = 0;

Copilot uses AI. Check for mistakes.
_pressedButton = 0;
},
onPointerCancel: (_) {
if(_pressedButton & kPrimaryButton != 0) _changeScale(1);
_pressedButton = 0;
},
child: AnimatedScale(
scale: _scale,
duration: widget.duration,
Expand Down
6 changes: 3 additions & 3 deletions lib/widgets/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ class _NavigationBarButton extends StatelessWidget {
final designVariables = DesignVariables.of(context);
final color = selected ? designVariables.iconSelected : designVariables.icon;

Widget result = AnimatedScaleOnTap(
Widget result = AnimatedScaleOnPrimaryPointerDown(
scaleEnd: 0.875,
duration: const Duration(milliseconds: 100),
child: Material(
Expand Down Expand Up @@ -389,7 +389,7 @@ void _showMainMenu(BuildContext context, {
child: Column(children: menuItems)))),
const Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: AnimatedScaleOnTap(
child: AnimatedScaleOnPrimaryPointerDown(
scaleEnd: 0.95,
duration: Duration(milliseconds: 100),
child: BottomSheetDismissButton(
Expand Down Expand Up @@ -459,7 +459,7 @@ abstract class _MenuButton extends StatelessWidget {
~WidgetState.pressed: selected ? borderSideSelected : null,
}));

return AnimatedScaleOnTap(
return AnimatedScaleOnPrimaryPointerDown(
duration: const Duration(milliseconds: 100),
scaleEnd: 0.95,
child: ConstrainedBox(
Expand Down
33 changes: 33 additions & 0 deletions test/widgets/button_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,39 @@ void main() {
check(renderObject).size.equals(Size.square(40));
});

group('AnimatedScaleOnPrimaryPointerDown', () {
void checkScale(WidgetTester tester, Finder finder, double expectedScale) {
final scale = tester.widget<AnimatedScale>(finder).scale;
check(scale).equals(expectedScale);
}

testWidgets('Animation happen instantly when tap down', (tester) async {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I run this test without your change in lib/widgets/button.dart, it still passes. That means it's not doing its job.

addTearDown(testBinding.reset);

await tester.pumpWidget(TestZulipApp(
child: AnimatedScaleOnPrimaryPointerDown(
scaleEnd: 0.95,
duration: Duration(milliseconds: 100),
child: UnconstrainedBox(
child: ZulipIconButton(
icon: ZulipIcons.follow,
onPressed: () {})))));
await tester.pump();

final animatedScaleFinder = find.byType(AnimatedScale);

// Now that the widget is being held down, its scale should be at the target scaleEnd i.e 0.95.
final gesture = await tester.startGesture(tester.getCenter(find.byType(ZulipIconButton)));
await tester.pumpAndSettle(const Duration(milliseconds: 50));
checkScale(tester, animatedScaleFinder, 0.95);

// After releasing, the scale must return to 1.0.
await gesture.up();
await tester.pumpAndSettle();
checkScale(tester, animatedScaleFinder, 1.0);
});
});

// TODO test that the touch feedback fills the whole square
});
}