Skip to main content

an_state 1.4.2 copy "an_state: ^1.4.2" to clipboard
an_state: ^1.4.2 copied to clipboard

A reactive state management library for Flutter, providing seamless integration between ViewModels, Widgets, and reactive observables.

example/example.dart

import 'package:flutter/material.dart';
import 'package:an_state/an_state.dart';
import 'package:an_viewmodel/an_viewmodel.dart';
import 'package:anlifecycle/anlifecycle.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return LifecycleApp(
      child: MaterialApp(
        navigatorObservers: [LifecycleNavigatorObserver.hookMode()],
        home: const HomePage(),
      ),
    );
  }
}

class CounterViewModel extends ViewModel {
  late final count = stateMutableOf(0.asStateOf);
  late final isEven = stateOf(() => count.value % 2 == 0);

  void increment() {
    count.value++;
  }
}

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return const HomePageContent();
  }
}

class HomePageContent extends StatelessWidget {
  const HomePageContent({super.key});

  @override
  Widget build(BuildContext context) {
    // Accessing the ViewModel via context.viewModels()
    // The factory is provided here; it will only be called if the ViewModel doesn't exist yet.
    final vm =
        context.viewModels<CounterViewModel>(factory: CounterViewModel.new);

    return Scaffold(
      appBar: AppBar(title: const Text('an_state Example')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('Count (from ViewModel): ${context.listenReactiveState(vm.count)}'),
            Text('Is Even: ${context.listenReactiveState(vm.isEven)}'),
            const SizedBox(height: 20),
            const LocalCounter(),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: vm.increment,
        child: const Icon(Icons.add),
      ),
    );
  }
}

class LocalCounter extends StatelessWidget {
  const LocalCounter({super.key});

  @override
  Widget build(BuildContext context) {
    // Using rememberMutableState for local UI state with .asStateOf
    final localCount = context.rememberMutableState(0.asStateOf);

    return Column(
      children: [
        const Text('Local state (using remember):'),
        Text('${localCount.value}',
            style: Theme.of(context).textTheme.headlineMedium),
        ElevatedButton(
          onPressed: () => localCount.value++,
          child: const Text('Increment Local'),
        ),
      ],
    );
  }
}
1
likes
160
points
403
downloads

Documentation

API reference

Publisher

verified publisheraymtools.com

Weekly Downloads

A reactive state management library for Flutter, providing seamless integration between ViewModels, Widgets, and reactive observables.

Repository (GitHub)
View/report issues

License

Apache-2.0 (license)

Dependencies

an_async_data, an_lifecycle_cancellable, an_pager, an_reactive_state, an_viewmodel, anlifecycle, cancellable, flutter, remember

More

Packages that depend on an_state