Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter Listview -> Container 'width' not working?

Tags:

flutter

flutter flutter Listview -> Container 'width' not working ?

/example/ ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

scaffold 
body : ListView
children : [
    Container (
        width: 100, // Not Working 
        height: 100, // Ok
        color: Colors.red.
    ),
]

!!! but !!!

scaffold 
body : ListView
children : [
    Stack(
      children : [
        Container (
          width: 100, // OK
          height: 100, // Ok
          color: Colors.red.
        ),
    ]

why ??

enter image description here

like image 419
덕상상 Avatar asked Aug 25 '26 14:08

덕상상


2 Answers

Try below code, Wrap your Container inside UnconstrainedBox()

Refer UnconstrainedBox

Refer Layout Constraints

ListView(
      shrinkWrap: true,
      children: [
        UnconstrainedBox(
          child: Container(
            width: 100, // OK
            height: 100, // OK
            color: Colors.red,
          ),
        )
      ],
    ),

Your result-> Image

like image 90
Ravindra S. Patil Avatar answered Aug 27 '26 03:08

Ravindra S. Patil


Below Code Also will work. we can also use Align Widget.

ListView(
    shrinkWrap: true,
    children: [
      Align(
        alignment: Alignment.topLeft,
        child: Container(
          width: 100,
          height: 100,
          color: Colors.red,
        ),
      )
    ],
  )

Also check how constrain works in flutter.https://docs.flutter.dev/development/ui/layout/constraints

like image 25
Aftab Bhadki Avatar answered Aug 27 '26 04:08

Aftab Bhadki