Last active
September 30, 2020 18:49
-
-
Save MBeliou/1e761cd9d238347de3711fa5963e5d11 to your computer and use it in GitHub Desktop.
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
import 'package:flutter/material.dart'; | |
import 'package:flutter/rendering.dart'; | |
final Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MouseRegionApp()); | |
} | |
class MouseRegionApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: MyStatefulWidget(), | |
), | |
), | |
); | |
} | |
} | |
class MyStatefulWidget extends StatefulWidget { | |
MyStatefulWidget({Key key}) : super(key: key); | |
@override | |
_MyStatefulWidgetState createState() => _MyStatefulWidgetState(); | |
} | |
class _MyStatefulWidgetState extends State<MyStatefulWidget> { | |
bool onFirst = false; | |
bool onSecond = false; | |
Offset exitFrom = Offset(0,0); | |
MouseCursor _mouseCursor = SystemMouseCursors.basic; | |
@override | |
Widget build(BuildContext context) { | |
return FocusableActionDetector( | |
mouseCursor: _mouseCursor, | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: [ | |
Container(height: 50, color: Colors.white,), | |
Expanded( | |
child: MouseRegion( | |
onEnter: (PointerEvent details) => setState(() => onFirst = true), | |
onExit: (PointerEvent details) => setState(() { | |
onFirst = false; | |
exitFrom = details.position; | |
}), | |
child: Container( | |
color: onFirst ? Colors.red : Colors.blue, | |
child: Center(child: Text(onFirst ? "Look mom, I'm hovering" : "I last exited from $exitFrom")) | |
), | |
), | |
), | |
Expanded( | |
child: MouseRegion( | |
onEnter: (PointerEvent details) => setState(() { | |
onSecond = true; | |
if (_mouseCursor == SystemMouseCursors.progress) { | |
_mouseCursor = SystemMouseCursors.basic; | |
} else { | |
_mouseCursor = SystemMouseCursors.progress; | |
} | |
}), | |
onExit: (PointerEvent details) => | |
setState(() => onSecond = false), | |
child: Container( | |
color: onSecond ? Colors.red : Colors.green, | |
), | |
), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment