focus_manager.dart
1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import 'package:flutter/widgets.dart';
mixin FocusManagerMixin<T extends StatefulWidget> on State<T> {
FocusNode get focusNode;
late FocusNode _oldFocusNode;
late FocusAttachment _focusAttachment;
@override
void initState() {
super.initState();
_focusAttachment = focusNode.attach(context);
_oldFocusNode = focusNode;
}
@override
void didUpdateWidget(T oldWidget) {
if (focusNode != _oldFocusNode) {
_focusAttachment.detach();
_focusAttachment = focusNode.attach(context);
_oldFocusNode = focusNode;
}
super.didUpdateWidget(oldWidget);
}
@override
void dispose() {
_focusAttachment.detach();
super.dispose();
}
@mustCallSuper
@override
Widget build(BuildContext context) {
super.build(context);
_focusAttachment.reparent();
return _NullWidget();
}
}
class _NullWidget extends StatelessWidget {
const _NullWidget();
@override
Widget build(BuildContext context) {
throw FlutterError(
'Widgets that mix FocusManagerMixin into their State must call'
'super.build() but must ignore the return value of the superclass.');
}
}