11import 'package:flutter/material.dart' ;
22import '../api_service.dart' ;
33import 'ask_page.dart' ;
4+ import 'login_page.dart' ;
45
56class HomePage extends StatefulWidget {
6- const HomePage ({super .key});
7+ final String username;
8+ const HomePage ({super .key, required this .username});
79
810 @override
911 State <HomePage > createState () => _HomePageState ();
@@ -12,6 +14,7 @@ class HomePage extends StatefulWidget {
1214class _HomePageState extends State <HomePage > {
1315 final ApiService api = ApiService ();
1416 List inquiries = [];
17+ List answers = [];
1518
1619 @override
1720 void initState () {
@@ -20,12 +23,18 @@ class _HomePageState extends State<HomePage> {
2023 }
2124
2225 Future <void > loadData () async {
23- List data = await api.getInquiries ();
26+ List inqData = await api.getInquiries ();
27+ List ansData = await api.getAnswers ();
2428 setState (() {
25- inquiries = data;
29+ inquiries = inqData;
30+ answers = ansData;
2631 });
2732 }
2833
34+ List getAnswersForInquiry (int inquiryId) {
35+ return answers.where ((a) => a['inquiry_id' ] == inquiryId).toList ();
36+ }
37+
2938 @override
3039 Widget build (BuildContext context) {
3140 return Scaffold (
@@ -36,18 +45,21 @@ class _HomePageState extends State<HomePage> {
3645 child: ListView (
3746 padding: EdgeInsets .zero,
3847 children: [
39- const DrawerHeader (
40- decoration: BoxDecoration (color: Colors .deepPurple),
48+ DrawerHeader (
49+ decoration: const BoxDecoration (color: Colors .deepPurple),
4150 child: Column (
4251 crossAxisAlignment: CrossAxisAlignment .start,
4352 mainAxisAlignment: MainAxisAlignment .end,
4453 children: [
45- CircleAvatar (
54+ const CircleAvatar (
4655 radius: 30 ,
4756 child: Icon (Icons .person, size: 30 ),
4857 ),
49- SizedBox (height: 8 ),
50- Text ('User Name' , style: TextStyle (color: Colors .white, fontSize: 16 )),
58+ const SizedBox (height: 8 ),
59+ Text (
60+ widget.username,
61+ style: const TextStyle (color: Colors .white, fontSize: 16 ),
62+ ),
5163 ],
5264 ),
5365 ),
@@ -71,7 +83,11 @@ class _HomePageState extends State<HomePage> {
7183 leading: const Icon (Icons .logout),
7284 title: const Text ('Sign out' ),
7385 onTap: () {
74- Navigator .popUntil (context, (route) => route.isFirst);
86+ Navigator .pushAndRemoveUntil (
87+ context,
88+ MaterialPageRoute (builder: (context) => const LoginPage ()),
89+ (route) => false ,
90+ );
7591 },
7692 ),
7793 ],
@@ -81,10 +97,40 @@ class _HomePageState extends State<HomePage> {
8197 itemCount: inquiries.length,
8298 itemBuilder: (context, index) {
8399 final item = inquiries[index];
84- return ListTile (
85- leading: const CircleAvatar (child: Icon (Icons .person)),
86- title: Text (item['title' ] ?? 'No title' ),
87- subtitle: Text (item['body' ] ?? '' ),
100+ final inquiryAnswers = getAnswersForInquiry (item['inquiry_id' ]);
101+ return Card (
102+ margin: const EdgeInsets .symmetric (horizontal: 8 , vertical: 4 ),
103+ child: Padding (
104+ padding: const EdgeInsets .all (8.0 ),
105+ child: Column (
106+ crossAxisAlignment: CrossAxisAlignment .start,
107+ children: [
108+ Row (
109+ children: [
110+ const CircleAvatar (
111+ radius: 16 ,
112+ child: Icon (Icons .person, size: 16 ),
113+ ),
114+ const SizedBox (width: 8 ),
115+ Text (
116+ item['title' ] ?? 'No title' ,
117+ style: const TextStyle (fontWeight: FontWeight .bold),
118+ ),
119+ ],
120+ ),
121+ const SizedBox (height: 4 ),
122+ Text (item['body' ] ?? '' ),
123+ if (inquiryAnswers.isNotEmpty) ...[
124+ const Divider (),
125+ const Text ('Answers:' , style: TextStyle (fontWeight: FontWeight .bold, fontSize: 12 )),
126+ ...inquiryAnswers.map ((answer) => Padding (
127+ padding: const EdgeInsets .only (top: 4 , left: 8 ),
128+ child: Text ('• ${answer ['body' ]}' ),
129+ )),
130+ ],
131+ ],
132+ ),
133+ ),
88134 );
89135 },
90136 ),
0 commit comments