import 'dart:io'; 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/contact_model.dart'; import 'package:together_mobile/models/init_get_it.dart'; import 'package:together_mobile/models/user_model.dart'; import 'package:together_mobile/request/search_new.dart'; import 'package:together_mobile/request/user_profile.dart'; import 'package:together_mobile/utils/app_dir.dart'; class SearchNewScreen extends StatefulWidget { const SearchNewScreen({super.key}); @override State createState() => _SearchNewScreenState(); } class _SearchNewScreenState extends State { final TextEditingController _controller = TextEditingController(); bool _isAvailable = false; bool _isHttpSent = false; @override void dispose() { _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( centerTitle: true, title: const Text('搜索'), ), body: Column( children: [ Container( margin: EdgeInsets.fromLTRB( 0, 30, 0, MediaQuery.of(context).size.height / 2 - 80 - 175, ), height: 30, child: _isHttpSent ? const CircularProgressIndicator() : const SizedBox(), ), Container( alignment: Alignment.centerLeft, width: 350, height: 50, child: TextField( onChanged: (value) { if (value.isEmpty) { setState(() { _isAvailable = false; }); } else { setState(() { _isAvailable = true; }); } }, textAlignVertical: TextAlignVertical.center, controller: _controller, decoration: InputDecoration( // contentPadding: EdgeInsets.only(left: 5), contentPadding: const EdgeInsets.symmetric(horizontal: 10), hintText: '用户名/邮箱/群聊号', prefixIcon: const Icon(Icons.search), border: OutlineInputBorder( borderRadius: BorderRadius.circular(20), ), ), ), ), const SizedBox( height: 30, ), Row( children: [ const Spacer(), FilledButton( onPressed: () {}, style: FilledButton.styleFrom( backgroundColor: _isAvailable ? kSecondaryColor : kUnAvailableColor, ), child: const Text('搜索群聊'), ), const Spacer(), FilledButton( onPressed: () async { if (!_isAvailable) { return; } String condition = ''; if (_controller.text.contains('@')) { condition = 'email'; } else { condition = 'username'; } await _searchNewFriend(context, condition); }, style: FilledButton.styleFrom( backgroundColor: _isAvailable ? kPrimaryColor : kUnAvailableColor, ), child: const Text('搜索好友'), ), const Spacer(), ], ) ], )); } Future _searchNewFriend(BuildContext context, String condition) async { if (!_isAvailable || _isHttpSent) { return; } String searchValue = _controller.text; if (searchValue == getIt.get().username || searchValue == getIt.get().email) { context.pushNamed('MainProfile'); return; } var (isExisted, friendId) = getIt.get().getIdBy( 'username', searchValue, ); if (isExisted) { context.pushNamed( 'FriendProfile', queryParameters: {'friendId': friendId}, ); return; } setState(() { _isHttpSent = true; }); Map res = await searchContactBy( condition, searchValue, ); setState(() { _isHttpSent = false; }); if (res['code'] == 10501) { // ignore: use_build_context_synchronously CherryToast.warning(title: const Text('用户不存在')).show(context); } else { Map accountProfile = { 'id': res['data']['id'], 'username': res['data']['username'], 'email': res['data']['email'], 'nickname': res['data']['nickname'] ?? '', 'birthday': res['data']['birthday'] ?? '', 'location': res['data']['location'] ?? '', 'status': res['data']['status'] ?? '', 'sign': res['data']['sign'] ?? '', 'gender': res['data']['gender'] == null ? '' : res['data']['gender'] == 'man' ? '男' : '女', 'avatar': res['data']['avatar'] ?? '', }; // if (accountProfile['avatar']!.isNotEmpty) { // if (!File(accountProfile['avatar']!).existsSync()) { // var data = await downloadAvatar(res['data']['avatar']); // await File(accountProfile['avatar']!).writeAsBytes(data); // } // } // ignore: use_build_context_synchronously context.pushNamed('AddFriend', queryParameters: accountProfile); } } }