diff --git a/lib/pages/dynamics/widgets/vote.dart b/lib/pages/dynamics/widgets/vote.dart index a6918115..79ff9789 100644 --- a/lib/pages/dynamics/widgets/vote.dart +++ b/lib/pages/dynamics/widgets/vote.dart @@ -6,18 +6,15 @@ import 'package:PiliPlus/http/loading_state.dart'; import 'package:PiliPlus/models/dynamics/vote_model.dart'; import 'package:PiliPlus/utils/utils.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; class VotePanel extends StatefulWidget { final VoteInfo voteInfo; final FutureOr> Function(Set, bool) callback; - final bool embedded; const VotePanel({ super.key, required this.voteInfo, required this.callback, - this.embedded = false, }); @override @@ -28,7 +25,6 @@ class _VotePanelState extends State { bool anonymity = false; late VoteInfo _voteInfo; - late final bool _embedded; late final groupValue = _voteInfo.myVotes?.toSet() ?? {}; late var _percentage = _cnt2Percentage(_voteInfo.options); late bool _enabled = groupValue.isEmpty && @@ -36,21 +32,10 @@ class _VotePanelState extends State { late bool _showPercentage = !_enabled; late final _maxCnt = _voteInfo.choiceCnt ?? _voteInfo.options.length; - late final _selectedNum = ValueNotifier(groupValue.length); - late final _canVote = ValueNotifier(false); - @override void initState() { super.initState(); _voteInfo = widget.voteInfo; - _embedded = widget.embedded || widget.voteInfo.options.length < 5; - } - - @override - void dispose() { - _selectedNum.dispose(); - _canVote.dispose(); - super.dispose(); } @override @@ -97,45 +82,35 @@ class _VotePanelState extends State { ? '已结束' : '已完成', ), - if (_enabled) - ValueListenableBuilder( - valueListenable: _selectedNum, - builder: (_, val, __) => Text('$val / $_maxCnt'), - ), + if (_enabled) Text('${groupValue.length} / $_maxCnt'), ], ), - if (_embedded) - _buildContext() - else - Flexible(fit: FlexFit.loose, child: _buildContext()), + Flexible(fit: FlexFit.loose, child: _buildContext()), if (_enabled) Padding( padding: const EdgeInsets.only(top: 16), - child: ValueListenableBuilder( - valueListenable: _canVote, - builder: (_, val, __) => OutlinedButton( - onPressed: val - ? () async { - final res = await widget.callback( - groupValue, - anonymity, - ); - if (res.isSuccess) { - if (mounted) { - setState(() { - _enabled = false; - _showPercentage = true; - _voteInfo = res.data; - _percentage = _cnt2Percentage(_voteInfo.options); - }); - } - } else { - SmartDialog.showToast((res as Error).errMsg ?? ''); + child: OutlinedButton( + onPressed: groupValue.isNotEmpty + ? () async { + final res = await widget.callback( + groupValue, + anonymity, + ); + if (res.isSuccess) { + if (mounted) { + setState(() { + _enabled = false; + _showPercentage = true; + _voteInfo = res.data; + _percentage = _cnt2Percentage(_voteInfo.options); + }); } + } else { + res.toast(); } - : null, - child: const Center(child: Text('投票')), - ), + } + : null, + child: const Center(child: Text('投票')), ), ), ], @@ -159,7 +134,6 @@ class _VotePanelState extends State { anonymity = val; }, ), - // TODO 转发到动态 ]; Widget _buildOptions(int index) { @@ -167,7 +141,7 @@ class _VotePanelState extends State { final selected = groupValue.contains(opt.optidx); return Padding( padding: const EdgeInsets.symmetric(vertical: 4), - child: PercentageChip( + child: PercentageChip( label: opt.optdesc!, percentage: _showPercentage ? _percentage[index] : null, selected: selected, @@ -184,31 +158,20 @@ class _VotePanelState extends State { } else { groupValue.remove(optidx); } - _selectedNum.value = groupValue.length; - _canVote.value = groupValue.isNotEmpty; setState(() {}); } Widget _buildContext() { - return _embedded - ? Column( - mainAxisSize: MainAxisSize.min, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - ...List.generate(_voteInfo.options.length, _buildOptions), - if (_enabled) ..._checkBoxs, - ], - ) - : CustomScrollView( - shrinkWrap: true, - slivers: [ - SliverList.builder( - itemCount: _voteInfo.options.length, - itemBuilder: (context, index) => _buildOptions(index), - ), - if (_enabled) SliverList.list(children: _checkBoxs) - ], - ); + return CustomScrollView( + shrinkWrap: true, + slivers: [ + SliverList.builder( + itemCount: _voteInfo.options.length, + itemBuilder: (context, index) => _buildOptions(index), + ), + if (_enabled) SliverList.list(children: _checkBoxs) + ], + ); } static List _cnt2Percentage(List