intrinsic_dimension

v0.3.1

A widget that offers the dimensions (width and height) and the start offset of the widget you return from a builder callback function.

Package archive: https://pubdev.letsnova.ru/api/archives/intrinsic_dimension/0.3.1.tar.gz

Installdart pub add intrinsic_dimension

Readme

IntrinsicDimension

A widget that expose a builder to obtain the dimensions of the widget returned from the builder.

coverage

Motivation

There are some situations when we want to know the dimensions of a widget to make an operation or to create another widget based on those dimensions. However, we cannot know the dimensions of a widget because the widget could not have an specific dimension.

For example, imagine you have a Text widget and you want to know the height of the widget to draw a vertical line of the same height. How can we know the height of the widget if we don't know the fontSize and the interline space?

In this case, we need to draw the widget and then, from the first frame, get the render box of the widget which contains the dimensions to be able to operate with those dimensions on the next frames.

Usage

Return the widget you want to know its dimensions from the builder function of the IntrinsicDimension widget.

main.dart

void main() => runApp(MyApp());
                
                class MyApp extends StatelessWidget {
                  @override
                  Widget build(BuildContext context) {
                    return MaterialApp(
                            title: 'Intrinsic Dimension Example',
                            debugShowCheckedModeBanner: false,
                            home: Scaffold(
                                appBar: AppBar(
                                title: const Text('Intrinsic Dimension Example'),
                                ),
                                body: IntrinsicDimension(
                                    listener: (context, width, height, startOffset) {
                                        print('WIDTH: $width');
                                        print('HEIGHT: $height');
                                        print('OFFSET: $startOffset');
                                    }
                                    builder: (context, width, height, startOffset) {
                                        // use the height to draw the vertical line
                                        // in the second frame
                                        return Row(
                                                children: [
                                                Container(
                                                    width: 2,
                                                    height: height,
                                                    color: Colors.black,
                                                ),
                                                const SizedBox(width: 8),
                                                Column(
                                                    crossAxisAlignment: CrossAxisAlignment.start,
                                                    mainAxisSize: MainAxisSize.min,
                                                    children: [
                                                        const Text('Vertical line'),
                                                        Text('Offset: $startOffset'),
                                                    ],
                                                ),
                                            ],
                                        );
                                    },
                                ),
                            ),
                        );
                    }
                }
                

Gallery

Dart Versions

  • Dart 2: >= 2.12

Author

Changelog

0.3.1

Achieve 100% test coverage.

0.3.0

Add the rebuild boolean property to allow the widget to be rebuilt on every frame. Useful for rebuilding the widget by changing the font size from the OS settings and consequently the size and position of the widget change.

0.2.1

Remove the null-aware operator from widget binding instance because it is unnecessary since Flutter 3.0.0.

0.2.0

Expose a listener function to offer width, height, and start offset to execute an action without interfering with the widget rebuild. Ideal for connecting widgets.

0.1.1

Fix gallery image provider from documentation.

0.1.0

First version.

  • Expose a builder function to offer width, height, and start offset of the returned widget.