How to format row in flutter?
for creating an Instagram like comment section UI you can check the below code. I have added a hardcoded list for display purpose you can insert your own server data with logic to differentiate between both list views. For this UI I have created a normal comment object having all replies to that user in replies list such as
single user comment object
{
'name': 'person 1',
'message': 'Some text message from person 1',
'replies': [
{
'name': 'person 2',
'message': 'Some text message from person 2',
},
{
'name': 'person 3',
'message': 'Some text message from person 3',
},
]
},
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
final List<dynamic> commentList = [
{
'name': 'person 1',
'message': 'Some text message from person 1',
'replies': [
{
'name': 'person 2',
'message': 'Some text message from person 2',
},
{
'name': 'person 3',
'message': 'Some text message from person 3',
},
]
},
{
'name': 'person 4',
'message': 'Some text message from person 4',
'replies': []
},
{
'name': 'person 5',
'message': 'Some text message from person 5',
'replies': [
{
'name': 'person 6',
'message': 'Some text message from person 6',
}
]
}
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Hello World"),),
body: ListView.builder(
padding: const EdgeInsets.all(0.0),
shrinkWrap: true,
itemCount: commentList.length,
itemBuilder: (context, index) {
return Column(children: [
ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 8.0),
title: Text(commentList[index]['name']),
leading: Icon(Icons.fiber_new),
),
ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
padding: const EdgeInsets.all(0.0),
itemCount: commentList[index]['replies'].length ?? 0,
itemBuilder: (context, i) {
return ListTile(
contentPadding:
const EdgeInsets.symmetric(horizontal: 24.0),
leading: Icon(Icons.pages),
title: Text(commentList[index]['replies'][i]['name'] ),
);
})
]);
}));
}
}
Run this cord in dartpad and refactor it according to your use.