So as the question suggests I have a button.
And I want to create a Container that has the exact size of the button. Is there a way to achieve this behaviour?
CodePudding user response:
To find the current size of the button, you can open the flutter inspector and just view the button size in your IDE!
Hope this helps
CodePudding user response:
Just put that button Inside of a container, and copy the height and the width for the new Container
CodePudding user response:
You can achieve that using the key
property bound to your button and reading its height from that key and setting it to Container
height on WidgetsBinding.instance.addPostFrameCallback
like so:
class _TestState extends State<Test> {
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
buttonHeight = buttonKey.currentContext?.size?.height ?? 0;
});
});
super.initState();
}
final buttonKey = GlobalKey();
double buttonHeight = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
ElevatedButton(
key: buttonKey,
onPressed: () {},
child: Text('button'),
),
Container(
height: buttonHeight,
color: Colors.blue,
alignment: Alignment.center,
child: Text(
'button height container',
),
),
],
),
),
);
}
}