-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec7e98d
commit 4b3744b
Showing
5 changed files
with
191 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/mix/lib/src/core/widget_state/press_event_mix_state.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
enum PressEvent { | ||
idle, | ||
onTapUp, | ||
onTapDown; | ||
} | ||
|
||
class PressEventMixWidgetState extends InheritedWidget { | ||
const PressEventMixWidgetState(this.event, {super.key, required super.child}); | ||
|
||
static PressEventMixWidgetState? of(BuildContext context) { | ||
return context.dependOnInheritedWidgetOfExactType(); | ||
} | ||
|
||
final PressEvent event; | ||
|
||
@override | ||
bool updateShouldNotify(PressEventMixWidgetState oldWidget) { | ||
return event != oldWidget.event; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/mix/test/src/core/mix_state/press_states_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mix/mix.dart'; | ||
import 'package:mix/src/core/widget_state/internal/gesture_mix_state.dart'; | ||
Check warning on line 4 in packages/mix/test/src/core/mix_state/press_states_test.dart
|
||
|
||
import '../../../helpers/context_finder.dart'; | ||
import '../../../helpers/testing_utils.dart'; | ||
|
||
extension _WidgetTesterX on WidgetTester { | ||
PressEventMixWidgetState findPressEventWidget() { | ||
return findWidgetOfType<PressEventMixWidgetState>(); | ||
} | ||
} | ||
|
||
void main() { | ||
testWidgets( | ||
'Pressable should transition through (onTapDown and onTapUp) states correctly', | ||
(tester) async { | ||
int counter = 0; | ||
await tester.pumpMaterialApp( | ||
PressableBox( | ||
onPress: () { | ||
counter++; | ||
}, | ||
child: Text('$counter'), | ||
), | ||
); | ||
|
||
// Initial state should be idle | ||
expect(tester.findPressEventWidget().event, PressEvent.idle); | ||
|
||
// Press down on the PressableBox | ||
final gesture = await tester.press(find.byType(PressableBox)); | ||
await tester.pumpAndSettle(); | ||
expect(tester.findPressEventWidget().event, PressEvent.onTapDown); | ||
|
||
// Release the press | ||
await gesture.up(); | ||
await tester.pumpAndSettle(); | ||
expect(tester.findPressEventWidget().event, PressEvent.onTapUp); | ||
|
||
expect(counter, equals(1)); | ||
|
||
await tester.pump(const Duration(milliseconds: 300)); | ||
expect(tester.findPressEventWidget().event, PressEvent.idle); | ||
}); | ||
} |