A LayoutBuilder with an extra value

Overview

value_layout_builder

Pub

A LayoutBuilder with an extra value. It's useful when you want to build a widget with a value computed during layout.

Features

  • ValueLayoutBuilder - The RenderBox version.
  • SliverValueLayoutBuilder - The RenderSliver version.

Getting started

In the pubspec.yaml of your flutter project, add the following dependency:

dependencies:
  ...
  value_layout_builder:

In your library add the following import:

import 'package:value_layout_builder/value_layout_builder.dart';

For help getting started with Flutter, view the online documentation.

Usage

ValueLayoutBuilder and SliverValueLayoutBuilder will pair with a custom RenderObject which accepts a one of them as a child.

For example it is used in flutter_sticky_header in order to know how much the header is hidden.

The typical usage is to expose a builder in the parameter of your widget's constructor which takes a BuildContext and the value you want to get from layout, and to pass it from your custom RenderObject.

The following code is a dumb example to show you how to use it:

class _Tester<T> extends SingleChildRenderObjectWidget {
  const _Tester({
    Key? key,
    required this.value,
    required Widget child,
  }) : super(key: key, child: child);

  final T value;

  @override
  _RenderTester<T> createRenderObject(BuildContext context) {
    return _RenderTester<T>(value: value);
  }

  @override
  void updateRenderObject(
    BuildContext context,
    _RenderTester<T> renderObject,
  ) {
    renderObject.value = value;
  }
}

class _RenderTester<T> extends RenderBox
    with RenderObjectWithChildMixin<RenderBox> {
  _RenderTester({
    required T value,
  }) : _value = value;

  T get value => _value;
  T _value;
  set value(T value) {
    if (_value != value) {
      _value = value;
      markNeedsLayout();
    }
  }

  @override
  void performLayout() {
    // We create a specific constraints with the value we want to pass to the builder.
    BoxValueConstraints<T> constraints = BoxValueConstraints<T>(
      constraints: this.constraints,
      value: value,
    );
    child.layout(constraints);
    size = constraints.biggest;
  }

  @override
  void paint(PaintingContext context, Offset offset) {
    context.paintChild(child, offset);
  }
}

We can then use _Tester<T> like this:

_Tester<String>(
  value: 'test',
  child: ValueLayoutBuilder<String>(
    builder: (context, constraints) {
      // The constraints holds the String value.
      final String value = constraints.value;
      return Text(value);
    },
  ),
)

Sponsoring

I'm working on my packages on my free-time, but I don't have as much time as I would. If this package or any other package I created is helping you, please consider to sponsor me. By doing so, I will prioritize your issues or your pull-requests before the others.

Changelog

Please see the Changelog page to know what's recently changed.

Contributions

Feel free to contribute to this project.

If you find a bug or want a feature, but don't know how to fix/implement it, please fill an issue.
If you fixed a bug or implemented a feature, please send a pull request.

You might also like...

Lightweight and blazing fast key-value database written in pure Dart.

Lightweight and blazing fast key-value database written in pure Dart.

Fast, Enjoyable & Secure NoSQL Database Hive is a lightweight and blazing fast key-value database written in pure Dart. Inspired by Bitcask. Documenta

Dec 30, 2022

Immutable value types, enum classes, and serialization.

Built Values for Dart Introduction Built Value provides: Immutable value types; EnumClass, classes that behave like enums; JSON serialization. Immutab

Dec 23, 2022

A Dart package that helps to implement value based equality without needing to explicitly override == and hashCode.

A Dart package that helps to implement value based equality without needing to explicitly override == and hashCode.

Simplify Equality Comparisons Overview Being able to compare objects in Dart often involves having to override the == operator as well as hashCode. No

Jan 8, 2023

Cupertino buttons which are used as radio buttons in order to select one value

Cupertino buttons which are used as radio buttons in order to select one value

Flutter Cupertino Radio Choice Cupertino buttons which are used as radio buttons in order to select one value. Tutorial A complete tutorial how to use

Sep 18, 2022

A custom Flutter value slider that makes a wave effect when dragged.

A Flutter slider that makes a wave effect when dragged. Does a little bounce when dropped. Demo Getting Started To use this plugin, add wave_slider as

Dec 21, 2022

ITS A SIMPLE CRYPTO APP THAT GIVES OR DISPLAYS PRICES - %CHANGE AND CHANGE VALUE OF TICKER (VARIOUS CRYPTO ASSERTS)

btcpricetracker A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started

Dec 28, 2021

Get or set persistent storage value based on MMKV framework.

mmkv_flutter Plugin that allow Flutter to read value from persistent storage or save value to persistent storage based on MMKV framework Getting Start

Jan 17, 2022

Value listenable test - Assists in testing ValueListenable objects (ex: ValueNotifier).

value_listenable_test Assists in testing ValueListenable objects (ex: ValueNotifier). install Added in your pubspec.yaml as dev dependency: dev_depend

Feb 23, 2022

FTFS is a Flutter package which uses a TextField Widget to search and select a value from a list

FTFS is a Flutter package which uses a TextField Widget to search and select a value from a list

FTFS is a Flutter package which uses a TextField Widget to search and select a value from a list. It's a simple, lightweight, and fully tested package unlike other "autocomplete" or textfield search packages.

Jan 5, 2022

This package allows you to scroll/select the value directly from the dropdown with less effort and time.

Direct Select This package allows you to scroll/select the value directly from the dropdown with less effort and time. Inspired by Virgil Pana shot Sa

Nov 25, 2022

How to get the most value from Dart static analysis

This package is deprecated. Before it was deprecated, it was the way to get analysis options matching those used internally at Google. This was useful

Nov 4, 2022

Provider Demo - Simple Provider using provider update counter and apply a timer also increase and decrease that value by pressing buttons

Provider Demo - Simple Provider using provider update counter and apply a timer also increase and decrease that value by pressing buttons

state_management simple Provider using provider update counter and apply a timer

Feb 2, 2022

Hive is a lightweight and blazing fast key-value database

Hive is a lightweight and blazing fast key-value database

Hive Manager Hive is a lightweight and blazing fast key-value database Features Cross platform - mobile, desktop Performance Strong encryption built

Feb 24, 2022

Return a result ErrorOr with either a value T or an error Object.

ErrorOr Return a result ErrorOr with either a value T or an error Object. Features Always return a value ErrorOr from an async function. Let the calle

Nov 6, 2022

A Flutter increment value project with smart contract on Astar local

A Flutter increment value project with smart contract on Astar local

dapp_astar_evm_example A Flutter increment value project with smart contract on Astar local.

Oct 26, 2022

How to get the most value from Dart static analysis

This package is deprecated. Before it was deprecated, it was the way to get analysis options matching those used internally at Google. This was useful

Nov 4, 2022

Its a simple app which gives Weather Update, Bit Coin Value Comparator, and Flash Chat Application

Its a simple app which gives Weather Update, Bit Coin Value Comparator, and Flash Chat Application

Bundle_App_MajorProject Description : Its a simple app which is a bundle of Weather Update App, Bit Coin Value Comparator App, and Flash Chat Applicat

Sep 9, 2022

An rx stream builder widget that is able to pre-populate a flutter StreamBuilder with data from an rx stream if the stream is either a value or a replay observable.

An rx stream builder widget that is able to pre-populate a flutter StreamBuilder with data from an rx stream if the stream is either a value or a replay observable. For example the RX stream is a BehaviorSubject or a ReplaySubject.

Jan 22, 2022
Comments
  • Cannot build apk

    Cannot build apk

    When I ran flutter build apk for my flutter project I always got error.

    Compiler message:
    ../../../../Flutter/flutter/.pub-cache/hosted/pub.dartlang.org/value_layout_builder-0.1.0/lib/src/sliver_value_layout_builder.dart:117:5: Error: The method 'rebuildIfNecessary' isn't defined for the class '_RenderSliverValueLayoutBuilder<T>'.
     - '_RenderSliverValueLayoutBuilder' is from 'package:value_layout_builder/src/sliver_value_layout_builder.dart' ('../../../../Flutter/flutter/.pub-cache/hosted/pub.dartlang.org/value_layout_builder-0.1.0/lib/src/sliver_value_layout_builder.dart').
    Try correcting the name to the name of an existing method, or defining a method named 'rebuildIfNecessary'.
        rebuildIfNecessary();
        ^^^^^^^^^^^^^^^^^^
    ../../../../Flutter/flutter/.pub-cache/hosted/pub.dartlang.org/value_layout_builder-0.1.0/lib/src/value_layout_builder.dart:105:5: Error: The method 'rebuildIfNecessary' isn't defined for the class '_RenderValueLayoutBuilder<T>'.
     - '_RenderValueLayoutBuilder' is from 'package:value_layout_builder/src/value_layout_builder.dart' ('../../../../Flutter/flutter/.pub-cache/hosted/pub.dartlang.org/value_layout_builder-0.1.0/lib/src/value_layout_builder.dart').
    Try correcting the name to the name of an existing method, or defining a method named 'rebuildIfNecessary'.
        rebuildIfNecessary();
        ^^^^^^^^^^^^^^^^^^
    Target kernel_snapshot failed: Exception: Errors during snapshot creation: null
    build failed.
    
    FAILURE: Build failed with an exception.
    
    * Where:
    Script '/Users/xxx/Documents/Flutter/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 882
    
    * What went wrong:
    Execution failed for task ':app:compileFlutterBuildRelease'.
    > Process 'command '/Users/xxx/Documents/Flutter/flutter/bin/flutter'' finished with non-zero exit value 1
    
    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    
    * Get more help at https://help.gradle.org
    
    BUILD FAILED in 12s
    Running Gradle task 'assembleRelease'...
    Running Gradle task 'assembleRelease'... Done                      12,9s
    Gradle task assembleRelease failed with exit code 1
    
    

    It's more like error with this method rebuildIfNecessary() I'm running on 1.17.5 version of flutter

    opened by Xawery94 9
  • Null safety migration

    Null safety migration

    Hi @letsar Thanks for your work We actually use your library and we want to contribute by migrating them to null safety what do you think about it ? #2 😄

    opened by John-Dormevil 6
Owner
Romain Rastel
Flutter Developer
Romain Rastel
A fast, extra light and synchronous key-value storage to Get framework

get_storage A fast, extra light and synchronous key-value in memory, which backs up data to disk at each operation. It is written entirely in Dart and

Jonny Borges 257 Dec 21, 2022
Manually add the extra hitTest area of a widget without changing its size or layout.

extra_hittest_area Language: README.md | 中文简体 Manually add the extra hitTest area of a widget without changing its size or layout. Parent widgets The

FlutterCandies 13 Dec 6, 2022
Routinger is a task scheduler app that is made to make you a better person at no extra cost. The code is open-source. Dart language and Flutter framework are used extensively.

Routinger This is a simple app that will allow you to schedule your tasks, create a simple to-do, and also make recurring tasks. The app ends you noti

Routinger 16 Dec 17, 2022
A Funtioning basic Clock UI APP with extra functionalities such as displaying thecurrent time location being used and checking time for other timezones simultaneosly.

clock_UI A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if this

Anjola Favour Ayomikun 0 Dec 28, 2021
Image Extensions A wrapper library for image package with some extra functions.

A wrapper library for image package with some extra functions. Installation Add this to your package's pubspec.yaml file: dependencie

Vanxh 1 Jan 15, 2022
ValueNotifier, but outside Flutter and with some extra perks

Welcome to state_notifier~ This package is a recommended solution for managing state when using Provider or Riverpod. Long story short, instead of ext

Remi Rousselet 290 Dec 24, 2022
Add an extra tap region to button widgets

ExtraTapRegion A widget to add an extra tap region around the child widget. Example @override Widget build(BuildContext context) { return DeferredPo

Yusuke Otsuka 0 May 17, 2022
this repo is testing the custom painter by changing according to slider value. It was intended to be used for volume control in audio player.

custom_paint_demo Trying a widget called custom paint Getting Started This project is a starting point for a Flutter application. A few resources to g

Edy 7 Jan 16, 2022
Lightweight and blazing fast key-value database written in pure Dart.

Fast, Enjoyable & Secure NoSQL Database Hive is a lightweight and blazing fast key-value database written in pure Dart. Inspired by Bitcask. Documenta

HiveDB 3.4k Dec 30, 2022
A reactive key-value store for Flutter projects. Like shared_preferences, but with Streams.

streaming_shared_preferences A reactive key-value store for Flutter projects. streaming_shared_preferences adds reactive functionality on top of share

Iiro Krankka 244 Dec 22, 2022