widget_size 1.0.0 widget_size: ^1.0.0 copied to clipboard
A widget to calculate it's size after being built and attached to a widget tree
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;
}