together_mobile/lib/screens/chat/chat_screen.dart

130 lines
4.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'components/chat_tile.dart';
import 'package:together_mobile/models/contact_model.dart';
import 'package:together_mobile/models/apply_list_model.dart';
import 'package:together_mobile/request/apply.dart';
import 'package:together_mobile/request/server.dart';
import 'package:together_mobile/request/contact.dart';
import 'package:together_mobile/models/user_model.dart';
import 'package:together_mobile/models/init_get_it.dart';
import 'package:together_mobile/request/user_profile.dart';
class ChatScreen extends StatefulWidget {
const ChatScreen({super.key});
@override
State<ChatScreen> createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
Future<bool> _getChatData() async {
if (!getIt.get<UserProfile>().isInitialised) {
Map<String, dynamic> porfileRes =
await getMyProfile(getIt.get<UserAccount>().id);
await getIt.get<UserProfile>().init(porfileRes['data']);
Map<String, dynamic> applyRes =
await getApplyList(getIt.get<UserAccount>().id);
if (applyRes['code'] == 10600) {
getIt.get<ApplyList>().init(applyRes['data']);
}
Map<String, dynamic> contactRes =
await getContact(getIt.get<UserAccount>().id);
if (contactRes['code'] == 10700) {
getIt.get<Contact>().init(contactRes['data']);
}
Map<String, dynamic> contactAccountProfilesRes =
await getContactAccountProfiles(
getIt.get<Contact>().friends.keys.toList());
if (contactAccountProfilesRes['code'] == 10700) {
getIt
.get<ContactAccountProfile>()
.init(contactAccountProfilesRes['data']);
}
}
return Future(() => true);
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _getChatData(),
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (snapshot.hasData) {
return Scaffold(
appBar: AppBar(
leading: getIt.get<UserProfile>().avatar.isEmpty
? const CircleAvatar(
backgroundImage: AssetImage('assets/images/user_2.png'),
)
: CircleAvatar(
backgroundImage: CachedNetworkImageProvider(
'$avatarsUrl/${getIt.get<UserProfile>().avatar}',
),
),
title: Text(getIt.get<UserProfile>().nickname),
centerTitle: true,
actions: [
IconButton(
onPressed: () {},
splashRadius: 20,
icon: const Icon(Icons.search),
),
IconButton(
onPressed: () {},
splashRadius: 20,
icon: const Icon(Icons.add),
),
],
),
body: SafeArea(
// Use ListView.builder because it renders list element on demand
child: RefreshIndicator(
onRefresh: () async {
return Future.delayed(const Duration(
seconds: 2,
));
},
child: ListView.builder(
physics: const BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics(),
),
itemCount: 15,
itemBuilder: (BuildContext context, int index) {
return const ChatTile();
},
),
),
),
);
} else {
return const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 60,
height: 60,
child: CircularProgressIndicator(),
),
Padding(
padding: EdgeInsets.only(top: 20),
child: Text('Loading data....'),
)
],
),
);
}
},
);
}
}