Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

text - Fix minimum width to a Widget which needs to expand in Flutter

I need to fix a minimum width to my Column Widgets. Inside each of them, I have Text Widgets which can be very short or very long. I need to fix a minimum width to them in order to have an acceptable size of Column even if the text is short. The other Column need obviously to adapt himself.

Row(children: [
  Column(
    children: [
      Container(
        constraints: BoxConstraints(minWidth: 80), // do not work
        child: Text("short text"),
      ),
    ],
  ),
  Column(
    children: [
      Container(
        constraints: BoxConstraints(minWidth: 110), // do not work
        child: RichText(
          text: TextSpan(
            text:"very very longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg text")),
        ),
      ],
    ),
  ],
)
question from:https://stackoverflow.com/questions/65850491/fix-minimum-width-to-a-widget-which-needs-to-expand-in-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There's probably a dozen ways to do what you want. And likely none of them straightforward or easy to understand. (The subject of constraints & sizes is quite complicated. See this constraints page for more examples & explanations.)

Here's one potential solution.

This will set a minimum width for the blue column (based on stepWidth), but will expand/grow if the text (child) inside wants to.

The yellow column will resize to accommodate the blue column.

class ExpandedRowPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Expanded Row Page'),
      ),
      body: SafeArea(
        child: Center(
            child: Row(
              children: [
                IntrinsicWidth(
                  stepWidth: 100,
// BLUE Column
                  child: Container(
                    color: Colors.lightBlueAccent,
                      child: Column(
                        children: [
                          //Text('Short'),
                          Text('shrt')
                        ],
                      )
                  ),
                ),
// YELLOW Column
                Flexible(
                  child: Container(
                    alignment: Alignment.center,
                    color: Colors.yellow,
                      child: Column(
                        children: [
                          Text('Very lonnnnnnnnnnnnnnnnnnnnnnnnnnnng texttttttttttttt'),
                        ],
                      )
                  ),
                )
              ],
            )
        ),
      ),
    );
  }
}

You could do the above without a Flexible yellow column, but a very long text child would cause an Overflow warning without a Flexible or Expanded wrapping widget.

A Row widget by itself has an infinite width constraint. So if a child wants to be bigger than screen width, it can, and will cause an overflow. (Try removing Flexible above and rebuild to see.)

Flexible and Expanded, used only inside Row & Column (or Flex, their superclass), checks screen width and other widgets inside a Row, and provides its children with a defined constraint size instead of infinite. Children (inside Flexible/Expanded) can now look up to parent for a constraint and size themselves accordingly.

A Text widget for example, will wrap its text when it's too wide for constraints given by Flexible/Expanded.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...