copy_size_widget 1.0.0
copy_size_widget: ^1.0.0 copied to clipboard
A widget that sizes itself using another widget's size.
import 'dart:math';
import 'package:copy_size_widget/copy_size_widget.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
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 _size = 200;
final copyKey = GlobalKey();
void _incrementCounter() {
setState(() {
_size = Random().nextInt(300).toDouble();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
CopySizeWidget(
copyKey: copyKey,
child: Container(
color: Colors.red,
child: const Placeholder(),
),
),
Container(
key: copyKey,
color: Colors.blue,
width: _size,
height: _size,
child: const Placeholder(),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}