120 lines
3.9 KiB
Dart
120 lines
3.9 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:together_mobile/models/apply_list_model.dart';
|
|
import 'package:together_mobile/request/apply.dart';
|
|
import 'package:together_mobile/utils/app_dir.dart';
|
|
import 'components/chat_tile.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> getPorfileRes =
|
|
await getMyProfile(getIt.get<UserAccount>().id);
|
|
await getIt.get<UserProfile>().init(getPorfileRes['data']);
|
|
if (getPorfileRes['data']['avatar'] != null) {
|
|
String avatarPath =
|
|
await getAvatarPath(getPorfileRes['data']['avatar']);
|
|
if (!File(avatarPath).existsSync()) {
|
|
var data = await downloadAvatar(getPorfileRes['data']['avatar']);
|
|
File file = await File(avatarPath).create(recursive: true);
|
|
file.writeAsBytes(data);
|
|
}
|
|
}
|
|
|
|
Map<String, dynamic> getApplyRes =
|
|
await getApplyList(getIt.get<UserAccount>().id);
|
|
if (getApplyRes['code'] == 10600) {
|
|
getIt.get<ApplyList>().init(getApplyRes['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: FileImage(
|
|
File(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....'),
|
|
)
|
|
],
|
|
),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
}
|
|
}
|