feat(profile): default avatars via DiceBear "thumbs" (CC0), drop boring_avatars
Replace the flutter_boring_avatars fallback with a friendly DiceBear "thumbs" face, generated offline (dicebear_core) and deterministically from the pubkey, rendered as SVG (flutter_svg). The thumbs style is CC0 1.0 (public domain) — no attribution, AGPL-clean — unlike Multiavatar whose custom licence restricts use. Style parsed once and per-pubkey SVG memoized; the CC0 <metadata> block is stripped to keep flutter_svg quiet.
This commit is contained in:
parent
1c2b3d5e8d
commit
109c85298c
6 changed files with 153 additions and 45 deletions
|
|
@ -1,27 +1,18 @@
|
|||
import 'package:dicebear_core/dicebear_core.dart' show Avatar, Style;
|
||||
import 'package:dicebear_styles/thumbs.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_boring_avatars/flutter_boring_avatars.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
|
||||
import '../services/offer_thumbnail.dart' show decodeDataUri;
|
||||
import '../services/profile_cache.dart';
|
||||
import 'avatar.dart';
|
||||
import 'seed_glyph.dart';
|
||||
import 'theme.dart';
|
||||
|
||||
/// Palette for the generated default avatars — earthy seed-and-leaf tones so
|
||||
/// the fallback art sits inside the app's palette rather than fighting it.
|
||||
const _generatedAvatarPalette = BoringAvatarPalette([
|
||||
seedGreen,
|
||||
seedRating, // warm amber
|
||||
Color(0xFFB5541F), // terracotta pot
|
||||
Color(0xFF6B4F2A), // seed-coat brown
|
||||
Color(0xFFD3E8C8), // pale leaf
|
||||
]);
|
||||
|
||||
/// A small round avatar for a person. When they've set an avatar ([picture] — a
|
||||
/// `data:` photo thumbnail or a `tane:seed:<glyph>` illustration) it's shown;
|
||||
/// otherwise it falls back to a generated pattern drawn deterministically from
|
||||
/// their [pubkey] (boring-avatars "beam"). Same pubkey → same picture on every
|
||||
/// device, so a person stays visually recognizable without sharing anything.
|
||||
/// otherwise it falls back to a [GeneratedAvatar] drawn deterministically from
|
||||
/// their [pubkey]. Same pubkey → same picture on every device, so a person
|
||||
/// stays visually recognizable without sharing anything.
|
||||
class PeerAvatar extends StatelessWidget {
|
||||
const PeerAvatar({
|
||||
required this.pubkey,
|
||||
|
|
@ -34,7 +25,7 @@ class PeerAvatar extends StatelessWidget {
|
|||
final String pubkey;
|
||||
final String? name;
|
||||
|
||||
/// The person's chosen avatar value; null/empty → the coloured-initial disc.
|
||||
/// The person's chosen avatar value; null/empty → the [GeneratedAvatar].
|
||||
final String? picture;
|
||||
final double radius;
|
||||
|
||||
|
|
@ -58,16 +49,52 @@ class PeerAvatar extends StatelessWidget {
|
|||
}
|
||||
}
|
||||
|
||||
return GeneratedAvatar(pubkey: pubkey, name: name, radius: radius);
|
||||
}
|
||||
}
|
||||
|
||||
/// The `thumbs` DiceBear style, parsed once — [Style.parse] validates the style
|
||||
/// definition against its schema, which is not free to repeat per build.
|
||||
final _thumbsStyle = Style.parse(thumbs);
|
||||
|
||||
/// Rendered-SVG memo keyed by pubkey, so a person's avatar is generated once and
|
||||
/// reused across every rebuild and list row rather than re-rendered each time.
|
||||
final _svgCache = <String, String>{};
|
||||
|
||||
/// Strips the `<metadata>` block (the style's CC0 licence notice) that
|
||||
/// flutter_svg cannot parse and logs a warning for; CC0 requires no attribution
|
||||
/// in the rendered image, and the licence still ships with the package source.
|
||||
final _metadataElement = RegExp(r'<metadata\b[^>]*>.*?</metadata>', dotAll: true);
|
||||
|
||||
String _renderThumbs(String pubkey) =>
|
||||
Avatar(_thumbsStyle, {'seed': pubkey}).svg.replaceAll(_metadataElement, '');
|
||||
|
||||
/// The deterministic default avatar for a person with no chosen [picture]: a
|
||||
/// friendly DiceBear "thumbs" face (CC0) seeded from their [pubkey], so the same
|
||||
/// key yields the same face on every device with nothing shared. Rendered as an
|
||||
/// SVG (no network, no assets). Exposes [pubkey] so call sites/tests can find it.
|
||||
class GeneratedAvatar extends StatelessWidget {
|
||||
const GeneratedAvatar({
|
||||
required this.pubkey,
|
||||
this.name,
|
||||
this.radius = 14,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final String pubkey;
|
||||
final String? name;
|
||||
final double radius;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final svg = _svgCache[pubkey] ??= _renderThumbs(pubkey);
|
||||
return Semantics(
|
||||
label: name,
|
||||
image: true,
|
||||
child: SizedBox.square(
|
||||
dimension: radius * 2,
|
||||
child: BoringAvatar(
|
||||
name: pubkey,
|
||||
type: BoringAvatarType.beam,
|
||||
palette: _generatedAvatarPalette,
|
||||
shape: const OvalBorder(),
|
||||
child: ClipOval(
|
||||
child: SizedBox.square(
|
||||
dimension: radius * 2,
|
||||
child: SvgPicture.string(svg, fit: BoxFit.cover),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue