lite_ref 0.8.1 copy "lite_ref: ^0.8.1" to clipboard
lite_ref: ^0.8.1 copied to clipboard

A lightweight dependency injection package with support for overriding for testing.

example/README.md

source code with tests: examples/counter/lib/main.dart

import 'package:flutter/material.dart';
import 'package:lite_ref/lite_ref.dart';
import 'package:state_beacon/state_beacon.dart';

class Controller {
  late final _count = Beacon.writable(0);

  // we expose it as a readable beacon
  // so it cannot be changed from outside the controller.
  ReadableBeacon<int> get count => _count;

  void increment() => _count.value++;
  void decrement() => _count.value--;
}

final countControllerRef = Ref.scoped((ctx) => Controller());

void main() {
  runApp(LiteRefScope(child: const MyApp()));
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Lite Ref and State Beacon Counter'),
          centerTitle: true,
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        ),
        body: const Center(child: CounterText()),
        floatingActionButton: const Buttons(),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final controller = countControllerRef.of(context);
    final count = controller.count.watch(context);
    final theme = Theme.of(context);
    return Text('$count', style: theme.textTheme.displayLarge);
  }
}

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

  @override
  Widget build(BuildContext context) {
    final controller = countControllerRef.of(context);
    return Column(
      mainAxisAlignment: MainAxisAlignment.end,
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        FloatingActionButton(
          onPressed: controller.increment,
          child: const Icon(Icons.add),
        ),
        const SizedBox(height: 8),
        FloatingActionButton(
          onPressed: controller.decrement,
          child: const Icon(Icons.remove),
        ),
      ],
    );
  }
}
14
likes
150
pub points
72%
popularity

Publisher

verified publishernosy.dev

A lightweight dependency injection package with support for overriding for testing.

Repository (GitHub)
View/report issues

Topics

#dependency-injection #provider #di

Documentation

API reference

License

MIT (LICENSE)

Dependencies

basic_interfaces, flutter, lite_ref_core

More

Packages that depend on lite_ref