Home > database >  How to control the size of a textbutton inside listView in flutter?
How to control the size of a textbutton inside listView in flutter?

Time:11-09

I am using the following button snip:

  Widget _formTextButton(onPressed, buttonText) => TextButton(
        style: TextButton.styleFrom(
          foregroundColor: Colors.white,
          backgroundColor: Colors.blue,
          padding: const EdgeInsets.all(16.0),
          textStyle: const TextStyle(fontSize: 20),
        ),
        onPressed: onPressed,
        child: Text(buttonText),
      );

I need to be able to control the size of the button, is there any way to do that? I tried to put it inside a SizedBox but I could not get it to work!

CodePudding user response:

try this:

  Widget _formTextButton(onPressed, buttonText) => ConstrainedBox(
  constraints:BoxConstraints(
    minHeight:80, // change it to what you need
    minWidth:200, // change it to what you need
    ),
    child: TextButton(
        style: TextButton.styleFrom(
          foregroundColor: Colors.white,
          backgroundColor: Colors.blue,
          padding: const EdgeInsets.all(16.0),
          textStyle: const TextStyle(fontSize: 20),
        ),
        onPressed: onPressed,
        child: Text(buttonText),
      )
);

this will force the widget to be at minimal in that height and width

CodePudding user response:

One workaround for set width for TextButton in listView is use Align and SizedBox like this:

Widget _formTextButton(onPressed, buttonText) => Align(
  child: SizedBox(
     width: 40,
     height: 20,
     child: TextButton(
        style: TextButton.styleFrom(
          foregroundColor: Colors.white,
          backgroundColor: Colors.blue,
          padding: const EdgeInsets.all(16.0),
          textStyle: const TextStyle(fontSize: 20),
        ),
        onPressed: onPressed,
        child: Text(buttonText),
      )
   )
);

enter image description here

  • Related