A flutter package to control rebuilds of a widget based on updates of a Listenable

Overview

Pub Version Flutter CI

A flutter package to control rebuilds of a widget based on updates of a Listenable.

What is Grab?

Grab is similar to ValueListenablebuiler or AnimatedBuilder, but not a widget like them. It comes with mixins and extensions of BuildContext.

If a Grab mixin is added to a widget and an extension method grab() or grabAt() is used inside it with some Listenable passed in, the widget is rebuilt whenever the Listenable (or only specific part of it) is updated.

Good for state management

What this package does is only rebuild a widget according to changes in a Listenable. Despite such simplicity, it becomes a powerful state management tool if combined with some DI package like get_it.

Grab works as long as a Listenable is available in any way when grab() or grabAt() is used, so you can use your favourite DI package to pass a Listenable around.

Motivation

The blog post below really inspired the author of this package. It shows a picture of how simple state management could be.

Combining this idea, some sort of DI and Grab, you can focus on creating a good app with no trouble understanding how to use it. This is an advantage over other packages with a larger API surface.

Supported Listenables

The Listenable can be anything that inherits a Listenable; ChangeNotifier, ValueNotifier, TextEditingController, Animation / AnimationController, ScrollController, etc.

Usage

Mixins

Add StatelessGrabMixin or StatefulGrabMixin to a widget in which you want to use extensions.

class MyWidget extends StatelessWidget with StatelessGrabMixin
class MyWidget extends StatefulWidget with StatefulGrabMixin

Each mixin has an alias.
Use Grab for StatelessGrabMixin or Grabful for StatefulGrabMixin if you like shorter names.

class MyWidget extends StatelessWidget with Grab
class MyWidget extends StatefulWidget with Grabful

Extensions

You can use grab() and grabAt() on a BuildContext.
They are like watch() and select() of provider.

Make sure to add a mixin to the StatelessWidget/StatefulWidget where these extensions are used. An error occurs otherwise.

grab()

grab() listens to the Listenable passed to it, and the widget that the BuildContext belongs to is rebuilt every time the Listenable is updated.

final notifier = ValueNotifier(0);
@override
Widget build(BuildContext context) {
  final count = context.grab<int>(counterNotifier);
  return Text('$count');
}

The return value is the Listenable itself, or its value if the Listenable is ValueListenable.

In the above example, the Listenable is a ValueNotifier extending ValueListenable, so the count returned by grab() is not the Listenable itself but the value of ValueNotifier.

grabAt()

The widget is rebuilt only when there is a change in the value returned by the selector, which is a callback function passed as the second argument.

final notifier = ValueNotifier(
  Item(name: 'Milk', quantity: 3),
);
@override
Widget build(BuildContext context) {
  final name = context.grabAt(notifier, (Item item) => item.name);
  return Text(name);
}

The selector receives the Listenable itself, or its value if the Listenable is ValueListenable.

In this example, the value of the notifier is Item that has name and quantity. The widget is not rebuilt if quantity has a new value but name is not updated.

Tip

The value returned by the selector can be anything as long as it is possible to evaluate its equality with the previous value with the == operator.

final bool isEnough = context.grabAt(
  notifier,
  (Item item) => item.quantity > 5,
);

Supposing that the quantity was 3 in the previous build, if it's changed to 2, the widget is not going to be rebuilt because isEnough remains false.

You might also like...

A widget that allow user resize the widget with drag

Flutter-Resizable-Widget A widget that allow user resize the widget with drag Note: this widget uses Getx Example bandicam.2021-11-11.12-34-41-056.mp4

Dec 13, 2022

Flutter Package: When your desired layout or animation is too complex for Columns and Rows, this widget lets you position/size/rotate/transform its child in complex ways.

Flutter Package: When your desired layout or animation is too complex for Columns and Rows, this widget lets you position/size/rotate/transform its child in complex ways.

align_positioned Widgets in this package: AlignPositioned AnimatedAlignPositioned AnimChain Why are these widgets an indispensable tool? When your des

Dec 12, 2022

The flutter_otp_text_field package for flutter is a TextField widget that allows you to display different style pin.

The flutter_otp_text_field package for flutter is a TextField widget that allows you to display different style pin.

flutter_otp_text_field flutter_otp_text_field The flutter_otp_text_field package for flutter is a TextField widget that allows you to display differen

Nov 8, 2022

An awesome Flutter package with widget extension.

jr_extension An awesome Flutter package with widget extension. Why do I want to create this lib? In SwiftUI framework created

Sep 28, 2022

A flutter package for the OTP Field widget.

A flutter package for the OTP Field widget.

OTP Text Field A flutter package to create a OTP Text Field widget in your application. Stay tuned for the latest updates: 📱 Screenshots ⚙️ Installat

Oct 30, 2022

Flutter package which helps you to implement Ticket Widget in your app.

Flutter package which helps you to implement Ticket Widget in your app.

✨ Ticket Widget Flutter package which helps you to implement Ticket Widget in your app. The source code is 100% Dart, and it is an updated null safe v

Dec 30, 2022

Code generation for Flutter Padding widgets based on your constants

Code generation for Flutter Padding widgets based on your constants

Oct 20, 2022

A Flutter sticky headers & index ListView. Based on scrollable_positioned_list.

A Flutter sticky headers & index ListView. Based on scrollable_positioned_list.

Language: English | 中文简体 azlistview A Flutter sticky headers & index ListView. Based on scrollable_positioned_list. AzListView, SuspensionView, IndexB

Jan 7, 2023

Flutter Carousel Pro - A Flutter Carousel widget

Flutter Carousel Pro - A Flutter Carousel widget

Carousel Extended A Flutter Carousel widget. Usage As simple as using any flutter Widget. Based on Carousel Pro but extended to be able to navigate be

Dec 7, 2020
Full customable rolling switch widget for flutter apps based on Pedro Massango's 'crazy-switch' widget

lite_rolling_switch Full customable rolling switch widget for flutter apps based on Pedro Massango's 'crazy-switch' widget https://github.com/pedromas

Eduardo Muñoz 48 Dec 1, 2022
⌚️ A general flutter timeline widget based on real-world application references

framework platform tags title flutter Android, iOS, Web, macOS, Linux, Windows flutter, timeline, flutter timeline, timeline tile flutter timeline flu

Universe 285 Dec 21, 2022
A simple Flutter widget to add in the widget tree when you want to show nothing, with minimal impact on performance.

nil A simple widget to add in the widget tree when you want to show nothing, with minimal impact on performance. Why? Sometimes, according to a condit

Romain Rastel 127 Dec 22, 2022
A flutter carousel widget, support infinite scroll, and custom child widget.

carousel_slider A carousel slider widget. Features Infinite scroll Custom child widgets Auto play Supported platforms Flutter Android Flutter iOS Flut

Bart T 1 Nov 25, 2021
A Flutter Widget to make interactive timeline widget.

Bubble Timeline Package A Flutter Widget to make interactive timeline widget. This widget can be used to make Event Timelines, or Timelines for certai

Vansh Goel 12 Sep 22, 2022
📸 Easy to use yet very customizable zoomable image widget for Flutter, Photo View provides a gesture sensitive zoomable widget.

?? Easy to use yet very customizable zoomable image widget for Flutter, Photo View provides a gesture sensitive zoomable widget. Photo View is largely used to show interacive images and other stuff such as SVG.

Blue Fire 1.7k Jan 7, 2023
A widget lib that the widget in this lib can react to flutter ScrollController's offset

Language: English | 中文简体 linked_scroll_widgets A lib full of widgets that can react to the scrollController's offset change,to custom your UI effect.

WenJingRui 8 Oct 16, 2022
Progress Dialog widget for flutter projects with ability to customize loading widget, background color and background blur.

DISCONTINUED Checkout ArsDialog ars_progress_dialog Customizable progress dialog for Flutter applications with smooth animation for background dim col

Arsam 8 Apr 15, 2022
A Flutter widget that will give a Glitch Animation Effect to it's child widget.

GlitchEffect A Flutter widget that will give a Glitch Animation Effect to it's child widget. Installation Add the latest version of package to your pu

Sameer Singh 6 Nov 25, 2022
Widget, that can make any static located widget hidable

Installing See the official installing guidline from hidable/install Usage & Overview To start using Hidable widget, we have to create a ScrollControl

Anon 18 Dec 16, 2022