listenable 0.3.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 57

Listenable #

Listenable help you to manage Flutter widgets and state synchronization by leveraging the [ValueListenable] class. You will annotate you models and it will generate the required code for your models to be listenable by a ListenableBuilder widget.

Usage #

A simple usage example:

1. Add dependencies #

dependencies:
  listenable: ^0.3.0

dev_dependencies:
  listenable_generator: ^0.3.0  
  build_runner: ^1.1.0

2. Annotate your code and add the generated part to your file #

import 'package:listenable/listenable.dart';

part 'main.g.dart';

class Counter = _Counter with _Counter$Listenable;

@ListenableModel()
abstract class _Counter {
  @ListenableProperty()
  int _counter = 0;

  void increment() {
    _counter++;
  }
}

3. Run the generation source command #

 flutter pub run build_runner build

4. Listen for changes in your widgets #

Rebuild you widgets using the flutter existing widgets, you don't need any additional library

import 'package:flutter/widgets.dart';

class CounterWidget extends StatelessWidget {
  final Counter _counter;
  CounterWidget(this._counter);

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder<int>(
      valueListenable: _counter.counterListenable,
      builder: (context, count, _) => Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            'Current Value for counter :',style: TextStyle(fontSize: 23),
          ),
          Text(
            '$count',
            style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold),
          ),
        ],
      ),
    );
  }
}

5. Run the generation source command continuously (optional) #

 flutter pub run build_runner watch

A complete example #

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:listenable/listenable.dart';

part 'main.g.dart';

class Counter = _Counter with _Counter$Listenable;

@ListenableModel()
abstract class _Counter {
  @ListenableProperty()
  int _counter = 0;

  void increment() {
    _counter++;
  }
}

class CounterWidget extends StatelessWidget {
  final Counter _counter;

  CounterWidget(this._counter);

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder<int>(
      valueListenable: _counter.counterListenable,
      builder: (context, count, _) => Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            'Current Value for counter :',
            style: TextStyle(fontSize: 23),
          ),
          Text(
            '$count',
            style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold),
          ),
        ],
      ),
    );
  }
}

class MyApp extends StatelessWidget {
  final _counter = Counter();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage('Flutter Demo Home Page', _counter),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final Counter _counter;
  final String title;

  MyHomePage(this.title, this._counter);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Center(
        child: CounterWidget(_counter),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _counter.increment(),
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

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

Examples #

Features and bugs #

Please file feature requests and bugs at the issue tracker.

0.3.0 #

  • Documentation update.

0.2.0 #

  • Documentation update.

0.1.0 #

  • Initial version, created by Camilo Alvarez

example/README.md

Listenable Examples #

Examples of Listenable usage.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  listenable: ^0.3.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:listenable/listenable.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
13
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
57
Learn more about scoring.

We analyzed this package on Oct 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:listenable/listenable.dart.

Health suggestions

Format lib/listenable.dart.

Run dartfmt to format lib/listenable.dart.

Format lib/src/listenable_annotations.dart.

Run dartfmt to format lib/src/listenable_annotations.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
test 1.3.4