import 'package:PiliPlus/common/widgets/dialog.dart'; import 'package:PiliPlus/common/widgets/loading_widget.dart'; import 'package:PiliPlus/common/widgets/scroll_physics.dart'; import 'package:PiliPlus/http/loading_state.dart'; import 'package:PiliPlus/models/member/tags.dart'; import 'package:PiliPlus/pages/follow/child_view.dart'; import 'package:PiliPlus/utils/utils.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:get/get.dart'; import 'controller.dart'; class FollowPage extends StatefulWidget { const FollowPage({super.key}); @override State createState() => _FollowPageState(); } class _FollowPageState extends State { final FollowController _followController = Get.put( FollowController(), tag: Utils.generateRandomString(8), ); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( _followController.isOwner ? '我的关注' : '${_followController.name}的关注', ), actions: _followController.isOwner ? [ IconButton( onPressed: _onCreateTag, icon: const Icon(Icons.add), tooltip: '新建分组', ), IconButton( onPressed: () => Get.toNamed( '/followSearch', arguments: { 'mid': _followController.mid, }, ), icon: const Icon(Icons.search_outlined), tooltip: '搜索', ), PopupMenuButton( icon: const Icon(Icons.more_vert), itemBuilder: (context) => [ PopupMenuItem( onTap: () => Get.toNamed('/blackListPage'), child: const Row( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.block, size: 19), SizedBox(width: 10), Text('黑名单管理'), ], ), ) ], ), const SizedBox(width: 6), ] : null, ), body: _followController.isOwner ? Obx(() => _buildBody(_followController.followState.value)) : FollowChildPage( controller: _followController, mid: _followController.mid), ); } bool _isCustomTag(tagid) { return tagid != null && tagid != 0 && tagid != -10 && tagid != -2; } Widget _buildBody(LoadingState loadingState) { return switch (loadingState) { Loading() => loadingWidget, Success() => Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ SafeArea( top: false, bottom: false, child: TabBar( isScrollable: true, tabAlignment: TabAlignment.start, controller: _followController.tabController, tabs: List.generate(_followController.tabs.length, (index) { return Obx(() { final item = _followController.tabs[index]; int? count = item.count; if (_isCustomTag(item.tagid)) { return GestureDetector( onLongPress: () { _onHandleTag(index, item); }, child: Tab( child: Row( children: [ Text( '${item.name}${count != null ? '($count)' : ''} ', ), Icon(Icons.menu, size: 18), ], ), ), ); } return Tab( text: '${item.name}${count != null ? '($count)' : ''}'); }); }).toList(), onTap: (value) { if (!_followController.tabController!.indexIsChanging) { final item = _followController.tabs[value]; if (_isCustomTag(item.tagid)) { _onHandleTag(value, item); } } }, ), ), Expanded( child: Material( color: Colors.transparent, child: tabBarView( controller: _followController.tabController, children: _followController.tabs .map( (item) => FollowChildPage( controller: _followController, mid: _followController.mid, tagid: item.tagid, ), ) .toList(), ), ), ), ], ), Error() => FollowChildPage( controller: _followController, mid: _followController.mid), _ => throw UnimplementedError(), }; } void _onHandleTag(int index, MemberTagItemModel item) { showDialog( context: context, builder: (context) { return AlertDialog( clipBehavior: Clip.hardEdge, contentPadding: const EdgeInsets.symmetric(vertical: 12), content: Column( mainAxisSize: MainAxisSize.min, children: [ ListTile( onTap: () { Get.back(); String tagName = item.name!; showConfirmDialog( context: context, title: '编辑分组名称', content: TextFormField( autofocus: true, initialValue: tagName, onChanged: (value) => tagName = value, inputFormatters: [ LengthLimitingTextInputFormatter(16), ], decoration: const InputDecoration(border: OutlineInputBorder()), ), onConfirm: () { if (tagName.isNotEmpty) { _followController.onUpdateTag( index, item.tagid, tagName); } }, ); }, dense: true, title: const Text( '修改名称', style: TextStyle(fontSize: 14), ), ), ListTile( onTap: () { Get.back(); showConfirmDialog( context: context, title: '删除分组', content: '删除后,该分组下的用户依旧保留?', onConfirm: () { _followController.onDelTag(item.tagid); }, ); }, dense: true, title: const Text( '删除分组', style: TextStyle(fontSize: 14), ), ), ], ), ); }, ); } void _onCreateTag() { String tagName = ''; showConfirmDialog( context: context, title: '新建分组', content: TextFormField( autofocus: true, initialValue: tagName, onChanged: (value) => tagName = value, inputFormatters: [ LengthLimitingTextInputFormatter(16), ], decoration: const InputDecoration(border: OutlineInputBorder()), ), onConfirm: () { _followController.onCreateTag(tagName); }, ); } }