Хорошо, это потому, что потомки ListView
всегда растягиваются, я внес некоторые изменения в ваш код, сначала используйте виджет Align
для выравнивания влево или вправо, в зависимости от индекса, и оберните ваш Container
с помощью UnconstrainedBox
чтобы избежать заполнения ширины.
ListView.builder(
padding: EdgeInsets.only(top: 8.0, left: 15.0, right: 15.0),
itemCount: messages.length,
itemBuilder: (context, index) {
return Align(
alignment:
index % 2 == 0 ? Alignment.centerLeft : Alignment.centerRight,
child: UnconstrainedBox(
child: Container(
padding: EdgeInsets.all(20.0),
decoration: index % 2 == 0
? BoxDecoration(
border: Border.all(
color: Colors.grey,
),
borderRadius: BorderRadius.all(
Radius.circular(30.0),
),
)
: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.all(
Radius.circular(30.0),
),
),
child: Text(
messages[index].text,
style: TextStyle(color: Colors.black),
),
),
),
);
},
);