0

I need to get and store the width of a standard flat button once it is created, how can I do it?

double _width; //store here the flatbutton's width created below

Widget _flatButtonStoreWidth (String text){

  return FlatButton(
    child: Text(text),
    onPressed: () {},
  );
}
rafitajaen
  • 399
  • 6
  • 15

1 Answers1

0

you can use the LayoutBuilder widget, here is a demo from https://stackoverflow.com/a/41558369/4465386.

return FlatButton(
  child: new LayoutBuilder(
    builder: (BuildContext context, BoxConstraints constraints) {
      setState(() => _width = constraints.maxWidth);
        return new Text('this is a button');
    }
  ),
);
Zvi Karp
  • 3,621
  • 3
  • 25
  • 40
  • I don't want to set its width, i want to get its width once flat button is created. Thanks! :) – rafitajaen Jan 07 '20 at 10:15
  • I implemented that in a clean project of Flutter to test it, only with a Scaffold and Center Widget, and it thowed this Exception: The following assertion was thrown building LayoutBuilder: setState() or markNeedsBuild() called during build. This MyHomePage widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. – rafitajaen Jan 07 '20 at 11:04