Reputation: 53
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:
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
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
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