To use this plugin, add widget_size as a dependency in your pubspec.yaml file.
dependencies:
widget_size: ^lastVersion
double _yourHeight = 50;
double _yourWidth = 50;
WidgetSize(
onChange: (Size size) {
// your Widget size available here
_yourHeight = size.height;
_yourWidth = size.width;
},
child: Container(
height: _yourHeight,
width: _yourWidth,
color: Theme.of(context).primaryColor,
),
)
Run this command:
With Flutter:
$ flutter pub add widget_size
This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get
):
dependencies:
widget_size: ^1.0.0
Alternatively, your editor might support flutter pub get
. Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:widget_size/widget_size.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;
}
Download details:
Author: buildtoapp.com
Source: https://github.com/javaherisaber/widget_size