Last active
September 16, 2020 13:05
-
-
Save JoseAlba/d16345202d0e26d40fe14904657dc24a to your computer and use it in GitHub Desktop.
Focusable Action Detector
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'; | |
import 'package:flutter/services.dart'; | |
void main() { | |
runApp(MaterialApp(home: FAD())); | |
} | |
class FAD extends StatefulWidget { | |
FAD({Key key}) : super(key: key); | |
@override | |
_FADState createState() => _FADState(); | |
} | |
class _FADState extends State<FAD> { | |
Map<LogicalKeySet, Intent> _shortcutMap; | |
Map<Type, Action<Intent>> _actionMap; | |
MouseCursor _mouseCursor; | |
@override | |
void initState() { | |
super.initState(); | |
_mouseCursor = SystemMouseCursors.basic; | |
_shortcutMap = <LogicalKeySet, Intent>{ | |
LogicalKeySet(LogicalKeyboardKey.keyP): | |
const _ShowSecretMessageIntent.P(), | |
LogicalKeySet(LogicalKeyboardKey.keyL): | |
const _ShowSecretMessageIntent.L(), | |
LogicalKeySet(LogicalKeyboardKey.keyA): | |
const _ShowSecretMessageIntent.A(), | |
LogicalKeySet(LogicalKeyboardKey.keyT): | |
const _ShowSecretMessageIntent.T(), | |
LogicalKeySet(LogicalKeyboardKey.keyF): | |
const _ShowSecretMessageIntent.F(), | |
LogicalKeySet(LogicalKeyboardKey.keyO): | |
const _ShowSecretMessageIntent.O(), | |
LogicalKeySet(LogicalKeyboardKey.keyR): | |
const _ShowSecretMessageIntent.R(), | |
LogicalKeySet(LogicalKeyboardKey.keyM): | |
const _ShowSecretMessageIntent.M(), | |
}; | |
_actionMap = <Type, Action<Intent>>{ | |
_ShowSecretMessageIntent: CallbackAction<_ShowSecretMessageIntent>( | |
onInvoke: _actionHandler, | |
), | |
}; | |
} | |
List<Widget> children = [ | |
Expanded( | |
child: Focus( | |
autofocus: true, | |
child: Container( | |
color: Colors.red, | |
child: Text('Press P'), | |
), | |
), | |
), | |
]; | |
void _actionHandler(_ShowSecretMessageIntent intent) { | |
switch (intent.type) { | |
case _SecretMessageType.P: | |
setState(() { | |
children.add( | |
Expanded( | |
child: Container( | |
color: Colors.orange, | |
child: Text('Press L'), | |
), | |
), | |
); | |
}); | |
break; | |
case _SecretMessageType.L: | |
setState(() { | |
children.add( | |
Expanded( | |
child: Container( | |
color: Colors.yellow, | |
child: Text('Press A'), | |
), | |
), | |
); | |
}); | |
break; | |
case _SecretMessageType.A: | |
setState(() { | |
children.add( | |
Expanded( | |
child: Container( | |
color: Colors.green, | |
child: Text('Press T'), | |
), | |
), | |
); | |
}); | |
break; | |
case _SecretMessageType.T: | |
setState(() { | |
children.add( | |
Expanded( | |
child: Container( | |
color: Colors.blue, | |
child: Text('Press F'), | |
), | |
), | |
); | |
}); | |
break; | |
case _SecretMessageType.F: | |
setState(() { | |
children.add( | |
Expanded( | |
child: Container( | |
color: Colors.indigo, | |
child: Text('Press O'), | |
), | |
), | |
); | |
}); | |
break; | |
case _SecretMessageType.O: | |
setState(() { | |
children.add( | |
Expanded( | |
child: Container( | |
color: Colors.pink, | |
child: Text('Press R'), | |
), | |
), | |
); | |
}); | |
break; | |
case _SecretMessageType.R: | |
setState(() { | |
children.add( | |
Expanded( | |
child: Container( | |
color: Colors.white, | |
child: Text('Press M'), | |
), | |
), | |
); | |
}); | |
break; | |
case _SecretMessageType.M: | |
setState(() { | |
children.add( | |
Expanded( | |
child: Container( | |
color: Colors.black, | |
child: Text('Done'), | |
), | |
), | |
); | |
}); | |
break; | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return FocusableActionDetector( | |
actions: _actionMap, | |
shortcuts: _shortcutMap, | |
mouseCursor: _mouseCursor, | |
child: Scaffold( | |
appBar: AppBar( | |
title: const Text('Focusable Action Detector'), | |
actions: [ | |
IconButton( | |
icon: Icon(Icons.filter_1), | |
onPressed: () { | |
setState(() { | |
_mouseCursor = SystemMouseCursors.basic; | |
}); | |
}, | |
), | |
IconButton( | |
icon: Icon(Icons.filter_2), | |
onPressed: () { | |
setState(() { | |
_mouseCursor = SystemMouseCursors.text; | |
}); | |
}, | |
), | |
IconButton( | |
icon: Icon(Icons.filter_3), | |
onPressed: () { | |
setState(() { | |
_mouseCursor = SystemMouseCursors.click; | |
}); | |
}, | |
), | |
IconButton( | |
icon: Icon(Icons.filter_4), | |
onPressed: () { | |
setState(() { | |
_mouseCursor = SystemMouseCursors.forbidden; | |
}); | |
}, | |
), | |
IconButton( | |
icon: Icon(Icons.filter_5), | |
onPressed: () { | |
setState(() { | |
_mouseCursor = SystemMouseCursors.grab; | |
}); | |
}, | |
), | |
IconButton( | |
icon: Icon(Icons.filter_6), | |
onPressed: () { | |
setState(() { | |
_mouseCursor = SystemMouseCursors.grabbing; | |
}); | |
}, | |
), | |
IconButton( | |
icon: Icon(Icons.filter_7), | |
onPressed: () { | |
setState(() { | |
_mouseCursor = SystemMouseCursors.none; | |
}); | |
}, | |
), | |
], | |
), | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: children, | |
), | |
), | |
); | |
} | |
} | |
class _ShowSecretMessageIntent extends Intent { | |
const _ShowSecretMessageIntent({@required this.type}); | |
const _ShowSecretMessageIntent.P() : type = _SecretMessageType.P; | |
const _ShowSecretMessageIntent.L() : type = _SecretMessageType.L; | |
const _ShowSecretMessageIntent.A() : type = _SecretMessageType.A; | |
const _ShowSecretMessageIntent.T() : type = _SecretMessageType.T; | |
const _ShowSecretMessageIntent.F() : type = _SecretMessageType.F; | |
const _ShowSecretMessageIntent.O() : type = _SecretMessageType.O; | |
const _ShowSecretMessageIntent.R() : type = _SecretMessageType.R; | |
const _ShowSecretMessageIntent.M() : type = _SecretMessageType.M; | |
final _SecretMessageType type; | |
} | |
enum _SecretMessageType { | |
P, | |
L, | |
A, | |
T, | |
F, | |
O, | |
R, | |
M, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment