Skip to content

Flutter Package API (flutter_reacton)

Complete API reference for the flutter_reacton package -- Flutter widgets, context extensions, form state management, and lifecycle utilities for Reacton.

dart
import 'package:flutter_reacton/flutter_reacton.dart';

This package re-exports everything from package:reacton/reacton.dart, so a single import gives you both the core library and Flutter bindings.


Widgets

ReactonScope

Provides a ReactonStore to the widget tree. Wrap your app (or a subtree) with ReactonScope to make reactons available via context extensions.

dart
ReactonScope({
  Key? key,
  ReactonStore? store,
  List<ReactonOverride>? overrides,
  required Widget child,
})
ParameterTypeDefaultDescription
storeReactonStore?nullExisting store (creates a new one if not provided)
overridesList<ReactonOverride>?nullOverride reacton values (useful for testing)
childWidgetrequiredThe widget subtree

Static Methods

MethodSignatureDescription
of(context)ReactonStoreGet the store from the nearest ancestor (creates dependency -- rebuilds on store change)
read(context)ReactonStoreGet the store without creating a dependency
maybeOf(context)ReactonStore?Get the store if available, or null

ReactonOverride

Override a reacton's value in a ReactonScope.

dart
const ReactonOverride<T>(ReactonBase<T> reacton, T value)

ReactonBuilder<T>

A widget that rebuilds when a single reacton's value changes.

dart
const ReactonBuilder<T>({
  Key? key,
  required ReactonBase<T> reacton,
  required Widget Function(BuildContext context, T value) builder,
})
ParameterTypeDescription
reactonReactonBase<T>The reacton to watch
builderWidget Function(BuildContext, T)Builder called with the current value

Example:

dart
ReactonBuilder(
  reacton: counterReacton,
  builder: (context, count) => Text('$count'),
)

ReactonConsumer

A widget that provides a ReactonWidgetRef for watching multiple reactons within a single builder.

dart
const ReactonConsumer({
  Key? key,
  required Widget Function(BuildContext context, ReactonWidgetRef ref) builder,
})
ParameterTypeDescription
builderWidget Function(BuildContext, ReactonWidgetRef)Builder with access to a ref object

Example:

dart
ReactonConsumer(
  builder: (context, ref) {
    final count = ref.watch(counterReacton);
    final name = ref.watch(nameReacton);
    return Text('$name: $count');
  },
)

ReactonWidgetRef

Ref object provided by ReactonConsumer for accessing reactons.

MethodSignatureDescription
watch<T>(reacton)TWatch a reacton (rebuilds on change)
read<T>(reacton)TRead without subscribing
set<T>(reacton, value)voidSet a writable reacton's value
update<T>(reacton, updater)voidUpdate using a function
storeReactonStoreThe underlying store

ReactonListener<T>

A widget that listens to reacton changes for side effects without rebuilding the child widget.

dart
const ReactonListener<T>({
  Key? key,
  required ReactonBase<T> reacton,
  required void Function(BuildContext context, T value) listener,
  bool Function(T previous, T current)? listenWhen,
  required Widget child,
})
ParameterTypeDescription
reactonReactonBase<T>The reacton to listen to
listenervoid Function(BuildContext, T)Called when the value changes
listenWhenbool Function(T, T)?Optional condition for when to call the listener
childWidgetChild widget (not rebuilt)

Example:

dart
ReactonListener(
  reacton: errorReacton,
  listener: (context, error) {
    if (error != null) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text(error)),
      );
    }
  },
  child: MyWidget(),
)

ReactonSelector<T, S>

A widget that rebuilds only when a selected sub-value changes. More efficient than ReactonBuilder when you only need a small part of a complex value.

dart
const ReactonSelector<T, S>({
  Key? key,
  required ReactonBase<T> reacton,
  required S Function(T value) selector,
  required Widget Function(BuildContext context, S selected) builder,
})
ParameterTypeDescription
reactonReactonBase<T>The source reacton
selectorS Function(T)Function to extract the sub-value
builderWidget Function(BuildContext, S)Builder called with the selected value

Example:

dart
ReactonSelector<User, String>(
  reacton: userReacton,
  selector: (user) => user.name,
  builder: (context, name) => Text(name),
)

BuildContext Extensions

The primary API for using Reacton in Flutter widgets.

ReactonBuildContextExtension

MethodSignatureDescription
watch<T>(reacton)TWatch a reacton -- rebuilds this widget when the value changes
read<T>(reacton)TRead the current value without subscribing (use in event handlers)
set<T>(reacton, value)voidSet a writable reacton's value
update<T>(reacton, updater)voidUpdate a writable reacton using a function
reactonStoreReactonStoreAccess the store directly

Example:

dart
Widget build(BuildContext context) {
  final count = context.watch(counterReacton);
  return Column(
    children: [
      Text('$count'),
      ElevatedButton(
        onPressed: () => context.update(counterReacton, (c) => c + 1),
        child: Text('Increment'),
      ),
      ElevatedButton(
        onPressed: () => context.set(counterReacton, 0),
        child: Text('Reset'),
      ),
    ],
  );
}

Form State Management

FormReacton

A reactive form that manages a group of field reactons. Extends WritableReacton<FormState>.

dart
FormReacton reactonForm({
  required Map<String, FieldReacton> fields,
  String? name,
})
MemberTypeDescription
fieldsMap<String, FieldReacton>Map of field names to field reactons

FormState

FieldTypeDefaultDescription
isSubmittingboolfalseWhether the form is currently submitting
isSubmittedboolfalseWhether the form has been submitted
submitErrorString?nullError from the last submit attempt
submitCountint0Number of submit attempts

FieldReacton<T>

A reactive form field with validation, dirty tracking, and touch state. Extends WritableReacton<FieldState<T>>.

dart
FieldReacton<T> reactonField<T>(
  T initialValue, {
  List<Validator<T>> validators = const [],
  Future<String?> Function(T value)? asyncValidator,
  String? name,
})
MemberTypeDescription
validatorsList<Validator<T>>Synchronous validators
asyncValidatorFuture<String?> Function(T)?Async validator
initialFieldValueTInitial value before changes
validate(value)String?Run synchronous validators

FieldState<T>

FieldTypeDefaultDescription
valueTrequiredCurrent field value
errorString?nullValidation error message
isDirtyboolfalseWhether the value has changed from initial
isTouchedboolfalseWhether the user has interacted
isValidatingboolfalseWhether async validation is running
isValidboolcomputedWhether error is null

Validators

FactorySignatureDescription
required()Validator<String>String is not empty
minLength(min)Validator<String>Minimum string length
maxLength(max)Validator<String>Maximum string length
email()Validator<String>Valid email format
pattern(regex)Validator<String>Matches a regex pattern
range(min, max)Validator<num>Numeric range
matches(getOther)Validator<String>Two values match (e.g., password confirmation)
compose(validators)Validator<T>Combine multiple validators (returns first error)

Store Form Extensions

MethodSignatureDescription
isFormValid(form)boolCheck if all fields are valid
validateForm(form)boolValidate all fields; returns true if all valid
submitForm(form, {onValid, onError})Future<void>Validate and submit
resetForm(form)voidReset all fields to initial values
touchAllFields(form)voidTouch all fields (shows all errors)
isFormDirty(form)boolCheck if any field has been modified
formField<T>(form, name)FieldReacton<T>Get a specific field by name

Store Field Extensions

MethodSignatureDescription
setFieldValue<T>(field, value)voidSet value with auto-validation and dirty tracking
touchField<T>(field)voidMark field as touched
resetField<T>(field)voidReset field to initial state
fieldValue<T>(field)TGet the current value
fieldError<T>(field)String?Get the current error

Auto-Dispose

AutoDisposeManager

Manages automatic disposal of reactons when they have no active watchers.

dart
AutoDisposeManager(ReactonStore store, {Duration gracePeriod = const Duration(seconds: 5)})
MethodSignatureDescription
onWatch(ref)voidNotify that a watcher started watching
onUnwatch(ref)voidNotify that a watcher stopped watching (starts grace period)
watcherCount(ref)intCurrent watcher count for a reacton
cancelAll()voidCancel all pending disposals
dispose()voidDispose the manager

When the last watcher stops watching a reacton, a grace period timer starts. If no new watchers appear before it expires, the reacton is removed from the store.


What's Next

Released under the MIT License.