reactive_notifier 2.2.1 copy "reactive_notifier: ^2.2.1" to clipboard
reactive_notifier: ^2.2.1 copied to clipboard

A Dart library for managing reactive state efficiently, supporting multiples related state.

example/reactive_notifier_example.dart

import 'package:flutter/material.dart';
import 'package:reactive_notifier/reactive_notifier.dart';

enum ConnectionState {
  connected,
  unconnected,
  connecting,
  error,
  uploading,
  waiting,
  signalOff,
  errorOnSynchronized,
  synchronizing,
  synchronized,
  waitingForSynchronization
}

/// Test for current state [ReactiveNotifier].
final ReactiveNotifier<ConnectionState> reactiveConnectionState =
    ReactiveNotifier<ConnectionState>(() {
  /// You can put any code for initial value.
  return ConnectionState.signalOff;
});

void main() {
  /// Ensure flutter initialized.
  WidgetsFlutterBinding.ensureInitialized();

  runApp(
    MaterialApp(
      initialRoute: '/',
      routes: {
        '/': (BuildContext context) => const MyApp(),
      },
    ),
  );
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('ReactiveNotifier'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(10.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            /// 1. [ReactiveNotifier] Current connection state
            ReactiveBuilder<ConnectionState>(
              valueListenable: reactiveConnectionState,
              builder: (context, state, keep) {
                bool isConnected = state == ConnectionState.connected;
                return Column(
                  children: [
                    /// Prevents the widget from rebuilding.
                    /// Useful when you want to reuse it in another ReactiveBuilder.
                    keep(const Text("No state update")),

                    Chip(
                      label: Text(
                        state.name,
                      ),
                      deleteIcon: const Icon(Icons.remove_circle),
                      avatar: Icon(
                        Icons.wifi,
                        color: isConnected ? Colors.green : Colors.red,
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
      floatingActionButton: OutlinedButton(
        onPressed: () {
          /// Variation unconnected and connected.
          reactiveConnectionState.updateState(
              reactiveConnectionState.value == ConnectionState.connected
                  ? ConnectionState.unconnected
                  : ConnectionState.connected);
        },
        child: const Text('ReactiveNotifier'),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }
}
copied to clipboard
2
likes
0
points
153
downloads

Publisher

verified publisherjhonacode.com

Weekly Downloads

2024.07.10 - 2025.01.22

A Dart library for managing reactive state efficiently, supporting multiples related state.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on reactive_notifier