get_it_listenable_widget 0.1.0 copy "get_it_listenable_widget: ^0.1.0" to clipboard
get_it_listenable_widget: ^0.1.0 copied to clipboard

A ListenableWidget that resolves its ViewModel from GetIt, with optional factory params.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:get_it_listenable_widget/get_it_listenable_widget.dart';

final getIt = GetIt.instance;

/// A ViewModel that takes an initial value as a factory param.
class CounterViewModel extends ViewModel {
  CounterViewModel(this.count);

  int count;

  void increment() {
    count++;
    notifyListeners();
  }
}

void main() {
  // P1 = int (initial value), P2 = void (unused).
  getIt.registerFactoryParam<CounterViewModel, int, void>(
    (initial, _) => CounterViewModel(initial),
  );

  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'get_it_listenable_widget',
      theme: ThemeData(colorSchemeSeed: Colors.indigo, useMaterial3: true),
      home: Scaffold(
        appBar: AppBar(title: const Text('GetIt + ListenableWidget')),
        body: const Center(child: CounterPage(initial: 10)),
      ),
    );
  }
}

/// Resolves [CounterViewModel] from GetIt, passing [initial] as `param1`.
/// `autoDispose` defaults to `true`, which is correct here because the VM is a
/// fresh factory instance owned by this widget.
class CounterPage extends GetItListenableWidget<CounterViewModel, int, void> {
  const CounterPage({required int initial, super.key}) : super(param1: initial);

  @override
  Widget build(BuildContext context, CounterViewModel vm) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Text('${vm.count}', style: Theme.of(context).textTheme.displayMedium),
        const SizedBox(height: 16),
        FilledButton(onPressed: vm.increment, child: const Text('Increment')),
      ],
    );
  }
}
1
likes
160
points
23
downloads

Documentation

API reference

Publisher

verified publisherveenstra.dev

Weekly Downloads

A ListenableWidget that resolves its ViewModel from GetIt, with optional factory params.

Homepage
Repository (GitHub)
View/report issues

License

BSD-3-Clause (license)

Dependencies

flutter, get_it, listenable_widget

More

Packages that depend on get_it_listenable_widget