feat(ratings): Wallapop-style ratings in chat and offer detail

- SocialSession grows a ratings transport (fifth interface on the one
  shared channel).
- PeerRatingCubit: count, locale-aware average, and circleCount — how
  many ratings come from your own circle (reuses the ego-centric trust
  rule), the signal that makes strangers' review-stuffing irrelevant.
- Chat: slim rating strip under the trust banner; you can rate only
  someone you've talked with (soft anchor until the signed bilateral
  exchange form lands). Sheet with 5 stars + optional comment,
  pre-filled for editing, with a take-back action.
- Offer detail: author's stars under their name, with 'N from people
  you know' when your circle rated them; nothing shown when unrated.
- i18n ratings.* (en/es/pt/ast); tests for cubit and sheet.
This commit is contained in:
vjrj 2026-07-11 13:13:43 +02:00
parent acdacf1821
commit 1926e3bd98
18 changed files with 873 additions and 12 deletions

View file

@ -15,8 +15,10 @@ import '../services/social_connection.dart';
import '../services/social_service.dart';
import '../services/unread_service.dart';
import '../state/messages_cubit.dart';
import '../state/peer_rating_cubit.dart';
import '../state/trust_cubit.dart';
import 'peer_avatar.dart';
import 'rating_sheet.dart';
import 'theme.dart';
/// A 1:1 encrypted chat with one peer (redesign screen 10). Private and
@ -52,6 +54,7 @@ class ChatScreen extends StatefulWidget {
class _ChatScreenState extends State<ChatScreen> {
MessagesCubit? _messages;
TrustCubit? _trust;
PeerRatingCubit? _rating;
bool _loading = true;
String? _peerName;
String? _peerG1;
@ -94,9 +97,17 @@ class _ChatScreenState extends State<ChatScreen> {
selfPubkey: self,
);
unawaited(trust.load());
final rating = PeerRatingCubit(
session?.ratings,
peerPubkey: widget.peerPubkey,
selfPubkey: self,
trust: session?.trust,
);
unawaited(rating.load());
setState(() {
_messages = messages;
_trust = trust;
_rating = rating;
_peerName = cachedName;
_loading = false;
});
@ -166,6 +177,7 @@ class _ChatScreenState extends State<ChatScreen> {
unawaited(_unread?.markRead(widget.peerPubkey));
_messages?.close();
_trust?.close();
_rating?.close();
// The session belongs to the shared connection don't close it here.
_input.dispose();
super.dispose();
@ -175,6 +187,7 @@ class _ChatScreenState extends State<ChatScreen> {
Widget build(BuildContext context) {
final messages = _messages;
final trust = _trust;
final rating = _rating;
final t = context.t;
return Scaffold(
appBar: AppBar(
@ -189,12 +202,13 @@ class _ChatScreenState extends State<ChatScreen> {
),
],
),
body: _loading || messages == null || trust == null
body: _loading || messages == null || trust == null || rating == null
? const Center(child: CircularProgressIndicator())
: MultiBlocProvider(
providers: [
BlocProvider.value(value: messages),
BlocProvider.value(value: trust),
BlocProvider.value(value: rating),
],
child: _ChatBody(
controller: _input,
@ -244,6 +258,7 @@ class _ChatBody extends StatelessWidget {
child: Column(
children: [
const _TrustBanner(),
const _RatingStrip(),
Expanded(child: ChatMessageList(peerName: peerName)),
_Composer(controller: controller, onSend: onSend),
],
@ -353,6 +368,56 @@ class _TrustBanner extends StatelessWidget {
}
}
/// A slim reputation strip under the trust banner: everyone's stars for this
/// peer (highlighting how many come from people you know) and, once you've
/// actually talked with them, a button to leave or edit your own rating.
class _RatingStrip extends StatelessWidget {
const _RatingStrip();
@override
Widget build(BuildContext context) {
final t = context.t;
return BlocBuilder<PeerRatingCubit, PeerRatingState>(
builder: (context, state) {
final cubit = context.read<PeerRatingCubit>();
if (!cubit.isOnline || state.loading) return const SizedBox.shrink();
// Soft anchor: you can only rate someone you've talked with.
final canRate = context.select<MessagesCubit, bool>(
(m) => m.state.messages.isNotEmpty,
);
if (!state.hasRatings && !canRate) return const SizedBox.shrink();
return Container(
width: double.infinity,
color: seedPrimaryContainer.withValues(alpha: 0.3),
padding: const EdgeInsetsDirectional.fromSTEB(16, 4, 8, 4),
child: Row(
children: [
Expanded(
child: state.hasRatings
? RatingStars(
average: state.average,
count: state.count,
circleCount: state.circleCount,
)
: const SizedBox.shrink(),
),
if (canRate)
TextButton(
key: const Key('chat.rate'),
onPressed: state.busy
? null
: () => showRatingSheet(context, cubit),
child:
Text(state.iRated ? t.ratings.edit : t.ratings.rate),
),
],
),
);
},
);
}
}
/// A centered pill introducing a new day ("Today", "Yesterday", or a
/// locale-formatted date). Localized via [chatDayLabel].
class _DaySeparator extends StatelessWidget {

View file

@ -8,7 +8,10 @@ import 'package:go_router/go_router.dart';
import '../i18n/strings.g.dart';
import '../services/profile_cache.dart';
import '../services/social_connection.dart';
import '../services/social_service.dart';
import '../state/peer_rating_cubit.dart';
import 'market_widgets.dart';
import 'rating_sheet.dart';
import 'theme.dart';
/// Read-only "product page" for one discovered [Offer] (the Wallapop-style
@ -22,6 +25,7 @@ class MarketOfferDetailScreen extends StatefulWidget {
required this.connection,
this.mine = false,
this.profileCache,
this.selfPubkey,
super.key,
});
@ -36,6 +40,10 @@ class MarketOfferDetailScreen extends StatefulWidget {
/// Optional cache of peers' published display names; null in tests.
final ProfileCache? profileCache;
/// This user's key (hex), to weigh the author's ratings by your own circle;
/// null in tests (circle weighting then stays off).
final String? selfPubkey;
@override
State<MarketOfferDetailScreen> createState() =>
_MarketOfferDetailScreenState();
@ -46,6 +54,7 @@ class _MarketOfferDetailScreenState extends State<MarketOfferDetailScreen> {
String? _sellerAbout;
String? _sellerG1;
bool _profileLoading = true;
PeerRatingState? _sellerRating;
@override
void initState() {
@ -65,6 +74,7 @@ class _MarketOfferDetailScreenState extends State<MarketOfferDetailScreen> {
setState(() => _profileLoading = false);
return;
}
unawaited(_loadSellerRating(session));
try {
final profile = await session.profile.fetch(widget.offer.authorPubkeyHex);
if (profile != null && profile.name.isNotEmpty) {
@ -86,6 +96,25 @@ class _MarketOfferDetailScreenState extends State<MarketOfferDetailScreen> {
}
}
/// Best-effort summary of the author's ratings, circle-weighted when we know
/// who this user is. Reuses the cubit's aggregation without providing it.
Future<void> _loadSellerRating(SocialSession session) async {
final cubit = PeerRatingCubit(
session.ratings,
peerPubkey: widget.offer.authorPubkeyHex,
selfPubkey: widget.selfPubkey ?? '',
trust: widget.selfPubkey == null ? null : session.trust,
);
try {
await cubit.load();
if (mounted) setState(() => _sellerRating = cubit.state);
} catch (_) {
// offline / unreachable the seller section simply shows no stars.
} finally {
await cubit.close();
}
}
// No dispose of the session it belongs to the shared connection.
Future<void> _copyId() async {
@ -167,6 +196,7 @@ class _MarketOfferDetailScreenState extends State<MarketOfferDetailScreen> {
title: t.market.sharedBy,
name: _sellerName ?? shortPubkey(o.authorPubkeyHex),
about: _sellerAbout,
rating: _sellerRating,
loading: _profileLoading,
hasProfile:
_sellerName != null || _sellerAbout != null || _sellerG1 != null,
@ -227,11 +257,16 @@ class _SellerSection extends StatelessWidget {
required this.noProfileLabel,
required this.onCopyId,
required this.copyIdTooltip,
this.rating,
});
final String title;
final String name;
final String? about;
/// Circle-weighted summary of the author's ratings; stars show only when
/// someone actually rated them (absence is never a reproach).
final PeerRatingState? rating;
final bool loading;
final bool hasProfile;
final String noProfileLabel;
@ -261,13 +296,26 @@ class _SellerSection extends StatelessWidget {
),
const SizedBox(width: 12),
Expanded(
child: Text(
name,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: seedOnSurface,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
name,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: seedOnSurface,
),
),
if (rating != null && rating!.hasRatings) ...[
const SizedBox(height: 2),
RatingStars(
average: rating!.average,
count: rating!.count,
circleCount: rating!.circleCount,
),
],
],
),
),
IconButton(

View file

@ -0,0 +1,180 @@
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import '../i18n/strings.g.dart';
import '../state/peer_rating_cubit.dart';
import 'theme.dart';
/// A compact "★ 4,5 (12)" pill, plus how many ratings come from people you
/// know the signal that makes strangers' reviews easy to weigh.
class RatingStars extends StatelessWidget {
const RatingStars({
required this.average,
required this.count,
this.circleCount = 0,
super.key,
});
final double average;
final int count;
final int circleCount;
@override
Widget build(BuildContext context) {
final t = context.t;
final locale = Localizations.localeOf(context).toString();
final avg = NumberFormat('0.#', locale).format(average);
return Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
spacing: 6,
children: [
const Icon(Icons.star_rounded, size: 18, color: seedGreen),
Text(
'$avg ($count)',
style: const TextStyle(
color: seedOnSurface,
fontSize: 13,
fontWeight: FontWeight.w600,
),
),
if (circleCount > 0)
Text(
t.ratings.fromYourCircle(n: circleCount),
style: const TextStyle(color: seedGreen, fontSize: 13),
),
],
);
}
}
/// Opens the bottom sheet to leave, edit or take back your rating of the
/// peer behind [cubit]. Pre-filled when you already rated.
Future<void> showRatingSheet(BuildContext context, PeerRatingCubit cubit) {
return showModalBottomSheet<void>(
context: context,
isScrollControlled: true,
builder: (context) => Padding(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom,
),
child: _RatingForm(cubit: cubit),
),
);
}
class _RatingForm extends StatefulWidget {
const _RatingForm({required this.cubit});
final PeerRatingCubit cubit;
@override
State<_RatingForm> createState() => _RatingFormState();
}
class _RatingFormState extends State<_RatingForm> {
late int _stars;
late final TextEditingController _comment;
bool _saving = false;
@override
void initState() {
super.initState();
final state = widget.cubit.state;
_stars = state.myStars ?? 0;
_comment = TextEditingController(text: state.myComment);
}
@override
void dispose() {
_comment.dispose();
super.dispose();
}
Future<void> _save() async {
final messenger = ScaffoldMessenger.of(context);
final navigator = Navigator.of(context);
final saved = context.t.ratings.saved;
setState(() => _saving = true);
await widget.cubit.rate(_stars, comment: _comment.text.trim());
navigator.pop();
messenger.showSnackBar(SnackBar(content: Text(saved)));
}
Future<void> _retract() async {
final navigator = Navigator.of(context);
setState(() => _saving = true);
await widget.cubit.retract();
navigator.pop();
}
@override
Widget build(BuildContext context) {
final t = context.t;
final iRated = widget.cubit.state.iRated;
return Padding(
padding: const EdgeInsets.fromLTRB(24, 20, 24, 24),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
iRated ? t.ratings.edit : t.ratings.rate,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
color: seedTitle,
),
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (var s = 1; s <= 5; s++)
IconButton(
key: Key('rating.star.$s'),
iconSize: 36,
onPressed:
_saving ? null : () => setState(() => _stars = s),
icon: Icon(
s <= _stars
? Icons.star_rounded
: Icons.star_outline_rounded,
color: s <= _stars ? seedGreen : seedMuted,
),
),
],
),
const SizedBox(height: 8),
TextField(
key: const Key('rating.comment'),
controller: _comment,
enabled: !_saving,
maxLength: 280,
maxLines: 2,
decoration: InputDecoration(
hintText: t.ratings.commentHint,
border: const OutlineInputBorder(),
),
),
const SizedBox(height: 8),
Row(
children: [
if (iRated)
TextButton(
key: const Key('rating.retract'),
onPressed: _saving ? null : _retract,
child: Text(t.ratings.retract),
),
const Spacer(),
FilledButton(
key: const Key('rating.save'),
onPressed: _saving || _stars == 0 ? null : _save,
child: Text(t.common.save),
),
],
),
],
),
);
}
}