384 lines
15 KiB
Dart
384 lines
15 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 h36 = size.width / 10;
|
|
final c19 = size.width / 18.94736842105263;
|
|
final t18 = size.width / 20;
|
|
final w34 = size.width / 10.588235294117;
|
|
final t10 = size.width / 36;
|
|
final l15 = size.width / 24;
|
|
final l10 = size.width / 36;
|
|
final b5 = size.width / 72;
|
|
final s6 = size.width / 60;
|
|
final l11 = size.width / 32.727272727272;
|
|
final h6 = size.width / 60;
|
|
final h17 = size.width / 21.176470588235;
|
|
final l8 = size.width / 45;
|
|
final s5 = size.width / 72;
|
|
final l5 = size.width / 72;
|
|
final t17 = size.width / 21.176470588235;
|
|
final s7 = size.width / 51.428571428571;
|
|
final c2 = size.width / 180;
|
|
final t3 = size.width / 120;
|
|
|
|
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(
|
|
padding: EdgeInsets.only(right: l5, top: l5, bottom: l5),
|
|
margin: EdgeInsets.only(left: l11, right: l5),
|
|
child: Image(
|
|
width: l11,
|
|
height: l11,
|
|
image: const AssetImage('assets/images/btn_fanhui.png'),
|
|
),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: Container(
|
|
height: h17,
|
|
decoration: BoxDecoration(
|
|
color: const Color(0xFF202530),
|
|
borderRadius: BorderRadius.all(Radius.circular(c19)),
|
|
),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Container(
|
|
padding: EdgeInsets.only(left: l8),
|
|
child: Image(width: h6, height: h6, image: const AssetImage('assets/images/ic_search2.png')),
|
|
),
|
|
Expanded(
|
|
child: Container(
|
|
padding: EdgeInsets.only(left: l5, right: l5),
|
|
child: TextField(
|
|
controller: _searchController,
|
|
textAlign: TextAlign.start,
|
|
style: TextStyle(fontSize: s5, color: Colors.white),
|
|
decoration: 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: l8),
|
|
child: Image(
|
|
width: h6,
|
|
height: h6,
|
|
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: s5, color: Color(0xFF626262)),
|
|
),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
|
|
///搜索历史
|
|
searchList.isEmpty
|
|
? Expanded(
|
|
child: Container(
|
|
margin: EdgeInsets.only(left: l11, right: l11),
|
|
child: SingleChildScrollView(
|
|
child: Column(
|
|
children: [
|
|
Container(
|
|
padding: EdgeInsets.only(top: t17),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text(
|
|
"历史搜索",
|
|
style: TextStyle(fontSize: s7, color: const Color(0xFFD6D6D7)),
|
|
),
|
|
GestureDetector(
|
|
onTap: () {
|
|
searchHistoryList.clear();
|
|
saveArrayToSharedPrefs(searchHistoryList);
|
|
setState(() {});
|
|
},
|
|
child: Image(
|
|
width: s5,
|
|
height: h6,
|
|
image: AssetImage('assets/images/ic_del.png'),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
|
|
///搜索历史列表
|
|
Container(
|
|
width: size.width,
|
|
margin: EdgeInsets.only(top: t10),
|
|
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: b5, vertical: t3),
|
|
margin: EdgeInsets.only(right: l10, bottom: b5),
|
|
decoration: BoxDecoration(
|
|
color: Color(0xFF202530),
|
|
borderRadius: BorderRadius.all(Radius.circular(c2)),
|
|
),
|
|
child: Text(
|
|
searchHistoryList[index],
|
|
style: TextStyle(fontSize: s5, color: Color(0xFFB6B6B6)),
|
|
),
|
|
)
|
|
: Container(),
|
|
)),
|
|
),
|
|
),
|
|
|
|
///热门搜索
|
|
Container(
|
|
alignment: Alignment.centerLeft,
|
|
margin: EdgeInsets.only(top: t17),
|
|
child: Text(
|
|
"热门搜索",
|
|
style: TextStyle(fontSize: s7, color: const Color(0xFFD6D6D7)),
|
|
),
|
|
),
|
|
|
|
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 - l11 - l11,
|
|
margin: EdgeInsets.only(right: l11, top: t10),
|
|
child: Text(
|
|
"${gameHotSearch[index]}",
|
|
overflow: TextOverflow.ellipsis,
|
|
style: TextStyle(fontSize: s5, color: Color(0xFF939394)),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
),
|
|
)
|
|
: Expanded(
|
|
child: Container(
|
|
margin: EdgeInsets.only(left: l15, right: l15, top: l10),
|
|
child: ListView.builder(
|
|
padding: EdgeInsets.zero,
|
|
itemCount: searchList.length,
|
|
itemBuilder: (context, index) {
|
|
return _searchItem(searchList[index], w34, l10, l11, s6);
|
|
}),
|
|
)),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
_searchItem(SearchBean data, w34, l10, l11, s6) {
|
|
return GestureDetector(
|
|
behavior: HitTestBehavior.opaque,
|
|
onTap: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
//导航打开新视图
|
|
builder: (context) => GameInfoPage(
|
|
gameId: "${data.gameId}",
|
|
),
|
|
));
|
|
},
|
|
child: Container(
|
|
margin: EdgeInsets.only(top: l10),
|
|
child: Row(
|
|
children: [
|
|
CachedNetworkImage(
|
|
width: w34,
|
|
height: w34,
|
|
imageUrl: '${data.gameIconImage}',
|
|
errorWidget: (context, url, error) => const Icon(Icons.error),
|
|
),
|
|
Container(
|
|
margin: EdgeInsets.only(left: l11),
|
|
child: Text(
|
|
"${data.gameName}",
|
|
style: TextStyle(fontSize: s6, color: Color(0xFFD6D6D7)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|