together_mobile/lib/screens/more/more_screen.dart

218 lines
7.3 KiB
Dart

import 'package:cached_network_image/cached_network_image.dart';
import 'package:cherry_toast/cherry_toast.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:together_mobile/common/constants.dart';
import 'package:together_mobile/models/init_get_it.dart';
import 'package:together_mobile/models/user_model.dart';
import 'package:together_mobile/request/server.dart';
import 'package:together_mobile/request/user_profile.dart';
class MoreScreen extends StatefulWidget {
const MoreScreen({super.key});
@override
State<MoreScreen> createState() => _MoreScreenState();
}
class _MoreScreenState extends State<MoreScreen> {
final TextEditingController _controller = TextEditingController();
final String _initStatus = getIt.get<UserProfile>().status;
bool _isChanged = false;
@override
void initState() {
super.initState();
_controller.text = _initStatus;
}
@override
void dispose() {
super.dispose();
_controller.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
InkWell(
onTap: () {
context.pushNamed('MainProfile');
},
child: Padding(
padding: const EdgeInsets.all(
10,
),
child: Row(
children: [
SizedBox(
width: 90,
height: 90,
child: getIt.get<UserProfile>().avatar.isEmpty
? const CircleAvatar(
backgroundImage:
AssetImage('assets/images/user_2.png'),
)
: CircleAvatar(
backgroundImage: CachedNetworkImageProvider(
'$userAvatarsUrl/${getIt.get<UserProfile>().avatar}',
),
),
),
const SizedBox(
width: 15,
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
getIt.get<UserProfile>().nickname,
overflow: TextOverflow.ellipsis,
style: const TextStyle(fontSize: 20),
),
const SizedBox(height: 4,),
Text(
'用户名: ${getIt.get<UserAccount>().username}',
overflow: TextOverflow.ellipsis,
style: const TextStyle(
color: kUnActivatedColor,
fontSize: 14,
),
),
const SizedBox(height: 4,),
if (getIt.get<UserProfile>().status.isNotEmpty)
Container(
padding: const EdgeInsets.all(2.4),
decoration: BoxDecoration(
border: Border.all(
width: 1.0,
color: kUnAvailableColor,
),
borderRadius: BorderRadius.circular(8.0),
),
child: Text(
getIt.get<UserProfile>().status,
style: const TextStyle(
color: kContentColorLight,
fontSize: 14,
),
),
),
],
),
),
const SizedBox(
width: 15,
),
const Icon(Icons.keyboard_arrow_right),
],
),
),
),
const SizedBox(
height: 10,
),
ListTile(
onTap: () {
context.goNamed('Setting');
},
contentPadding: const EdgeInsets.all(10),
visualDensity: VisualDensity.compact,
leading: const Icon(Icons.settings),
title: const Text(
'设置',
style: TextStyle(fontSize: 18),
),
trailing: const Icon(Icons.keyboard_arrow_right),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
int? choose = await showDialog<int>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Center(
child: Text('设置个状态吧'),
),
content: Container(
width: double.maxFinite,
padding: const EdgeInsets.symmetric(horizontal: 10),
child: TextField(
onChanged: (String newValue) {
if (newValue != _initStatus) {
setState(() {
_isChanged = true;
});
} else {
setState(() {
_isChanged = false;
});
}
},
style: const TextStyle(fontSize: 18),
controller: _controller,
textAlignVertical: TextAlignVertical.center,
decoration: const InputDecoration(
isCollapsed: true,
prefixIcon: Icon(Icons.face),
hintText: '输入你的状态',
),
),
),
actions: [
TextButton(
onPressed: () {
if (_isChanged) {
context.pop<int>(1);
}
},
child: const Text(
'确定',
style: TextStyle(
color: kPrimaryColor,
),
),
),
TextButton(
onPressed: () {
context.pop<int>(0);
},
child: const Text(
'取消',
style: TextStyle(color: Colors.black),
),
),
],
);
},
);
if (choose == 1) {
final res = await changeStatus(
getIt.get<UserAccount>().id,
_controller.text,
);
if (res['code'] == 10300) {
CherryToast.success(
title: const Text('设置状态成功!'),
).show(context);
getIt.get<UserProfile>().updateStatus(_controller.text);
}
}
},
backgroundColor: kSecondaryColor,
tooltip: '设置个状态吧!',
child: const Icon(Icons.face),
),
);
}
}