import 'dart:ui';
|
|
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/material.dart';
|
import '../../api/video_api.dart';
|
import '../../ui/search/search_result.dart';
|
import '../../ui/widget/ad_express.dart';
|
import '../../ui/widget/search_widget.dart';
|
import '../../utils/ad_util.dart';
|
import '../../utils/video/search_record_util.dart';
|
|
import '../../utils/pageutils.dart';
|
|
class SearchPage extends StatefulWidget {
|
SearchPage({Key? key, required this.title}) : super(key: key);
|
|
// This widget is the home page of your application. It is stateful, meaning
|
// that it has a State object (defined below) that contains fields that affect
|
// how it looks.
|
|
// This class is the configuration for the state. It holds the values (in this
|
// case the title) provided by the parent (in this case the App widget) and
|
// used by the build method of the State. Fields in a Widget subclass are
|
// always marked "final".
|
|
final String title;
|
|
@override
|
_SearchPageState createState() => _SearchPageState();
|
}
|
|
class _SearchPageState extends State<SearchPage>
|
with AutomaticKeepAliveClientMixin {
|
final SugguestSearchController _sugguestSearchController =
|
SugguestSearchController();
|
|
bool showClose = false;
|
List<SearchKwModel> hotSearchList = [];
|
List<String> recordList = [];
|
AdType? adType;
|
|
@override
|
void initState() {
|
super.initState();
|
AdUtil.getAdType(AdPosition.videoSearch).then((value) {
|
setState(() {
|
adType = value;
|
});
|
});
|
|
getHotSearch();
|
setData();
|
}
|
|
void fromBack() {
|
FocusScope.of(context).requestFocus(FocusNode());
|
setData();
|
}
|
|
void setData() async {
|
List<String> tempList = await SearchRecordUtil.listRecord();
|
setState(() {
|
recordList = tempList;
|
});
|
}
|
|
void getHotSearch() async {
|
Map<String, dynamic>? result = await SearchApiUtil.getHotSearch(context);
|
if (result == null) {
|
return;
|
}
|
if (result["IsPost"] == "true") {
|
List<dynamic> list = result["Data"]["data"];
|
List<SearchKwModel> tempList =
|
List.filled(list.length, SearchKwModel(0, ""));
|
int row =
|
list.length % 2 == 0 ? (list.length ~/ 2) : ((list.length + 1) ~/ 2);
|
for (var i = 0; i < list.length; i++) {
|
if (i + 1 > row) {
|
tempList[(i + 1 - row) * 2 - 1] = SearchKwModel(i + 1, list[i]);
|
} else {
|
tempList[(i + 1) * 2 - 2] = SearchKwModel(i + 1, list[i]);
|
}
|
}
|
|
// mList.sort((a, b) => a.index.compareTo(b.index));
|
|
setState(() {
|
hotSearchList = tempList;
|
});
|
}
|
}
|
|
void getSuggestSearch(String key) async {
|
Map<String, dynamic>? result =
|
await SearchApiUtil.getSuggestSearch(context, key);
|
if (result == null) {
|
return;
|
}
|
if (result["IsPost"] == "true") {
|
List<dynamic> list = result["Data"]["data"];
|
|
List<String> suggestList = [];
|
list.forEach((element) {
|
suggestList.add(element);
|
});
|
|
_sugguestSearchController.setData!(suggestList);
|
if (suggestList.isNotEmpty) {
|
_sugguestSearchController.setShow!(true);
|
} else {
|
_sugguestSearchController.setShow!(false);
|
}
|
} else {
|
_sugguestSearchController.setData!([]);
|
_sugguestSearchController.setShow!(false);
|
}
|
}
|
|
@override
|
Widget build(BuildContext context) {
|
return Scaffold(
|
backgroundColor: Colors.white,
|
body: Stack(children: [
|
Column(
|
children: [
|
Container(
|
height: MediaQuery.of(context).viewPadding.top,
|
),
|
Container(
|
height: 5,
|
),
|
//搜索栏
|
SearchBar(
|
hint: widget.title,
|
onChange: (content) {
|
if (content.isEmpty) {
|
_sugguestSearchController.setData!([]);
|
_sugguestSearchController.setShow!(false);
|
} else {
|
getSuggestSearch(content);
|
}
|
},
|
onSubmit: (content) {
|
NavigatorUtil.navigateToNextPage(
|
context,
|
SearchResultPage(
|
title: content.isNotEmpty ? content : widget.title),
|
(data) {
|
fromBack();
|
});
|
},
|
),
|
//内容
|
Expanded(
|
child: SingleChildScrollView(
|
child: Column(children: [
|
recordList.isNotEmpty
|
? Container(
|
padding: const EdgeInsets.fromLTRB(12, 20, 12, 10),
|
child: Row(
|
crossAxisAlignment: CrossAxisAlignment.center,
|
children: [
|
const Text("历史搜索",
|
style: TextStyle(
|
color: Color(0xFFA6A6A6), fontSize: 15)),
|
Expanded(child: Container()),
|
InkWell(
|
onTap: () {
|
SearchRecordUtil.clearRecord();
|
setState(() {
|
recordList = [];
|
});
|
},
|
child: const Icon(
|
Icons.delete_outline,
|
color: Color(0xFFA6A6A6),
|
))
|
]))
|
: Container(),
|
Container(
|
padding: const EdgeInsets.fromLTRB(12, 7, 12, 10),
|
alignment: Alignment.topLeft,
|
child: Wrap(
|
spacing: 13.5,
|
runSpacing: 15.5,
|
alignment: WrapAlignment.start,
|
children:
|
recordList.map((e) => getRecordItem(e)).toList())),
|
|
///热门搜索
|
Container(
|
margin: const EdgeInsets.fromLTRB(12, 20, 12, 10),
|
alignment: Alignment.center,
|
padding: const EdgeInsets.fromLTRB(15, 15, 15, 25),
|
decoration: BoxDecoration(
|
borderRadius: BorderRadius.circular(15),
|
border: Border.all(color: Color(0xFFFFD7E4), width: 0.5),
|
gradient: const LinearGradient(
|
begin: Alignment.topCenter,
|
end: Alignment.bottomCenter,
|
colors: [
|
Color(0xFFFFCEDE),
|
Color(0xFFFFFFFF),
|
Color(0xFFFFFFFF)
|
])),
|
child: Column(
|
children: [
|
const Text(
|
"热门搜索",
|
style: TextStyle(color: Color(0xFFFF558D)),
|
),
|
Container(
|
height: 23,
|
),
|
Wrap(
|
alignment: WrapAlignment.spaceBetween,
|
runSpacing: 11,
|
spacing: 10,
|
children: hotSearchList
|
.map((e) => getHotItem(e.index, e.kw))
|
.toList())
|
],
|
),
|
),
|
|
///广告
|
adType == null
|
? Container()
|
: Container(
|
alignment: Alignment.center,
|
height: MediaQuery.of(context).size.width * 0.8,
|
child: _nativeAdView())
|
])))
|
],
|
),
|
SugguestSearchView(
|
contentList: [],
|
sugguestSearchController: _sugguestSearchController,
|
onCancel: (value) {
|
_sugguestSearchController.setShow!(false);
|
},
|
onItemClick: (value) {
|
_sugguestSearchController.setShow!(false);
|
NavigatorUtil.navigateToNextPage(
|
context, SearchResultPage(title: value), (data) {
|
fromBack();
|
});
|
},
|
)
|
]));
|
}
|
|
Widget _nativeAdView() {
|
return adType == AdType.csj
|
? CSJEXpressAd(
|
CSJADConstant.PID_VIDEO_SEARCH,
|
MediaQuery.of(context).size.width - 20,
|
(MediaQuery.of(context).size.width - 20) * 0.8,
|
close: () {
|
setState(() {
|
adType = null;
|
});
|
},
|
loadFail: () {
|
setState(() {
|
adType = null;
|
});
|
},
|
)
|
: GDTEXpressAd(
|
GDTADConstant.PID_VIDEO_SEARCH,
|
MediaQuery.of(context).size.width - 20,
|
(MediaQuery.of(context).size.width - 20) * 0.8,
|
close: () {
|
setState(() {
|
adType = null;
|
});
|
},
|
loadFail: () {
|
setState(() {
|
adType = null;
|
});
|
},
|
);
|
}
|
|
@override
|
bool get wantKeepAlive => true;
|
|
Widget getRecordItem(String text) {
|
return InkWell(
|
onTap: () {
|
NavigatorUtil.navigateToNextPage(
|
context, SearchResultPage(title: text), (data) {
|
fromBack();
|
});
|
},
|
child: Container(
|
padding: const EdgeInsets.fromLTRB(23, 8, 23, 8),
|
decoration: BoxDecoration(
|
borderRadius: BorderRadius.circular(8), color: Color(0xFFF7F7F7)),
|
child: Text(
|
text,
|
maxLines: 1,
|
overflow: TextOverflow.ellipsis,
|
style: const TextStyle(color: Colors.black, fontSize: 12),
|
),
|
));
|
}
|
|
Widget getHotItem(int index, String text) {
|
Color indexColor = const Color(0xFFD5D5D5);
|
switch (index) {
|
case 1:
|
indexColor = const Color(0xFFFF558D);
|
break;
|
case 2:
|
indexColor = const Color(0xFFFED73C);
|
break;
|
case 3:
|
indexColor = const Color(0xFF3CD7FE);
|
break;
|
}
|
return InkWell(
|
onTap: () {
|
NavigatorUtil.navigateToNextPage(
|
context, SearchResultPage(title: text), (data) {
|
fromBack();
|
});
|
},
|
child: SizedBox(
|
width: (MediaQuery.of(context).size.width - 70) / 2,
|
child: Row(
|
crossAxisAlignment: CrossAxisAlignment.center,
|
children: [
|
Container(
|
width: 17,
|
height: 17,
|
alignment: Alignment.center,
|
decoration: BoxDecoration(
|
borderRadius: BorderRadius.circular(6), color: indexColor),
|
child: Text(
|
"$index",
|
style: const TextStyle(color: Colors.white, fontSize: 11),
|
),
|
),
|
Container(
|
width: 12.5,
|
),
|
Expanded(
|
child: Text(
|
text,
|
maxLines: 1,
|
overflow: TextOverflow.ellipsis,
|
style: TextStyle(color: const Color(0xFF4A4A4A), fontSize: 12),
|
))
|
],
|
),
|
));
|
}
|
}
|
|
class SearchKwModel {
|
int index;
|
String kw;
|
|
SearchKwModel(this.index, this.kw);
|
}
|