value_notifier_plus 0.3.0+4 copy "value_notifier_plus: ^0.3.0+4" to clipboard
value_notifier_plus: ^0.3.0+4 copied to clipboard

ValueNotifierPlus é um pacote que expande as funcionalidades de ValueNotifier do Flutter.

example/lib/main.dart

import 'dart:developer';

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

class CounterNotifier extends ValueNotifierPlus<int> {
  CounterNotifier() : super(0);

  void increment() => emit(state + 1);
  void decrement() => emit(state - 1);
}

class MyObserver extends ObserverPlus {
  @override
  void onChange<ValueNotifierPlusType extends ValueNotifierPlus>(
    ValueNotifierPlusType notifier,
    Object? state,
  ) {
    log('Notifier: $notifier, State: $state');
  }
}

void main() {
  ValueNotifierPlus.observer = MyObserver();

  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return ProviderPlus(
      provider: CounterNotifier(),
      child: Builder(
        builder: (context) => const MaterialApp(
          home: CounterPage(),
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final counter = context.of<CounterNotifier>();

    return Scaffold(
      appBar: AppBar(
        title: const Text('Counter Notifier'),
      ),
      body: Center(
        child: ConsumerPlus<CounterNotifier, int>(
          notifier: counter,
          listener: (context, state) {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text('State changed to $state')),
            );
          },
          builder: (context, count) {
            return Text(
              '$count',
              style: const TextStyle(fontSize: 24),
            );
          },
        ),
      ),
      floatingActionButton: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          FloatingActionButton(
            onPressed: () {
              counter.increment();
            },
            child: const Icon(Icons.add),
          ),
          const SizedBox(height: 8),
          FloatingActionButton(
            onPressed: () {
              counter.decrement();
            },
            child: const Icon(Icons.remove),
          ),
        ],
      ),
    );
  }
}
7
likes
150
points
0
downloads

Publisher

verified publisherflutterando.com.br

Weekly Downloads

ValueNotifierPlus é um pacote que expande as funcionalidades de ValueNotifier do Flutter.

Repository (GitHub)
View/report issues

Topics

#reactive-programming #state-management #observables #reactive #bloc

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on value_notifier_plus