value_notifier_plus 0.1.0+9 copy "value_notifier_plus: ^0.1.0+9" to clipboard
value_notifier_plus: ^0.1.0+9 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 ValueNotifierPlusObserver {
  @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 PlusProvider(
      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: ValueNotifierPlusConsumer<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
0
pub points
9%
popularity

Publisher

verified publisherflutterando.com.br

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

Repository (GitHub)
View/report issues

Topics

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

License

unknown (license)

Dependencies

flutter

More

Packages that depend on value_notifier_plus