375 lines
14 KiB
Dart
375 lines
14 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 '../game/game_info_page.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
|
|
super.dispose();
|
|
subscription.cancel();
|
|
}
|
|
|
|
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;
|
|
final t10 = size.width / 36;
|
|
final l15 = size.width / 24;
|
|
final l10 = size.width / 36;
|
|
final b5 = size.width / 72;
|
|
final s14 = size.width / 25.714285714285;
|
|
final s13 = size.width / 27.692307692307;
|
|
final t4 = size.width / 90;
|
|
final l11 = size.width / 32.727272727272;
|
|
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFF17181A),
|
|
body: Column(
|
|
children: [
|
|
Container(
|
|
margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top + t10),
|
|
child: Row(
|
|
children: [
|
|
GestureDetector(
|
|
onTap: () {
|
|
Navigator.pop(context);
|
|
},
|
|
child: Container(
|
|
margin: EdgeInsets.symmetric(horizontal: l15),
|
|
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: EdgeInsets.only(left: l10),
|
|
child: Image(width: w16, height: w16, image: const AssetImage('assets/images/ic_search.png')),
|
|
),
|
|
Expanded(
|
|
child: Container(
|
|
alignment: Alignment.center,
|
|
padding: EdgeInsets.only(left: l10, right: l10, bottom: b5),
|
|
child: TextField(
|
|
controller: _searchController,
|
|
style: TextStyle(fontSize: s13, 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: EdgeInsets.only(right: t10),
|
|
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: EdgeInsets.symmetric(horizontal: l15),
|
|
child: Text(
|
|
"搜索",
|
|
style: TextStyle(fontSize: s13, color: Color(0xFF626262)),
|
|
),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
|
|
///搜索历史
|
|
searchList.isEmpty
|
|
? Container(
|
|
margin: EdgeInsets.only(left: l15, right: l15, 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: Image(
|
|
width: l15,
|
|
height: l15,
|
|
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: EdgeInsets.symmetric(horizontal: s13, vertical: t4),
|
|
margin: EdgeInsets.only(right: l10,bottom: b5),
|
|
decoration: BoxDecoration(
|
|
color: Color(0xFF202530),
|
|
borderRadius: BorderRadius.all(Radius.circular(t4)),
|
|
),
|
|
child: Text(
|
|
searchHistoryList[index],
|
|
style: TextStyle(fontSize: s13, 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) => GestureDetector(
|
|
onTap: () {
|
|
FunctionUtil.loading();
|
|
_viewModel.gameSearch(gameHotSearch[index]);
|
|
_searchController.text = gameHotSearch[index];
|
|
searchHistoryList.add(_searchController.text);
|
|
saveArrayToSharedPrefs(searchHistoryList);
|
|
},
|
|
child: Container(
|
|
width: size.width / 2 - l15 - l15,
|
|
margin: EdgeInsets.only(right: l10, top: l10),
|
|
child: Text(
|
|
"${gameHotSearch[index]}",
|
|
overflow: TextOverflow.ellipsis,
|
|
style: TextStyle(fontSize: s14, color: Color(0xFF939394)),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
)
|
|
: Expanded(
|
|
child: Container(
|
|
margin: EdgeInsets.only(left: l15, right: l15, top: h36),
|
|
child: ListView.builder(
|
|
padding: EdgeInsets.zero,
|
|
itemCount: searchList.length,
|
|
itemBuilder: (context, index) {
|
|
return _searchItem(searchList[index], w57, t18, l11, s14);
|
|
}),
|
|
)),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
_searchItem(SearchBean data, w57, t18, l11, s14) {
|
|
return GestureDetector(
|
|
behavior: HitTestBehavior.opaque,
|
|
onTap: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
//导航打开新视图
|
|
builder: (context) => GameInfoPage(
|
|
gameId: "${data.gameId}",
|
|
),
|
|
));
|
|
},
|
|
child: 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: EdgeInsets.only(left: l11),
|
|
child: Text(
|
|
"${data.gameName}",
|
|
style: TextStyle(fontSize: s14, color: Color(0xFFD6D6D7)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|