You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pharmacy/lib/chatview.dart

202 lines
7.0 KiB

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:healthcare_pharmacy/models/chatconversation_model.dart';
import 'package:healthcare_pharmacy/settings.dart';
import 'ChatMessage.dart';
class ChatUIconvoid extends StatefulWidget {
const ChatUIconvoid({super.key});
@override
State<ChatUIconvoid> createState() => _ChatUIconvoidState();
}
class _ChatUIconvoidState extends State<ChatUIconvoid> {
TextEditingController _messageController = TextEditingController();
bool isLoading=false;
List<GetCoversatitonIdModel> chatCoversatitonIdList = [];
List<ChatMessage> messages = [
ChatMessage(message: "Hello", type: "receiver"),
ChatMessage(message: "How have you?", type: "receiver"),
ChatMessage(
message: "I am doing fine.How have you?",
type: "sender"),
];
@override
void initState() {
// TODO: implement initState
isLoading=true;
getConversasionId();
super.initState();
}
Future<void> getConversasionId() async {
isLoading = true;
try {
var response = await AppSettings.getChatId();
setState(() {
chatCoversatitonIdList =
((jsonDecode(response)['newConversation']) as List).map((dynamic model) {
return GetCoversatitonIdModel.fromJson(model);
}).toList();
// Extracting the conversation_id from the response
String conversationId =
jsonDecode(response)['newConversation']['conversation_id'];
// Use the conversationId as needed in your code
print('Conversation ID: $conversationId');
isLoading = false;
isLoading = false;
});
} catch (e) {
setState(() {
isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
automaticallyImplyLeading: false,
backgroundColor: Colors.white,
flexibleSpace: SafeArea(
child: Container(
padding: EdgeInsets.only(right: 16),
child: Row(
children: <Widget>[
IconButton(
onPressed: (){
Navigator.pop(context);
},
icon: Icon(Icons.arrow_back,color: Colors.black,),
),
SizedBox(width: 2,),
CircleAvatar(
backgroundImage: NetworkImage("https://www.shutterstock.com/image-photo/pharmacist-holding-medicine-box-capsule-260nw-717437125.jpg"),
maxRadius: 20,
),
SizedBox(width: 12,),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Arminta pharma",style: TextStyle( fontSize: 16 ,fontWeight: FontWeight.w600),),
// SizedBox(height: 6,),
// Text("Online",style: TextStyle(color: Colors.grey.shade600, fontSize: 13),),
],
),
),
Icon(Icons.settings,color: Colors.black54,),
],
),
),
),
),
body: Stack(
children: <Widget>[
ListView.builder(
itemCount: messages.length,
shrinkWrap: true,
padding: EdgeInsets.only(top: 10,bottom: 60),
itemBuilder: (context, index){
return Container(
padding: EdgeInsets.only(left: 14,right: 14,top: 10,bottom: 10),
child: Align(
alignment: (messages[index].type == "receiver"?Alignment.topLeft:Alignment.topRight),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: (messages[index].type == "receiver"?Colors.grey.shade200:Colors.blue[200]),
),
padding: EdgeInsets.symmetric(horizontal: 16,vertical: 10),
child: Text(messages[index].message, style: TextStyle(fontSize: 15),),
),
),
);
},
),
Align(
alignment: Alignment.bottomLeft,
child: Container(
padding: EdgeInsets.only(left: 10, bottom: 10, top: 10),
height: 60,
width: double.infinity,
color: Colors.white,
child: Row(
children: <Widget>[
GestureDetector(
onTap: () {},
child: Container(
height: 30,
width: 30,
decoration: BoxDecoration(
color: Colors.lightBlue,
borderRadius: BorderRadius.circular(30),
),
child: Icon(
Icons.add,
color: Colors.white,
size: 20,
),
),
),
SizedBox(
width: 15,
),
Expanded(
child: TextField(
controller: _messageController,
decoration: InputDecoration(
hintText: "Write message...",
hintStyle: TextStyle(color: Colors.black54),
border: InputBorder.none),
),
),
SizedBox(
width: 15,
),
FloatingActionButton(
onPressed: () {
String newMessage = _messageController.text;
if (newMessage.isNotEmpty) {
// Add the new message to the list
setState(() {
messages.add(
ChatMessage(
message: newMessage,
type: "sender",
),
);
});
// Clear the text field
_messageController.clear();
}
},
child: Icon(
Icons.send,
color: Colors.white,
size: 18,
),
backgroundColor: Colors.blue,
elevation: 0,
),
],
),
),
),
],
));
}
}