disco 1.0.0+1 copy "disco: ^1.0.0+1" to clipboard
disco: ^1.0.0+1 copied to clipboard

A Flutter library bringing a new concept of scoped providers for dependency injection, which are independent of any specific state management solution.

example/lib/main.dart

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

abstract class Model extends ChangeNotifier {
  void incrementCounter();

  int get counter;
}

class ModelImplementation extends Model {
  int _counter = 0;

  @override
  int get counter => _counter;

  @override
  void incrementCounter() {
    _counter++;
    notifyListeners();
  }
}

final modelProvider = Provider<Model>((context) => ModelImplementation());

void main() {
  runApp(const MainApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Disco Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    // Provide the modelProvider to descendants
    return ProviderScope(
      providers: [modelProvider],
      // This builder gives a descendant context, only descendants can access
      // this scope
      child: Builder(
        builder: (context) {
          // retrieve the model
          final model = modelProvider.of(context);
          return Scaffold(
            body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  const Text(
                    'You have pushed the button this many times:',
                  ),
                  // Rebuilds this widget when the model changes
                  ListenableBuilder(
                    listenable: model,
                    builder: (context, child) {
                      return Text(model.counter.toString());
                    },
                  ),
                ],
              ),
            ),
            floatingActionButton: FloatingActionButton(
              // increment the counter when the button is pressed
              onPressed: model.incrementCounter,
              child: const Icon(Icons.add),
            ),
          );
        },
      ),
    );
  }
}
15
likes
160
points
27
downloads

Publisher

verified publishermariuti.com

Weekly Downloads

A Flutter library bringing a new concept of scoped providers for dependency injection, which are independent of any specific state management solution.

Homepage
Repository (GitHub)

Topics

#dependency-injection #scope #provider

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

flutter, meta

More

Packages that depend on disco