SteamCloudGame/lib/tools/home/search_page.dart
2024-12-01 18:09:13 +08:00

343 lines
13 KiB
Dart

import 'dart:async';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../beans/search_bean.dart';
import '../../common/func.dart';
import 'my_home_model.dart';
class SearchPage extends StatefulWidget {
const SearchPage({super.key});
@override
State<SearchPage> createState() => _SearchPageState();
}
class _SearchPageState extends State<SearchPage> {
final TextEditingController _searchController = TextEditingController();
bool isContent = true;
late StreamSubscription subscription;
final MyHomeModel _viewModel = MyHomeModel();
List<SearchBean> searchList = [];
List<String> searchHistoryList = [];
List<dynamic> gameHotSearch = [];
@override
void initState() {
// TODO: implement initState
super.initState();
subscription = _viewModel.streamController.stream.listen((event) {
String code = event['code'];
if (code.isNotEmpty) {
switch (code) {
case "gameSearch":
EasyLoading.dismiss();
searchList = event['data'];
if (searchList.isEmpty) {
EasyLoading.showToast("未搜索到相关游戏");
}
break;
case "gameHotSearch":
gameHotSearch = event['data'];
break;
}
setState(() {});
}
});
loadData();
}
loadData() async {
searchHistoryList = await readArrayFromSharedPrefs();
_viewModel.gameHotSearch();
setState(() {});
}
@override
void dispose() {
// TODO: implement dispose
subscription.cancel();
super.dispose();
}
void _searchChanged(String str) {
if (str != "") {
isContent = false;
} else {
isContent = true;
}
setState(() {});
}
// 保存数组到SharedPreferences
Future<void> saveArrayToSharedPrefs(List<String> array) async {
final prefs = await SharedPreferences.getInstance();
// 将数组转换为字符串列表,并使用逗号分隔
final String arrayString = array.join(',');
prefs.setString('searchList', arrayString);
}
// 从SharedPreferences读取数组
Future<List<String>> readArrayFromSharedPrefs() async {
final prefs = await SharedPreferences.getInstance();
final String arrayString = prefs.getString('searchList') ?? '';
// 将字符串列表转换回数组
return arrayString.split(',');
}
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
final w19 = size.width / 18.94736842105263;
final h26 = size.width / 13.84615384615385;
final h36 = size.width / 10;
final c19 = size.width / 18.94736842105263;
final w16 = size.width / 22.5;
final t18 = size.width / 20;
final w155 = size.width / 2.32258064516129;
final w57 = size.width / 6.315789473684211;
return Scaffold(
backgroundColor: const Color(0xFF17181A),
body: Column(
children: [
Container(
margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top + 10),
child: Row(
children: [
GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 15),
child: Image(
width: w19,
height: h26,
image: const AssetImage('assets/images/btn_fanhui.png'),
),
),
),
Expanded(
child: Container(
height: h36,
decoration: BoxDecoration(
color: const Color(0xFF202530),
borderRadius: BorderRadius.all(Radius.circular(c19)),
),
child: Row(
children: [
Container(
padding: const EdgeInsets.only(left: 10),
child: Image(width: w16, height: w16, image: const AssetImage('assets/images/ic_search.png')),
),
Expanded(
child: Container(
alignment: Alignment.center,
padding: const EdgeInsets.only(left: 10, right: 10, bottom: 5),
child: TextField(
controller: _searchController,
style: const TextStyle(fontSize: 13, color: Colors.white),
decoration: const InputDecoration(
border: InputBorder.none,
),
onChanged: _searchChanged,
onSubmitted: (value) {
if (_searchController.text == "") {
EasyLoading.showToast("请输入内容");
return;
}
FunctionUtil.loading();
searchHistoryList.add(_searchController.text);
saveArrayToSharedPrefs(searchHistoryList);
//搜索
_viewModel.gameSearch(_searchController.text);
setState(() {});
},
),
),
),
///清空输入内
!isContent
? GestureDetector(
onTap: () {
setState(() {
_searchController.text = "";
searchList.clear();
isContent = true;
});
},
child: Container(
padding: const EdgeInsets.only(right: 10),
child: Image(
width: w16,
height: w16,
image: const AssetImage('assets/images/ic_clear.png'),
),
),
)
: Container(),
],
),
)),
GestureDetector(
onTap: () {
if (_searchController.text == "") {
EasyLoading.showToast("请输入内容");
return;
}
FunctionUtil.loading();
searchHistoryList.add(_searchController.text);
saveArrayToSharedPrefs(searchHistoryList);
//搜索
_viewModel.gameSearch(_searchController.text);
setState(() {});
},
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 15),
child: const Text(
"搜索",
style: TextStyle(fontSize: 13, color: Color(0xFF626262)),
),
),
)
],
),
),
///搜索历史
searchList.isEmpty
? Container(
margin: EdgeInsets.only(left: 14, right: 14, top: h36),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"历史搜索",
style: TextStyle(fontSize: w16, color: const Color(0xFFD6D6D7)),
),
GestureDetector(
onTap: () {
searchHistoryList.clear();
saveArrayToSharedPrefs(searchHistoryList);
setState(() {});
},
child: const Image(
width: 14,
height: 14,
image: AssetImage('assets/images/ic_del.png'),
),
),
],
),
///搜索历史列表
Container(
width: size.width,
margin: EdgeInsets.only(top: t18),
child: Wrap(
children: List.generate(
searchHistoryList.length,
(index) => GestureDetector(
onTap: () {
//搜索
FunctionUtil.loading();
_viewModel.gameSearch(searchHistoryList[index]);
_searchController.text = searchHistoryList[index];
isContent = false;
},
child: index != 0
? Container(
padding: const EdgeInsets.symmetric(horizontal: 13, vertical: 4),
margin: const EdgeInsets.only(right: 10),
decoration: const BoxDecoration(
color: Color(0xFF202530),
borderRadius: BorderRadius.all(Radius.circular(4)),
),
child: Text(
searchHistoryList[index],
style: const TextStyle(fontSize: 13, color: Color(0xFFB6B6B6)),
),
)
: Container(),
)),
),
),
Container(
alignment: Alignment.centerLeft,
margin: EdgeInsets.only(top: h36),
child: Text(
"热门搜索",
style: TextStyle(fontSize: w16, color: const Color(0xFFD6D6D7)),
),
),
Container(
margin: EdgeInsets.only(top: t18),
child: Wrap(
children: List.generate(
gameHotSearch.length,
(index) => Container(
width: w155,
margin: const EdgeInsets.only(right: 10, top: 10),
child: Text(
"${gameHotSearch[index]}",
overflow: TextOverflow.ellipsis,
style: const TextStyle(fontSize: 14, color: Color(0xFF939394)),
),
),
),
),
)
],
),
)
: Expanded(
child: Container(
margin: EdgeInsets.only(left: 14, right: 14, top: h36),
child: ListView.builder(
padding: EdgeInsets.zero,
itemCount: searchList.length,
itemBuilder: (context, index) {
return _searchItem(searchList[index], w57, t18);
}),
)),
],
),
);
}
_searchItem(SearchBean data, w57, t18) {
return Container(
margin: EdgeInsets.only(top: t18),
child: Row(
children: [
CachedNetworkImage(
width: w57,
height: w57,
imageUrl: '${data.gameIconImage}',
errorWidget: (context, url, error) => const Icon(Icons.error),
),
Container(
margin: const EdgeInsets.only(left: 11),
child: Text(
"${data.gameName}",
style: const TextStyle(fontSize: 14, color: Color(0xFFD6D6D7)),
),
),
],
),
);
}
}