property_change_notifier 0.1.4 copy "property_change_notifier: ^0.1.4" to clipboard
property_change_notifier: ^0.1.4 copied to clipboard

outdated

A drop-in replacement for ChangeNotifier for observing only certain properties of a model.

example/lib/main.dart

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

class MyModel with PropertyChangeNotifier<String> {
  int _foo = 0;
  int _bar = 0;

  int get foo => _foo;
  int get bar => _bar;

  set foo(int value) {
    _foo = value;
    notifyListeners('foo');
  }

  set bar(int value) {
    _bar = value;
    notifyListeners('bar');
  }
}

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Property Change Notifier',
      home: Scaffold(
        body: PropertyChangeProvider(
          value: MyModel(),
          child: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                GlobalListener(),
                FooListener(),
                BarListener(),
                FooUpdater(),
                BarUpdater(),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class GlobalListener extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return PropertyChangeConsumer<MyModel>(
      builder: (context, model, property) {
        if (property == null) return Container();
        return Text('$property changed');
      },
    );
  }
}

class FooListener extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return PropertyChangeConsumer<MyModel>(
      properties: ['foo'],
      builder: (context, model, property) {
        return Text('Foo is ${model.foo}');
      },
    );
  }
}

class BarListener extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return PropertyChangeConsumer<MyModel>(
      properties: ['bar'],
      builder: (context, model, property) {
        return Text('Bar is ${model.bar}');
      },
    );
  }
}

class FooUpdater extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final model = PropertyChangeProvider.of<MyModel>(context, listen: false).value;
    return RaisedButton(
      child: Text('Update foo'),
      onPressed: () => model.foo++
    );
  }
}

class BarUpdater extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final model = PropertyChangeProvider.of<MyModel>(context, listen: false).value;
    return RaisedButton(
      child: Text('Update bar'),
      onPressed: () => model.bar++
    );
  }
}
55
likes
0
pub points
90%
popularity

Publisher

verified publishermartinrybak.com

A drop-in replacement for ChangeNotifier for observing only certain properties of a model.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on property_change_notifier