import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_easyrefresh/easy_refresh.dart'; import 'package:get_it/get_it.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sport/bean/forum.dart'; import 'package:sport/constant/ui.dart' show ui_padding, ui_margin_list; import 'package:sport/pages/social/search_page.dart'; import 'package:sport/pages/social/user_friend_page.dart'; import 'package:sport/provider/lib/provider_widget.dart'; import 'package:sport/provider/lib/view_state.dart'; import 'package:sport/provider/lib/view_state_lifecycle.dart'; import 'package:sport/provider/message_model.dart'; import 'package:sport/provider/social_index_model.dart'; import 'package:sport/router/navigator_util.dart'; import 'package:sport/router/routes.dart'; import 'package:sport/widgets/appbar.dart'; import 'package:sport/widgets/decoration.dart'; import 'package:sport/widgets/error.dart'; import 'package:sport/widgets/image.dart'; import 'package:sport/widgets/loading.dart'; import 'package:sport/widgets/misc.dart'; import 'package:sport/widgets/page_resume.dart'; import 'package:sport/widgets/space.dart'; import 'package:umeng_common_sdk/umeng_common_sdk.dart'; class SocialIndexPage extends StatefulWidget { @override createState() => _PageState(); } class _PageState extends ViewStateLifecycle with AutomaticKeepAliveClientMixin, PageResume { @override void didChangeDependencies() { super.didChangeDependencies(); } @override void onResume() { super.onResume(); } @override Widget build(BuildContext context) { super.build(context); return Scaffold( body: ProviderWidget( model: model, onModelReady: (model) => model.initData(), builder: (_, model, __) { return EasyRefresh.builder( controller: model.refreshController, enableControlFinishRefresh: true, onRefresh: () => model.refresh(), header: buildClassicalHeader(), builder: (context, physics, header, footer) { return CustomScrollView( physics: physics, slivers: [ buildSliverAppBar(context, "社区", backgroundColor: Theme.of(context).scaffoldBackgroundColor, canBack: false, actions: [ IconButton( icon: Stack( alignment: Alignment.center, children: [ Image.asset("lib/assets/img/topbar_friendslist.png"), ValueListenableBuilder( valueListenable: GetIt.I().notifierFriend, builder: (BuildContext context, int value, Widget? child) { if (value > 0) { return Align( alignment: Alignment.topRight, child: Container( margin: const EdgeInsets.only(top: 6.0), width: 10, height: 10, decoration: BoxDecoration(shape: BoxShape.circle, color: Colors.red), child: Center( child: Text(""), ), ), ); } return Container(); }), ], ), onPressed: () async { UmengCommonSdk.onEvent("social_friend", {}); NavigatorUtil.goPage(context, (context) => UserFriendPage()); }, ), Selector( selector: (_, SocialIndexModel model) => model.viewState, shouldRebuild: (_, v) => v == ViewState.idle, builder: (BuildContext context, ViewState value, Widget? child) { return IconButton( icon: Stack( alignment: Alignment.center, children: [ Image.asset("lib/assets/img/bbs_icon_news.png"), FutureBuilder( future: SharedPreferences.getInstance(), builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done) { if (snapshot.data?.getBool("message_setting") ?? true == true) { return ValueListenableBuilder( valueListenable: GetIt.I().notifierSocial, builder: (BuildContext context, int value, Widget? child) { if (value > 0) { return Align( alignment: Alignment.topRight, child: Container( margin: const EdgeInsets.only(top: 6.0), width: 10, height: 10, decoration: BoxDecoration(shape: BoxShape.circle, color: Colors.red), child: Center( child: Text(""), ), ), ); } return Container(); }); } } return Container(); }), ], ), onPressed: () async { await NavigatorUtil.go(context, Routes.socialMessage); setState(() {}); }, ); }), const SizedBox(width: 6.0,), ], paddingLeading: false), if (header != null) header, if (model.isBusy) SliverToBoxAdapter( child: RequestLoadingWidget(), ), if (model.isIdle && model.list.isNotEmpty == true) SliverList( delegate: SliverChildBuilderDelegate( (context, index) { return _buildItemWidget(model.list[index]); }, childCount: model.list.length, ), ), if (model.isError) SliverToBoxAdapter( child: RequestErrorWidget(() { model.initData(); }), ), ], ); }); }), ); } Widget _buildSearchWidget() { return GestureDetector( onTap: () { Navigator.push(context, new MaterialPageRoute(builder: (context) => SearchPage())); }, child: Container( margin: EdgeInsets.fromLTRB(12.0, 0, 12.0, 12.0), height: 35, padding: EdgeInsets.fromLTRB(12.0, 0, 12.0, 0), decoration: BoxDecoration( color: Color(0xffF1F1F1), shape: BoxShape.rectangle, borderRadius: BorderRadius.all(Radius.circular(50)), ), child: Row( children: [ Image.asset("lib/assets/img/searchbar_icon_search.png"), Space( width: 4, ), Text( "输入关键词", strutStyle: StrutStyle(forceStrutHeight: true), style: TextStyle(fontSize: 14, color: Color(0xff999999)), ), ], ), ), ); } Widget _buildItemWidget(Forum item) { return Padding( padding: const EdgeInsets.fromLTRB(ui_padding, 6.0, ui_padding, 6.0), child: InkWell( onTap: () => NavigatorUtil.goSocialDetail(context, item), child: Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(10)), ), padding: const EdgeInsets.all(ui_padding), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ CachedNetworkImage(imageUrl: item.cover ?? "", width: 48.0, height: 48.0,), Padding( padding: const EdgeInsets.all(8.0), child: Text( "${item.name}", maxLines: 1, style: Theme.of(context).textTheme.headline2!.copyWith(fontSize: 16.0), ), ), Expanded( child: Center( child: Row( mainAxisSize: MainAxisSize.min, children: [ Image.asset("lib/assets/img/tab_bbs_normal.png", height: 14.0,), Padding( padding: const EdgeInsets.only(left: 5.0), child: ConstrainedBox( constraints: BoxConstraints(minWidth: 44), child: Text( "${item.subjectCount}", style: Theme.of(context).textTheme.bodyText1!, ), ), ), ],), ), ), Container( width: 79.0, height: 30.0, decoration: BoxDecoration(color: Theme.of(context).colorScheme.secondary, borderRadius: BorderRadius.circular(100.0)), child: Center( child: Text( "进入", style: Theme.of(context).textTheme.subtitle1?.copyWith(color: Colors.white), ), ), ), ], ), Padding( padding: const EdgeInsets.only(top: 12.0), child: Text( "${item.digest}", style: Theme.of(context).textTheme.subtitle1?.copyWith( height: 1.5,), ), ), ], ), ), ), ); } @override bool get wantKeepAlive => true; @override SocialIndexModel createModel() => SocialIndexModel(); }