Skip to content

Commit

Permalink
61: fixes mouse cursor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
maeddin committed Aug 12, 2024
1 parent 4fcb72c commit f971edd
Show file tree
Hide file tree
Showing 7 changed files with 388 additions and 409 deletions.
1 change: 0 additions & 1 deletion example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
/build/

# Web related
lib/generated_plugin_registrant.dart

# Symbolication related
app.*.symbols
Expand Down
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
onChanged: (i) => setState(() => value = i),
iconBuilder: rollingIconBuilder,
loading: false,
).vertical(),
const Padding(
padding: EdgeInsets.all(8.0),
Expand Down
2 changes: 1 addition & 1 deletion lib/animated_toggle_switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ part 'src/widgets/animation_type_builder.dart';
part 'src/foreground_indicator_transition.dart';
part 'src/widgets/animated_toggle_switch.dart';
part 'src/widgets/custom_animated_toggle_switch.dart';
part 'src/widgets/drag_region.dart';
part 'src/widgets/hover_region.dart';
part 'src/widgets/conditional_wrapper.dart';
640 changes: 328 additions & 312 deletions lib/src/widgets/custom_animated_toggle_switch.dart

Large diffs are not rendered by default.

94 changes: 0 additions & 94 deletions lib/src/widgets/drag_region.dart

This file was deleted.

57 changes: 57 additions & 0 deletions lib/src/widgets/hover_region.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
part of 'package:animated_toggle_switch/animated_toggle_switch.dart';

class _HoverRegion extends StatefulWidget {
final Widget child;
final MouseCursor? Function(Offset offset) cursorByOffset;
final MouseCursor defaultCursor;

const _HoverRegion({
Key? key,
required this.child,
required this.cursorByOffset,
this.defaultCursor = MouseCursor.defer,
}) : super(key: key);

@override
State<_HoverRegion> createState() => _HoverRegionState();
}

class _HoverRegionState extends State<_HoverRegion> {
Offset? _position;
MouseCursor? _cursor;

@override
Widget build(BuildContext context) {
if (_position != null) _updateHovering(_position!, rebuild: false);
// Listener is necessary because MouseRegion.onHover only gets triggered
// without buttons pressed
return Listener(
behavior: HitTestBehavior.translucent,
onPointerHover: _updatePointer,
onPointerMove: _updatePointer,
child: GestureDetector(
child: MouseRegion(
opaque: false,
hitTestBehavior: HitTestBehavior.translucent,
cursor: _cursor ?? widget.defaultCursor,
onExit: (e) => _setCursor(null),
child: widget.child,
),
),
);
}

void _updatePointer(PointerEvent event, {bool rebuild = true}) {
_updateHovering(event.localPosition, rebuild: rebuild);
}

void _updateHovering(Offset offset, {bool rebuild = true}) {
_setCursor(widget.cursorByOffset(_position = offset), rebuild: rebuild);
}

void _setCursor(MouseCursor? cursor, {bool rebuild = true}) {
if (_cursor == cursor) return;
_cursor = cursor;
if (rebuild) setState(() {});
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dev_dependencies:
flutter_test:
sdk: flutter
mocktail: ^1.0.0
dartdoc: ^8.0.13
dartdoc: any

flutter:

Expand Down

0 comments on commit f971edd

Please sign in to comment.