🐛 Bug fixes
This commit is contained in:
@@ -23,6 +23,7 @@ class PollSubmit extends ConsumerStatefulWidget {
|
||||
super.key,
|
||||
required this.poll,
|
||||
required this.onSubmit,
|
||||
required this.stats,
|
||||
this.initialAnswers,
|
||||
this.onCancel,
|
||||
this.showProgress = true,
|
||||
@@ -35,6 +36,7 @@ class PollSubmit extends ConsumerStatefulWidget {
|
||||
|
||||
/// Optional initial answers, keyed by questionId.
|
||||
final Map<String, dynamic>? initialAnswers;
|
||||
final Map<String, dynamic>? stats;
|
||||
|
||||
/// Optional cancel callback.
|
||||
final VoidCallback? onCancel;
|
||||
@@ -321,6 +323,153 @@ class _PollSubmitState extends ConsumerState<PollSubmit> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStats(BuildContext context, SnPollQuestion q) {
|
||||
if (widget.stats == null) return const SizedBox.shrink();
|
||||
final raw = widget.stats![q.id];
|
||||
if (raw == null) return const SizedBox.shrink();
|
||||
|
||||
Widget? body;
|
||||
|
||||
switch (q.type) {
|
||||
case SnPollQuestionType.rating:
|
||||
// rating: avg score (double or int)
|
||||
final avg = (raw['rating'] as num).toDouble();
|
||||
final theme = Theme.of(context);
|
||||
body = Row(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
children: [
|
||||
Icon(Icons.star, color: Colors.amber.shade600, size: 18),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
avg.toStringAsFixed(1),
|
||||
style: theme.textTheme.labelMedium?.copyWith(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
break;
|
||||
|
||||
case SnPollQuestionType.yesNo:
|
||||
// yes/no: map {true: count, false: count}
|
||||
if (raw is Map) {
|
||||
final int yes =
|
||||
(raw[true] is int)
|
||||
? raw[true] as int
|
||||
: int.tryParse('${raw[true]}') ?? 0;
|
||||
final int no =
|
||||
(raw[false] is int)
|
||||
? raw[false] as int
|
||||
: int.tryParse('${raw[false]}') ?? 0;
|
||||
final total = (yes + no).clamp(0, 1 << 31);
|
||||
final yesPct = total == 0 ? 0.0 : yes / total;
|
||||
final noPct = total == 0 ? 0.0 : no / total;
|
||||
final theme = Theme.of(context);
|
||||
body = Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_BarStatRow(
|
||||
label: 'Yes',
|
||||
count: yes,
|
||||
fraction: yesPct,
|
||||
color: Colors.green.shade600,
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
_BarStatRow(
|
||||
label: 'No',
|
||||
count: no,
|
||||
fraction: noPct,
|
||||
color: Colors.red.shade600,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'Total: $total',
|
||||
style: theme.textTheme.labelSmall?.copyWith(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
break;
|
||||
|
||||
case SnPollQuestionType.singleChoice:
|
||||
case SnPollQuestionType.multipleChoice:
|
||||
// map optionId -> count
|
||||
if (raw is Map) {
|
||||
final options = [...?q.options]
|
||||
..sort((a, b) => a.order.compareTo(b.order));
|
||||
final List<_OptionCount> items = [];
|
||||
int total = 0;
|
||||
for (final opt in options) {
|
||||
final dynamic v = raw[opt.id];
|
||||
final int count = v is int ? v : int.tryParse('$v') ?? 0;
|
||||
total += count;
|
||||
items.add(_OptionCount(id: opt.id, label: opt.label, count: count));
|
||||
}
|
||||
if (items.isNotEmpty) {
|
||||
items.sort(
|
||||
(a, b) => b.count.compareTo(a.count),
|
||||
); // show highest first
|
||||
}
|
||||
body = Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
for (final it in items)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 6),
|
||||
child: _BarStatRow(
|
||||
label: it.label,
|
||||
count: it.count,
|
||||
fraction: total == 0 ? 0 : it.count / total,
|
||||
),
|
||||
),
|
||||
if (items.isNotEmpty)
|
||||
Text(
|
||||
'Total: $total',
|
||||
style: Theme.of(context).textTheme.labelSmall?.copyWith(
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
break;
|
||||
|
||||
case SnPollQuestionType.freeText:
|
||||
// No stats
|
||||
break;
|
||||
}
|
||||
|
||||
if (body == null) return const SizedBox.shrink();
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.35),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Stats',
|
||||
style: Theme.of(context).textTheme.labelLarge?.copyWith(
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
body,
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBody(BuildContext context) {
|
||||
final q = _current;
|
||||
switch (q.type) {
|
||||
@@ -467,7 +616,13 @@ class _PollSubmitState extends ConsumerState<PollSubmit> {
|
||||
children: [
|
||||
_buildHeader(context),
|
||||
const SizedBox(height: 12),
|
||||
_AnimatedStep(key: ValueKey(_current.id), child: _buildBody(context)),
|
||||
_AnimatedStep(
|
||||
key: ValueKey(_current.id),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [_buildBody(context), _buildStats(context, _current)],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildNavBar(context),
|
||||
],
|
||||
@@ -475,6 +630,77 @@ class _PollSubmitState extends ConsumerState<PollSubmit> {
|
||||
}
|
||||
}
|
||||
|
||||
class _OptionCount {
|
||||
final String id;
|
||||
final String label;
|
||||
final int count;
|
||||
const _OptionCount({
|
||||
required this.id,
|
||||
required this.label,
|
||||
required this.count,
|
||||
});
|
||||
}
|
||||
|
||||
class _BarStatRow extends StatelessWidget {
|
||||
const _BarStatRow({
|
||||
required this.label,
|
||||
required this.count,
|
||||
required this.fraction,
|
||||
this.color,
|
||||
});
|
||||
|
||||
final String label;
|
||||
final int count;
|
||||
final double fraction;
|
||||
final Color? color;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final barColor = color ?? Theme.of(context).colorScheme.primary;
|
||||
final bgColor = Theme.of(
|
||||
context,
|
||||
).colorScheme.surfaceVariant.withOpacity(0.6);
|
||||
final fg =
|
||||
(fraction.isNaN || fraction.isInfinite)
|
||||
? 0.0
|
||||
: fraction.clamp(0.0, 1.0);
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('$label · $count', style: Theme.of(context).textTheme.labelMedium),
|
||||
const SizedBox(height: 4),
|
||||
LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final width = constraints.maxWidth;
|
||||
final filled = width * fg;
|
||||
return Stack(
|
||||
children: [
|
||||
Container(
|
||||
height: 8,
|
||||
width: width,
|
||||
decoration: BoxDecoration(
|
||||
color: bgColor,
|
||||
borderRadius: BorderRadius.circular(999),
|
||||
),
|
||||
),
|
||||
Container(
|
||||
height: 8,
|
||||
width: filled,
|
||||
decoration: BoxDecoration(
|
||||
color: barColor,
|
||||
borderRadius: BorderRadius.circular(999),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Simple fade/slide transition between questions.
|
||||
class _AnimatedStep extends StatelessWidget {
|
||||
const _AnimatedStep({super.key, required this.child});
|
||||
|
@@ -566,9 +566,10 @@ class PostItem extends HookConsumerWidget {
|
||||
),
|
||||
child: PollSubmit(
|
||||
initialAnswers: embedData['poll']?['user_answer']?['answer'],
|
||||
stats: embedData['poll']?['stats'],
|
||||
poll: SnPollWithStats.fromJson(embedData['poll']),
|
||||
onSubmit: (_) {},
|
||||
).padding(horizontal: 12, vertical: 8),
|
||||
).padding(horizontal: 16, vertical: 12),
|
||||
),
|
||||
_ => const Placeholder(),
|
||||
},
|
||||
|
Reference in New Issue
Block a user