hooks library
Hooks for widgets of the twk package, in the flutter_hooks style:
state, effects, memoized values, listenables, streams, futures,
animations and controllers that a build keeps by calling functions
(useState, useEffect, …), over the framework's elements.
The build of a HookWidget, of the state of a StatefulHookWidget or
of a HookBuilder calls them; a hook of one's own is a Hook and its
HookState, which use puts to work.
Classes
-
Hook<
R> - What a build uses a HookState by: the description, made anew by each build, that the state its first use created is updated with, as a widget updates its element.
- HookBuilder
- Shows what builder returns, which calls hooks: a HookWidget written in place.
-
HookState<
R, T extends Hook< R> > - What the element of a build that uses a Hook keeps for it between builds, and the value use gives the build (build).
- HookWidget
- A StatelessWidget whose build calls hooks.
-
ObjectRef<
T> - A box whose value can be read and replaced: what useRef keeps.
- StatefulHookWidget
- A StatefulWidget whose state's build calls hooks, besides keeping what a State keeps. Its hooks are deactivated before the state and disposed before it, activated after it.
- TextEditingControllerHookCreator
- What useTextEditingController is: a call or a fromValue.
Mixins
- HookElement
- Makes a ComponentElement keep the states of the hooks its build uses: the element of a HookWidget and of a StatefulHookWidget, and of other widgets whose builds call hooks.
Constants
- useTextEditingController → const TextEditingControllerHookCreator
-
A TextEditingController of the first build, of a text, or of a value
(
useTextEditingController.fromValue), made anew when the keys of a build differ from the last one's; the text of later builds is ignored.
Functions
-
use<
R> (Hook< R> hook) → R -
The value of
hookfor the build running: the value of its state, which the first build to use a hook in this place creates; see HookState. Called by the build of a HookWidget, of the state of a StatefulHookWidget or of a HookBuilder, directly or by the functions it calls, such asuseState. -
useAnimationController(
{Duration? duration, Duration? reverseDuration, double? initialValue, double lowerBound = 0, double upperBound = 1, TickerProvider? vsync, List< Object?> ? keys}) → AnimationController -
An AnimationController made by the first build, made anew when the
keysof a build differ from the last one's, and disposed with the element or when replaced. Its ticker is the hook's own, ticking in the frames of the app the widget is in, unlessvsyncgives one. That ticker is muted (Ticker.muted) while a TickerMode above is off, so that the controller stands still and asks for no frame, and goes on when it is on again, its time having gone on meanwhile: it is where it would have been. Avsyncis muted by its own owner. -
useCallback<
T extends Function> (T callback, [List< Object?> keys = const []]) → T -
callbackas the first build gave it, until a build whosekeysdiffer from the last one's: a function that stays identical for the widgets it is given to. -
useContext(
) → BuildContext - The context of the build running, in which a function that uses hooks can look up what is above it.
-
useEffect(
VoidCallback? effect(), [List< Object?> ? keys]) → void -
Runs
effectin the first build, and the function it returns, if any, when the element is unmounted. Withkeys, in a later build whose keys differ from the last one's, the function the last run returned runs first, theneffectagain; without, that happens in every build. -
useFocusNode(
{String? debugLabel, FocusOnKeyEventCallback? onKeyEvent, bool canRequestFocus = true, bool skipTraversal = false, bool descendantsAreFocusable = true}) → FocusNode -
A FocusNode of the first build, for a
Focuswidget'sfocusNode; each build sets itsdebugLabel,onKeyEvent,canRequestFocus,skipTraversalanddescendantsAreFocusable. -
useFocusScopeNode(
{String? debugLabel, FocusOnKeyEventCallback? onKeyEvent, bool canRequestFocus = true, bool skipTraversal = false, bool descendantsAreFocusable = true}) → FocusScopeNode -
A FocusScopeNode of the first build, for a
FocusScopewidget'snode; each build sets what useFocusNode's does. -
useFuture<
T> (Future< T> ? future, {T? initialData, bool preserveState = true}) → AsyncSnapshot<T> -
The latest snapshot of
future, frominitialDataon: waiting while the future runs, done with its value or error once it completes, which rebuilds the element, and none while there is no future. A snapshot keeps the latest data or error until the next. -
useInterval(
Duration period, void onTick()) → void -
Calls
onTickof the latest build once everyperiod, from the first build on, on the clock of the app the widget is in, until the element is disposed. The ticks areStepsof the app's clock: they fall in the frames at the multiples ofperiodsince the app started, so the first comes within oneperiodof the build, not after a whole one, and all that step by the same period tick in the same frame. A tick is called in a frame, where asetStateis as usual. -
useListController(
{int initialScrollOffset = 0, List< Object?> ? keys}) → ListController -
A ListController of the first build, whose list starts at
initialScrollOffset, made anew when thekeysof a build differ from the last one's. -
useListenable<
T extends Listenable?> (T listenable) → T -
listenable, whose notifications rebuild the element from this build on: the listener goes over to the listenable of a later build, and is removed when the element is unmounted. Null listens to nothing. -
useMemoized<
T> (T valueBuilder(), [List< Object?> keys = const []]) → T -
What
valueBuilderreturns in the first build, kept until a build whosekeysdiffer from the last one's calls it again. -
useRef<
T> (T initialValue) → ObjectRef< T> -
A box of
initialValue, the same one in every build: what is put in it stays without rebuilding anything. -
useScrollController(
{int initialScrollOffset = 0, List< Object?> ? keys}) → ScrollController -
A ScrollController of the first build, whose scrollable starts at
initialScrollOffset, made anew when thekeysof a build differ from the last one's. -
useState<
T> (T initialData) → ValueNotifier< T> -
A notifier of
initialData, made by the first build and disposed with the element, whose changes rebuild it:count.value++in a callback shows the new count. Later builds'initialDatais ignored. -
useStream<
T> (Stream< T> ? stream, {T? initialData, bool preserveState = true}) → AsyncSnapshot<T> -
The latest snapshot of
stream, frominitialDataon: waiting once the stream is listened to, active with each event it delivers, done once it closes, and none while there is no stream; each event rebuilds the element. A snapshot keeps the latest data or error until the next. -
useTicker(
TickerCallback onTick) → Ticker -
A Ticker made by the first build, not started, that calls the
onTickof the latest build once a frame while it is active, in the frames of the app the widget is in; disposed with the element. It is muted (Ticker.muted) while a TickerMode above is off, its time going on. -
useValueChanged<
T, R> (T value, R? valueChange(T oldValue, R? oldResult)) → R? -
What
valueChangereturned the last timevaluechanged: null in the first build, then, in each build whosevalueis not equal to the last one's,valueChangeof the last value and of what it returned before. -
useValueListenable<
T> (ValueListenable< T> valueListenable) → T -
The value of
valueListenable, whose notifications rebuild the element, as useListenable listens. -
useValueNotifier<
T> (T initialData, [List< Object?> ? keys]) → ValueNotifier<T> -
A notifier of
initialData, made by the first build, made anew when thekeysof a build differ from the last one's, and disposed with the element or when replaced. UnlikeuseState's, its changes rebuild nothing: useListenable or aValueListenableBuilderlisten to it.