Unverified Commit 3b945da0 authored by Greg Spencer's avatar Greg Spencer Committed by GitHub

Change focus example to be more canonical (and correct), listening to the...

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.
parent 67ee3e19
...@@ -236,6 +236,7 @@ class FocusAttachment { ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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'),
/// ), /// ),
/// ), /// ),
/// ); /// );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment