widget_size 1.0.0 copy "widget_size: ^1.0.0" to clipboard
widget_size: ^1.0.0 copied to clipboard

A widget to calculate it's size after being built and attached to a widget tree

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:widget_size/widget_size.dart';

void main() {
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'WidgetSize',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'WidgetSize Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  double _counter = 1;
  double _size = 50;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Your widget size is = $_widgetSize',
            ),
            const SizedBox(height: 50.0),
            WidgetSize(
              onChange: (Size size) {
                _size = size.height;
              },
              child: Container(
                height: _widgetSize,
                width: _widgetSize,
                color: Theme.of(context).primaryColor,
              ),
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  double get _widgetSize => (_counter * 10) + _size;
}
33
likes
160
points
2.06k
downloads

Publisher

verified publisherbuildtoapp.com

Weekly Downloads

A widget to calculate it's size after being built and attached to a widget tree

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter

More

Packages that depend on widget_size