mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-17 23:56:13 +08:00
opt: vote (#858)
This commit is contained in:
committed by
GitHub
parent
616c129ffd
commit
cf0bf1e587
@@ -6,18 +6,15 @@ import 'package:PiliPlus/http/loading_state.dart';
|
|||||||
import 'package:PiliPlus/models/dynamics/vote_model.dart';
|
import 'package:PiliPlus/models/dynamics/vote_model.dart';
|
||||||
import 'package:PiliPlus/utils/utils.dart';
|
import 'package:PiliPlus/utils/utils.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
|
||||||
|
|
||||||
class VotePanel extends StatefulWidget {
|
class VotePanel extends StatefulWidget {
|
||||||
final VoteInfo voteInfo;
|
final VoteInfo voteInfo;
|
||||||
final FutureOr<LoadingState<VoteInfo>> Function(Set<int>, bool) callback;
|
final FutureOr<LoadingState<VoteInfo>> Function(Set<int>, bool) callback;
|
||||||
final bool embedded;
|
|
||||||
|
|
||||||
const VotePanel({
|
const VotePanel({
|
||||||
super.key,
|
super.key,
|
||||||
required this.voteInfo,
|
required this.voteInfo,
|
||||||
required this.callback,
|
required this.callback,
|
||||||
this.embedded = false,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -28,7 +25,6 @@ class _VotePanelState extends State<VotePanel> {
|
|||||||
bool anonymity = false;
|
bool anonymity = false;
|
||||||
|
|
||||||
late VoteInfo _voteInfo;
|
late VoteInfo _voteInfo;
|
||||||
late final bool _embedded;
|
|
||||||
late final groupValue = _voteInfo.myVotes?.toSet() ?? {};
|
late final groupValue = _voteInfo.myVotes?.toSet() ?? {};
|
||||||
late var _percentage = _cnt2Percentage(_voteInfo.options);
|
late var _percentage = _cnt2Percentage(_voteInfo.options);
|
||||||
late bool _enabled = groupValue.isEmpty &&
|
late bool _enabled = groupValue.isEmpty &&
|
||||||
@@ -36,21 +32,10 @@ class _VotePanelState extends State<VotePanel> {
|
|||||||
late bool _showPercentage = !_enabled;
|
late bool _showPercentage = !_enabled;
|
||||||
late final _maxCnt = _voteInfo.choiceCnt ?? _voteInfo.options.length;
|
late final _maxCnt = _voteInfo.choiceCnt ?? _voteInfo.options.length;
|
||||||
|
|
||||||
late final _selectedNum = ValueNotifier(groupValue.length);
|
|
||||||
late final _canVote = ValueNotifier(false);
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_voteInfo = widget.voteInfo;
|
_voteInfo = widget.voteInfo;
|
||||||
_embedded = widget.embedded || widget.voteInfo.options.length < 5;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void dispose() {
|
|
||||||
_selectedNum.dispose();
|
|
||||||
_canVote.dispose();
|
|
||||||
super.dispose();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -97,45 +82,35 @@ class _VotePanelState extends State<VotePanel> {
|
|||||||
? '已结束'
|
? '已结束'
|
||||||
: '已完成',
|
: '已完成',
|
||||||
),
|
),
|
||||||
if (_enabled)
|
if (_enabled) Text('${groupValue.length} / $_maxCnt'),
|
||||||
ValueListenableBuilder(
|
|
||||||
valueListenable: _selectedNum,
|
|
||||||
builder: (_, val, __) => Text('$val / $_maxCnt'),
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
if (_embedded)
|
Flexible(fit: FlexFit.loose, child: _buildContext()),
|
||||||
_buildContext()
|
|
||||||
else
|
|
||||||
Flexible(fit: FlexFit.loose, child: _buildContext()),
|
|
||||||
if (_enabled)
|
if (_enabled)
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.only(top: 16),
|
padding: const EdgeInsets.only(top: 16),
|
||||||
child: ValueListenableBuilder(
|
child: OutlinedButton(
|
||||||
valueListenable: _canVote,
|
onPressed: groupValue.isNotEmpty
|
||||||
builder: (_, val, __) => OutlinedButton(
|
? () async {
|
||||||
onPressed: val
|
final res = await widget.callback(
|
||||||
? () async {
|
groupValue,
|
||||||
final res = await widget.callback(
|
anonymity,
|
||||||
groupValue,
|
);
|
||||||
anonymity,
|
if (res.isSuccess) {
|
||||||
);
|
if (mounted) {
|
||||||
if (res.isSuccess) {
|
setState(() {
|
||||||
if (mounted) {
|
_enabled = false;
|
||||||
setState(() {
|
_showPercentage = true;
|
||||||
_enabled = false;
|
_voteInfo = res.data;
|
||||||
_showPercentage = true;
|
_percentage = _cnt2Percentage(_voteInfo.options);
|
||||||
_voteInfo = res.data;
|
});
|
||||||
_percentage = _cnt2Percentage(_voteInfo.options);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
SmartDialog.showToast((res as Error).errMsg ?? '');
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
res.toast();
|
||||||
}
|
}
|
||||||
: null,
|
}
|
||||||
child: const Center(child: Text('投票')),
|
: null,
|
||||||
),
|
child: const Center(child: Text('投票')),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -159,7 +134,6 @@ class _VotePanelState extends State<VotePanel> {
|
|||||||
anonymity = val;
|
anonymity = val;
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
// TODO 转发到动态
|
|
||||||
];
|
];
|
||||||
|
|
||||||
Widget _buildOptions(int index) {
|
Widget _buildOptions(int index) {
|
||||||
@@ -167,7 +141,7 @@ class _VotePanelState extends State<VotePanel> {
|
|||||||
final selected = groupValue.contains(opt.optidx);
|
final selected = groupValue.contains(opt.optidx);
|
||||||
return Padding(
|
return Padding(
|
||||||
padding: const EdgeInsets.symmetric(vertical: 4),
|
padding: const EdgeInsets.symmetric(vertical: 4),
|
||||||
child: PercentageChip<int>(
|
child: PercentageChip(
|
||||||
label: opt.optdesc!,
|
label: opt.optdesc!,
|
||||||
percentage: _showPercentage ? _percentage[index] : null,
|
percentage: _showPercentage ? _percentage[index] : null,
|
||||||
selected: selected,
|
selected: selected,
|
||||||
@@ -184,31 +158,20 @@ class _VotePanelState extends State<VotePanel> {
|
|||||||
} else {
|
} else {
|
||||||
groupValue.remove(optidx);
|
groupValue.remove(optidx);
|
||||||
}
|
}
|
||||||
_selectedNum.value = groupValue.length;
|
|
||||||
_canVote.value = groupValue.isNotEmpty;
|
|
||||||
setState(() {});
|
setState(() {});
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildContext() {
|
Widget _buildContext() {
|
||||||
return _embedded
|
return CustomScrollView(
|
||||||
? Column(
|
shrinkWrap: true,
|
||||||
mainAxisSize: MainAxisSize.min,
|
slivers: [
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
SliverList.builder(
|
||||||
children: [
|
itemCount: _voteInfo.options.length,
|
||||||
...List.generate(_voteInfo.options.length, _buildOptions),
|
itemBuilder: (context, index) => _buildOptions(index),
|
||||||
if (_enabled) ..._checkBoxs,
|
),
|
||||||
],
|
if (_enabled) SliverList.list(children: _checkBoxs)
|
||||||
)
|
],
|
||||||
: CustomScrollView(
|
);
|
||||||
shrinkWrap: true,
|
|
||||||
slivers: [
|
|
||||||
SliverList.builder(
|
|
||||||
itemCount: _voteInfo.options.length,
|
|
||||||
itemBuilder: (context, index) => _buildOptions(index),
|
|
||||||
),
|
|
||||||
if (_enabled) SliverList.list(children: _checkBoxs)
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static List<double> _cnt2Percentage(List<Option> options) {
|
static List<double> _cnt2Percentage(List<Option> options) {
|
||||||
@@ -219,7 +182,7 @@ class _VotePanelState extends State<VotePanel> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PercentageChip<T> extends StatelessWidget {
|
class PercentageChip extends StatelessWidget {
|
||||||
final String label;
|
final String label;
|
||||||
final double? percentage;
|
final double? percentage;
|
||||||
final bool selected;
|
final bool selected;
|
||||||
@@ -237,6 +200,7 @@ class PercentageChip<T> extends StatelessWidget {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final colorScheme = Theme.of(context).colorScheme;
|
final colorScheme = Theme.of(context).colorScheme;
|
||||||
return ChoiceChip(
|
return ChoiceChip(
|
||||||
|
tooltip: label,
|
||||||
labelPadding: EdgeInsets.zero,
|
labelPadding: EdgeInsets.zero,
|
||||||
padding: EdgeInsets.zero,
|
padding: EdgeInsets.zero,
|
||||||
showCheckmark: false,
|
showCheckmark: false,
|
||||||
@@ -267,7 +231,7 @@ class PercentageChip<T> extends StatelessWidget {
|
|||||||
Row(
|
Row(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
Text(label),
|
Text(label, maxLines: 1, overflow: TextOverflow.ellipsis),
|
||||||
if (selected)
|
if (selected)
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.only(left: 4),
|
padding: const EdgeInsets.only(left: 4),
|
||||||
@@ -292,54 +256,6 @@ class PercentageChip<T> extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// class VoteCard extends StatefulWidget {
|
|
||||||
// final int voteId;
|
|
||||||
// final bool isSliver;
|
|
||||||
|
|
||||||
// const VoteCard(this.voteId, {super.key, this.isSliver = false});
|
|
||||||
|
|
||||||
// @override
|
|
||||||
// State<VoteCard> createState() => _VoteCardState();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// class _VoteCardState extends State<VoteCard> {
|
|
||||||
// late Future<LoadingState<VoteInfo>> _futureBuilderFuture;
|
|
||||||
|
|
||||||
// @override
|
|
||||||
// void initState() {
|
|
||||||
// super.initState();
|
|
||||||
// _futureBuilderFuture = getInfo();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Future<LoadingState<VoteInfo>> getInfo() =>
|
|
||||||
// DynamicsHttp.voteInfo(widget.voteId);
|
|
||||||
|
|
||||||
// @override
|
|
||||||
// Widget build(BuildContext context) {
|
|
||||||
// return FutureBuilder(
|
|
||||||
// future: _futureBuilderFuture,
|
|
||||||
// builder: (context, snapshot) => switch (snapshot.data) {
|
|
||||||
// Success(response: final res) => VotePanel(
|
|
||||||
// embedded: true,
|
|
||||||
// voteInfo: res,
|
|
||||||
// callback: (votes, anonymity) => DynamicsHttp.doVote(
|
|
||||||
// voteId: widget.voteId,
|
|
||||||
// votes: votes.toList(),
|
|
||||||
// anonymity: anonymity,
|
|
||||||
// )),
|
|
||||||
// Error(errMsg: final msg) => HttpError(
|
|
||||||
// isSliver: widget.isSliver,
|
|
||||||
// errMsg: msg,
|
|
||||||
// onReload: () {
|
|
||||||
// setState(() {
|
|
||||||
// _futureBuilderFuture = getInfo();
|
|
||||||
// });
|
|
||||||
// }),
|
|
||||||
// _ => const SizedBox.shrink()
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
Future showVoteDialog(BuildContext context, voteId, [dynamicId]) async {
|
Future showVoteDialog(BuildContext context, voteId, [dynamicId]) async {
|
||||||
final voteInfo = await DynamicsHttp.voteInfo(voteId);
|
final voteInfo = await DynamicsHttp.voteInfo(voteId);
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
@@ -348,7 +264,7 @@ Future showVoteDialog(BuildContext context, voteId, [dynamicId]) async {
|
|||||||
context: context,
|
context: context,
|
||||||
builder: (context) => AlertDialog(
|
builder: (context) => AlertDialog(
|
||||||
content: SizedBox(
|
content: SizedBox(
|
||||||
width: 120,
|
width: 160,
|
||||||
child: VotePanel(
|
child: VotePanel(
|
||||||
voteInfo: voteInfo.data,
|
voteInfo: voteInfo.data,
|
||||||
callback: (votes, anonymity) => DynamicsHttp.doVote(
|
callback: (votes, anonymity) => DynamicsHttp.doVote(
|
||||||
@@ -361,7 +277,7 @@ Future showVoteDialog(BuildContext context, voteId, [dynamicId]) async {
|
|||||||
),
|
),
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
SmartDialog.showToast((voteInfo as Error).errMsg ?? '');
|
voteInfo.toast();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user