Change focus example to be more canonical (and correct), listening to the focus node for changes. (#35913)

This changes the example for FocusNode to be more correct, listening to the focus node for changes, instead of assuming that it is the only one doing the changing.
This commit is contained in:
Greg Spencer 2019-07-10 16:49:32 -07:00 committed by GitHub
parent 67ee3e191e
commit 3b945da00d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -236,6 +236,7 @@ class FocusAttachment {
/// ///
/// class _ColorfulButtonState extends State<ColorfulButton> { /// class _ColorfulButtonState extends State<ColorfulButton> {
/// FocusNode _node; /// FocusNode _node;
/// bool _focused = false;
/// FocusAttachment _nodeAttachment; /// FocusAttachment _nodeAttachment;
/// Color _color = Colors.white; /// Color _color = Colors.white;
/// ///
@ -243,9 +244,18 @@ class FocusAttachment {
/// void initState() { /// void initState() {
/// super.initState(); /// super.initState();
/// _node = FocusNode(debugLabel: 'Button'); /// _node = FocusNode(debugLabel: 'Button');
/// _node.addListener(_handleFocusChange);
/// _nodeAttachment = _node.attach(context, onKey: _handleKeyPress); /// _nodeAttachment = _node.attach(context, onKey: _handleKeyPress);
/// } /// }
/// ///
/// void _handleFocusChange() {
/// if (_node.hasFocus != _focused) {
/// setState(() {
/// _focused = _node.hasFocus;
/// });
/// }
/// }
///
/// bool _handleKeyPress(FocusNode node, RawKeyEvent event) { /// bool _handleKeyPress(FocusNode node, RawKeyEvent event) {
/// if (event is RawKeyDownEvent) { /// if (event is RawKeyDownEvent) {
/// print('Focus node ${node.debugLabel} got key event: ${event.logicalKey}'); /// print('Focus node ${node.debugLabel} got key event: ${event.logicalKey}');
@ -274,6 +284,7 @@ class FocusAttachment {
/// ///
/// @override /// @override
/// void dispose() { /// void dispose() {
/// _node.removeListener(_handleFocusChange);
/// // The attachment will automatically be detached in dispose(). /// // The attachment will automatically be detached in dispose().
/// _node.dispose(); /// _node.dispose();
/// super.dispose(); /// super.dispose();
@ -284,24 +295,20 @@ class FocusAttachment {
/// _nodeAttachment.reparent(); /// _nodeAttachment.reparent();
/// return GestureDetector( /// return GestureDetector(
/// onTap: () { /// onTap: () {
/// if (_node.hasFocus) { /// if (_focused) {
/// setState(() {
/// _node.unfocus(); /// _node.unfocus();
/// });
/// } else { /// } else {
/// setState(() { /// _node.requestFocus();
/// _node.requestFocus();
/// });
/// } /// }
/// }, /// },
/// child: Center( /// child: Center(
/// child: Container( /// child: Container(
/// width: 400, /// width: 400,
/// height: 100, /// height: 100,
/// color: _node.hasFocus ? _color : Colors.white, /// color: _focused ? _color : Colors.white,
/// alignment: Alignment.center, /// alignment: Alignment.center,
/// child: Text( /// child: Text(
/// _node.hasFocus ? "I'm in color! Press R,G,B!" : 'Press to focus'), /// _focused ? "I'm in color! Press R,G,B!" : 'Press to focus'),
/// ), /// ),
/// ), /// ),
/// ); /// );