Skip to content

Instantly share code, notes, and snippets.

@this-is-richard
Created September 26, 2019 03:25
Show Gist options
  • Save this-is-richard/918a63933dc1836a17165d8e5a2cc49a to your computer and use it in GitHub Desktop.
Save this-is-richard/918a63933dc1836a17165d8e5a2cc49a to your computer and use it in GitHub Desktop.
class TouchedDetector extends StatefulWidget {
final Function(FocusNode, bool) renderer;
const TouchedDetector({Key key, this.renderer}) : super(key: key);
@override
_TouchedDetectorState createState() => _TouchedDetectorState();
}
class _TouchedDetectorState extends State<TouchedDetector> {
final _focusNode = FocusNode();
List<bool> _state = [];
@override
void initState() {
super.initState();
_focusNode.addListener(() {
setState(() {
_state.add(_focusNode.hasFocus);
});
});
}
@override
Widget build(BuildContext context) {
final _autoValidate = _state.length >= 2;
final child = widget.renderer(_focusNode, _autoValidate);
return Container(
child: child,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment