Pakete, Repositories und App-Kategorien suchen

GitHub-Repositoryihorbokov/flutter_onlooker

flutter_onlooker

Eine State-Management-Bibliothek, die eine einfache Lösung zum Aktualisieren des Zustands und der Navigation bereitstellt.

Projektcover von flutter_onlooker
Sterne
2
Forks
0
Letzter Push (UTC)
11.04.2024
Projektstatus
Aktiv

Original-README

Englischer Projektschnappschuss. Aktuelle Inhalte auf GitHub.

Onlooker

Pub style: very good analysis License: MIT

A state management library that provides a simple solution for updating state and navigation.

Usage

Lets take a look at how to use flutter_onlooker. The library provides: StateNotifier, StateObserver and StateNotifierProvider classes.

  1. StateNotifier is responsible for registering and notifying states. Also it can emit navigation events. The main methods of this class:
  • observable<S> registers observable state of S type.
  • initial<S> returns initial value for state of S type.
  • latest<S> returns the latest value for state of S type.
  • contains<S> checks whether a state of S type was registered before.
  • notify<S> notifies a new state of S type.
  • navigate<T extends Object?> notifies a new navigation event.
class IncrementStateNotifier extends StateNotifier {
  IncrementStateNotifier() {
    observable<int>(initial: _counter);
  }

  var _counter = 0;

  void useCase() {
    final initialState = initial<int>();
    final latestState = latest<int>();
    final containsState = contains<int>();

    notify<int>(++_counter);
    navigate<void>('/route', arguments: _counter);
  }
}
  1. StateObserver handles building a widget in response to new states. It takes 3 parameters:
  • required builder function that takes the BuildContext and state, this function is responsible for returning a widget that is to be rendered.
  • An optional notifier that can be passed directly or with using StateNotifierProvider.
  • An optional buildWhen can be implemented for more granular control over how often StateObserver rebuilds.
StateObserver<IncrementStateNotifier, int>(
  builder: (_, state) {
    return Text(
      '$state',
      style: Theme.of(context).textTheme.headlineMedium,
    );
  },
)
  1. StateNotifierProvider takes a create function that is responsible for creating the StateNotifier, child widget that will have access to the StateNotifier instance via Provider.of<StateNotifier>(context) or context.read<StateNotifier>() and optional router function that will receive navigation events.
StateNotifierProvider<IncrementStateNotifier>(
  create: (_) => IncrementStateNotifier(),
  child: const MyHomePage(title: 'Onlooker Demo'),
  router: (context, routeName, arguments) {
    return showDialog<void>(
      context: context,
      builder: (_) => AlertDialog(
        title: Text("Route is '$routeName'"),
        content: Text("You've clicked $arguments times!"),
      ),
    );
  },
)
Ihor Bokov GitHub avatar
GitHub-Autor

Ihor Bokov ↗

Crazy Flutter Developer

Ukraine
SprachenDartSwiftKotlinObjective-C

Verwendete Abhängigkeiten

Abhängigkeiten 3 Einträge

  • flutter{"sdk":"flutter"}
  • flutter_testEntwicklung{"sdk":"flutter"}
  • very_good_analysisEntwicklung^5.1.0

Wie wähle ich ein passendes Repository?

Nach Anwendungsfall filtern, Aktivität vergleichen und vor der Nutzung das Originalprojekt prüfen.

  1. Nach Name, Autor oder Beschreibung suchen und nach Thema eingrenzen.
  2. Sterne, Forks und letzte Pushes vergleichen. Sterne zeigen Interesse, garantieren aber keine Qualität.
  3. Vor der Nutzung README, Lizenz und Issues auf GitHub prüfen.

Quellen und Pflege

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗