Davidbeh
Davidbeh

Reputation: 53

Size Widgets in Rows/Columns by size of other Widgets in that Row/Column

I want to size a Widget inside a Row/Column with the minimum size of another widget of the same Row/Column

example:

class Example extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Align(
      alignment: Alignment.topCenter,
      child: FittedBox(
        child: Column(
          children: [
            LongWidget(),
            FlatButton(child: Text("flat button"),
              color: Colors.red,
              onPressed: () {},
            ),
          ],
        ),
      ),
    );
  }
}

it look currently like this:

enter image description here

i want to size the button to the size of the long widget. I tried Expanded and CrossAxisAlignment.Stretch but both break the column

Upvotes: 0

Views: 53

Answers (1)

chunhunghan
chunhunghan

Reputation: 54367

You can copy paste run full code below
You can use IntrinsicWidth and set FlatButton's minWidth: double.infinity
code snippet

return Align(
      alignment: Alignment.topCenter,
      child: IntrinsicWidth(
        child: Column(
          children: [
            Text("long" * 12),
            FlatButton(
              minWidth: double.infinity,
              child: Text("flat button"),
              color: Colors.red,
              onPressed: () {},
            ),
          ],
        ),
      ),
    );

working demo

enter image description here

full code

import 'package:flutter/material.dart';

class Example extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Align(
      alignment: Alignment.topCenter,
      child: IntrinsicWidth(
        child: Column(
          children: [
            Text("long" * 12),
            FlatButton(
              minWidth: double.infinity,
              child: Text("flat button"),
              color: Colors.red,
              onPressed: () {},
            ),
          ],
        ),
      ),
    );
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(        
        primarySwatch: Colors.blue,        
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: "test",),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {    
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {    
    return Scaffold(
      appBar: AppBar(        
        title: Text(widget.title),
      ),
      body: Center(        
        child: Column(       
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Example(),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), 
    );
  }
}

Upvotes: 1

Related Questions