My card is taking the whole screen width, while being inside of a SizedBox
of width 50 (much smaller than the screen). What’s going on? Here’s my code:
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ListView(
children: const [
SizedBox(
width: 50,
child: Card(
child: Row(
children: [
Text('test'),
]),
),
),
]
);
}
}
2
Answers
You can wrap the child of
ListView
withUnconstrainedBox
Widget.From Official Docs:
UnconstrainedBox
imposes no constraints on its child, allowing it to render at its "natural" size. LinkThe Listview widget completely controls their children width and height, you can solve this issue using UnconstrainedBox widget before your widget
there are two more solutions 1- using row 2- use stack before your widget , you can check this link for more information.