dev.farmer
dev.farmer

Reputation: 2779

Flutter - how to wrap contents the row width in the ListView?

I want to draw a vertical list. Each item in list should be filled only wrapped size.

But my code draw the list items with full width like:

enter image description here

I tried to use MainAxisSize.min but it doesn't work.

class HomePage extends StatelessWidget {
  HomePage({super.key});

  final List<String> list = ["apple", "banana", "cat", "dragon"];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
          shrinkWrap: true,
          itemCount: list.length,
          itemBuilder: (context, index) {
            return Container(
              decoration:
                  BoxDecoration(border: Border.all(color: Colors.black)),
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text('[$index]'),
                  const SizedBox(width: 4),
                  Text(list[index])
                ],
              ),
            );
          }),
    );
  }
}

Upvotes: 1

Views: 1152

Answers (4)

rszf
rszf

Reputation: 201

Add Align and align left:

Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
          shrinkWrap: true,
          itemCount: list.length,
          itemBuilder: (context, index) {
            return Align(
              alignment: Alignment.centerLeft,
              child: Container(
                decoration:
                    BoxDecoration(border: Border.all(color: Colors.black)),
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text('[$index]'),
                    const SizedBox(width: 4),
                    Text(list[index])
                  ],
                ),
              ),
            );
          }),
    );
  }

Upvotes: 3

eamirho3ein
eamirho3ein

Reputation: 17890

By default ListView take all avaialble width, even if you wrap your container with Align, this just change the item's width not the width that ListView takes(if you wrap your listview with colored container you will see that).

If you are not using long list, You can use SingleChildScrollView and Column like this:

body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: list.mapIndexed((index, e) {
            return Container(
              decoration:
                  BoxDecoration(border: Border.all(color: Colors.black)),
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text('[$index]'),
                  const SizedBox(width: 4),
                  Text(e)
                ],
              ),
            );
          }).toList(),
        ),
      )

enter image description here

Upvotes: 2

pmatatias
pmatatias

Reputation: 4404

because you use ListView, the child will will fill the width.

if you want to show not much data, consider use SingleChildScrollView i wrote an article here to compare them and explain when to use it

https://medium.com/easyread/my-october-flutter-notes-2-6e0c78cf2e56

enter image description here

Upvotes: 3

w461
w461

Reputation: 2678

More of a workaround: depending on the layout seeked, either place the ListView into a Row or put another widget at the end of the row.

Upvotes: 0

Related Questions