/
text_controller.dart
94 lines (82 loc) · 2.96 KB
/
text_controller.dart
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
part of 'hooks.dart';
class _TextEditingControllerHookCreator {
const _TextEditingControllerHookCreator();
/// Creates a [TextEditingController] that will be disposed automatically.
///
/// The [text] parameter can be used to set the initial value of the
/// controller.
TextEditingController call({String? text, List<Object?>? keys}) {
return use(_TextEditingControllerHook(text, keys));
}
/// Creates a [TextEditingController] from the initial [value] that will
/// be disposed automatically.
TextEditingController fromValue(
TextEditingValue value, [
List<Object?>? keys,
]) {
return use(_TextEditingControllerHook.fromValue(value, keys));
}
}
/// Creates a [TextEditingController], either via an initial text or an initial
/// [TextEditingValue].
///
/// To use a [TextEditingController] with an optional initial text, use:
/// ```dart
/// final controller = useTextEditingController(text: 'initial text');
/// ```
///
/// To use a [TextEditingController] with an optional initial value, use:
/// ```dart
/// final controller = useTextEditingController
/// .fromValue(TextEditingValue.empty);
/// ```
///
/// Changing the text or initial value after the widget has been built has no
/// effect whatsoever. To update the value in a callback, for instance after a
/// button was pressed, use the [TextEditingController.text] or
/// [TextEditingController.value] setters. To have the [TextEditingController]
/// reflect changing values, you can use [useEffect]. This example will update
/// the [TextEditingController.text] whenever a provided [ValueListenable]
/// changes:
/// ```dart
/// final controller = useTextEditingController();
/// final update = useValueListenable(myTextControllerUpdates);
///
/// useEffect(() {
/// controller.text = update;
/// }, [update]);
/// ```
///
/// See also:
/// - [TextEditingController], which this hook creates.
const useTextEditingController = _TextEditingControllerHookCreator();
class _TextEditingControllerHook extends Hook<TextEditingController> {
const _TextEditingControllerHook(
this.initialText, [
List<Object?>? keys,
]) : initialValue = null,
super(keys: keys);
const _TextEditingControllerHook.fromValue(
TextEditingValue this.initialValue, [
List<Object?>? keys,
]) : initialText = null,
super(keys: keys);
final String? initialText;
final TextEditingValue? initialValue;
@override
_TextEditingControllerHookState createState() {
return _TextEditingControllerHookState();
}
}
class _TextEditingControllerHookState
extends HookState<TextEditingController, _TextEditingControllerHook> {
late final _controller = hook.initialValue != null
? TextEditingController.fromValue(hook.initialValue)
: TextEditingController(text: hook.initialText);
@override
TextEditingController build(BuildContext context) => _controller;
@override
void dispose() => _controller.dispose();
@override
String get debugLabel => 'useTextEditingController';
}