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 createState() => _ChatScreenState(); } class _ChatScreenState extends State { Future _getChatData() async { if (!getIt.get().isInitialised) { Map porfileRes = await getMyProfile(getIt.get().id); await getIt.get().init(porfileRes['data']); Map applyRes = await getApplyList(getIt.get().id); if (applyRes['code'] == 10600) { getIt.get().init(applyRes['data']); } Map contactRes = await getContact(getIt.get().id); if (contactRes['code'] == 10700) { getIt.get().init(contactRes['data']); } Map contactAccountProfilesRes = await getContactAccountProfiles( getIt.get().friends.keys.toList()); if (contactAccountProfilesRes['code'] == 10700) { getIt .get() .init(contactAccountProfilesRes['data']); } } return Future(() => true); } @override Widget build(BuildContext context) { return FutureBuilder( future: _getChatData(), builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.hasData) { return Scaffold( appBar: AppBar( leading: getIt.get().avatar.isEmpty ? const CircleAvatar( backgroundImage: AssetImage('assets/images/user_2.png'), ) : CircleAvatar( backgroundImage: CachedNetworkImageProvider( '$avatarsUrl/${getIt.get().avatar}', ), ), title: Text(getIt.get().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....'), ) ], ), ); } }, ); } }