| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'package:news_app/extension/base.dart';
- import '../constant/api_const.dart';
- import '../http/http_util.dart';
- import '../http/model_parser.dart';
- import '../model/activity_model.dart';
- import '../model/hot_word_model.dart';
- class ActivitySearchProvider extends Notifier<ActivitySearchData> {
- @override
- ActivitySearchData build() {
- return ActivitySearchData();
- }
- Future<void>fetchActivityHotWord(String type) async{
- var jsonData = await HttpUtil().get(
- apiHotWord,
- queryParameters: {"q": type},
- );
- final newList = ModelParser.parseList<HotWordModel>(
- jsonData,
- HotWordModel.fromJson,
- );
- state = state.copyWith(hotWords: newList);
- }
- Future<void> fetchSearch(String keyword, int pageNum) async {
- final jsonData = await HttpUtil().get(
- apiActivitySearch,
- queryParameters: {
- "q": keyword,
- "pageSize": 10.toString(),
- "pageNum": pageNum.toString(),
- },
- );
- final response = ModelParser.parseObject<ActivitySearchModel>(
- jsonData,
- ActivitySearchModel.fromJson,
- );
- if (pageNum == 0) {
- state = state.copyWith(searchModel: response);
- } else {
- //把新的列表添加到旧的列表中
- state = state.copyWith(searchModel: ActivitySearchModel(
- total: response.total,
- rows: [...state.searchModel?.rows ?? [], ...response.rows ?? []],
- ));
- }
- }
- }
- class ActivitySearchModel {
- List<ActivityModelRecord?>? rows;
- int? total;
- ActivitySearchModel({this.rows, this.total});
- ActivitySearchModel.fromJson(Map<String, dynamic> json) {
- if (json['rows'] != null) {
- final v = json['rows'];
- final arr0 = <ActivityModelRecord>[];
- v.forEach((v) {
- arr0.add(ActivityModelRecord.fromJson(v));
- });
- rows = arr0;
- }
- total = json['total']?.toString().convertInt;
- }
- Map<String, dynamic> toJson() {
- final data = <String, dynamic>{};
- if (rows != null) {
- final v = rows;
- final arr0 = [];
- for (var v in v!) {
- arr0.add(v!.toJson());
- }
- data['rows'] = arr0;
- }
- data['total'] = total;
- return data;
- }
- }
- class ActivitySearchData{
- final ActivitySearchModel? searchModel;
- final List<HotWordModel> hotWords;
- ActivitySearchData({
- this.searchModel,
- this.hotWords = const [],
- });
- ActivitySearchData copyWith({
- ActivitySearchModel? searchModel,
- List<HotWordModel>? hotWords,
- }){
- return ActivitySearchData(
- searchModel: searchModel ?? this.searchModel,
- hotWords:hotWords ?? this.hotWords,
- );
- }
- }
|