Ein produktionsreifes Avatar-Widget, das ein Profilbild zeigt, wenn eines vorhanden ist, und sonst sauber auf gestaltete Initialen zurückfällt, wie bei Google, Slack und Microsoft Teams. Derselbe Name bekommt immer dieselbe Farbe.
So sieht es aus
Same name = same color. Always.
Was drin ist
Warum es dieses Widget gibt
Das CircleImage-Widget von FlutterFlow stürzt bei leeren oder kaputten Bild-URLs ab. Dieses Widget löst das: Es zeigt immer etwas Sinnvolles. Mit URL zeigt es das Bild. Ohne URL (oder wenn sie kaputt ist) zeigt es die Initialen. Keine Abstürze, keine leeren Kreise, keine Zusatzlogik.
Einbau
Geh in FlutterFlow zu Custom Code → Custom Widgets → + Create. Nenne es exakt `CustomAvatar`. Setze Standardbreite und -höhe auf `48`.
Füge alle 13 Parameter aus der Tabelle unten hinzu. Nur `firstName` ist Pflicht, alle anderen sind optional und haben sinnvolle Standardwerte.
Füge den kompletten Widget-Code aus dem Abschnitt unten ein. Klick auf Save, FlutterFlow kompiliert ihn automatisch.
Zieh CustomAvatar auf eine beliebige Seite. Übergib `firstName` und optional `lastName` und `imageUrl`. Den Rest erledigt das Widget automatisch.
API-Referenz
| Parameter | Typ | Standard | Beschreibung |
|---|---|---|---|
| firstName | String | — | Vorname (Pflicht), bestimmt Initialen und Farbe |
| lastName | String | — | Nachname (optional), ergänzt die zweite Initiale |
| imageUrl | String | — | URL des Profilbilds, fällt auf Initialen zurück, wenn leer oder defekt |
| size | double | 48 | Durchmesser des Avatars in Pixeln |
| isCircular | bool | true | Runde Form (true) oder abgerundetes Rechteck (false) |
| enableAnimation | bool | false | Einblend-Animation, wenn das Bild lädt |
| borderRadius | double | 8.0 | Eckenradius (nur wenn isCircular false ist) |
| borderColor | Color | transparent | Optionale Rahmenfarbe |
| borderWidth | double | 0 | Rahmenstärke |
| fontSize | double | auto | Schriftgröße der Initialen (skaliert standardmäßig mit der Avatar-Größe) |
| fontWeight | int | 600 | Schriftstärke der Initialen (400–900) |
| textColor | Color | White | Textfarbe der Initialen |
| backgroundColor | Color | auto | Überschreibt die automatisch vergebene Hintergrundfarbe |
Zum Kopieren
CustomAvatar(
firstName: "Brani",
)
// → Shows "B" with deterministic colorCustomAvatar(
firstName: "Brani",
lastName: "Mueller",
)
// → Shows "BM" with deterministic colorCustomAvatar(
firstName: "Jane",
lastName: "Doe",
imageUrl: "https://example.com/avatar.jpg",
)
// → Shows image, falls back to "JD" if brokenCustomAvatar(
firstName: "Jane",
lastName: "Doe",
size: 80,
isCircular: false,
borderRadius: 16,
borderColor: Color(0xFFE0E0E0),
borderWidth: 2,
enableAnimation: true,
)Kompletter Code
Kopiere die ganze Datei in FlutterFlow → Custom Code → Custom Widgets.
// Automatic FlutterFlow imports
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/widgets/index.dart'; // Imports other custom widgets
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom widget code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
class CustomAvatar extends StatefulWidget {
const CustomAvatar({
super.key,
this.width,
this.height,
required this.firstName,
this.lastName,
this.imageUrl,
this.size,
this.isCircular,
this.enableAnimation,
this.borderRadius,
this.borderColor,
this.borderWidth,
this.fontSize,
this.fontWeight,
this.textColor,
this.backgroundColor,
});
final double? width;
final double? height;
final String firstName;
final String? lastName;
final String? imageUrl;
final double? size;
final bool? isCircular;
final bool? enableAnimation;
final double? borderRadius;
final Color? borderColor;
final double? borderWidth;
final double? fontSize;
final int? fontWeight;
final Color? textColor;
final Color? backgroundColor;
@override
State<CustomAvatar> createState() => _CustomAvatarState();
}
class _CustomAvatarState extends State<CustomAvatar> {
// ── Color Palette ────────────────────────────────────────
static const List<Color> _palette = [
Color(0xFF4A90D9), // Blue
Color(0xFF50B86C), // Green
Color(0xFFE85D75), // Rose
Color(0xFFF5A623), // Amber
Color(0xFF9B59B6), // Purple
Color(0xFF1ABC9C), // Teal
Color(0xFFE67E22), // Orange
Color(0xFF3498DB), // Sky Blue
Color(0xFFE74C3C), // Red
Color(0xFF2ECC71), // Emerald
Color(0xFF8E44AD), // Deep Purple
Color(0xFFF39C12), // Sunflower
Color(0xFF16A085), // Dark Teal
Color(0xFFD35400), // Pumpkin
Color(0xFF2980B9), // Strong Blue
Color(0xFFC0392B), // Dark Red
];
String _getInitials() {
final first = widget.firstName.trim();
final last = (widget.lastName ?? '').trim();
if (first.isEmpty && last.isEmpty) return '?';
if (first.isEmpty) return last[0].toUpperCase();
if (last.isEmpty) return first[0].toUpperCase();
return '${first[0]}${last[0]}'.toUpperCase();
}
Color _getColorForName() {
final combined =
'${widget.firstName.trim()} ${(widget.lastName ?? '').trim()}'.trim();
if (combined.isEmpty) return _palette[0];
int hash = 0;
for (int i = 0; i < combined.length; i++) {
hash = combined.codeUnitAt(i) + ((hash << 5) - hash);
}
return _palette[hash.abs() % _palette.length];
}
bool get _hasImage =>
widget.imageUrl != null && widget.imageUrl!.trim().isNotEmpty;
@override
Widget build(BuildContext context) {
final double avatarSize = widget.size ?? 48;
final bool circular = widget.isCircular ?? true;
final double radius =
circular ? avatarSize / 2 : (widget.borderRadius ?? 12);
final double bWidth = widget.borderWidth ?? 0;
final Color bColor = widget.borderColor ?? Colors.transparent;
final Color bgColor = widget.backgroundColor ?? _getColorForName();
final Color fgColor = widget.textColor ?? Colors.white;
final double fSize = widget.fontSize ?? (avatarSize * 0.38);
final FontWeight fWeight =
FontWeight.values[(widget.fontWeight ?? 6).clamp(0, 8)];
final String initials = _getInitials();
final bool animate = widget.enableAnimation ?? false;
return Container(
width: avatarSize,
height: avatarSize,
decoration: BoxDecoration(
color: bgColor,
borderRadius: BorderRadius.circular(radius),
border: bWidth > 0 ? Border.all(color: bColor, width: bWidth) : null,
),
clipBehavior: Clip.antiAlias,
child: _hasImage
? Image.network(
widget.imageUrl!,
width: avatarSize,
height: avatarSize,
fit: BoxFit.cover,
frameBuilder: animate
? (context, child, frame, wasSynchronouslyLoaded) {
if (wasSynchronouslyLoaded) return child;
return AnimatedOpacity(
opacity: frame == null ? 0 : 1,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
child: child,
);
}
: null,
errorBuilder: (context, error, stackTrace) {
return Center(
child: Text(
initials,
style: TextStyle(
color: fgColor,
fontSize: fSize,
fontWeight: fWeight,
letterSpacing: 0.5,
height: 1,
),
textAlign: TextAlign.center,
),
);
},
)
: Center(
child: Text(
initials,
style: TextStyle(
color: fgColor,
fontSize: fSize,
fontWeight: fWeight,
letterSpacing: 0.5,
height: 1,
),
textAlign: TextAlign.center,
),
),
);
}
}⚖MIT — Free to use in personal and commercial projects.
Aus der Praxis
Feste Farben:: Das Widget wählt anhand des Namens eine von 16 abgestimmten Farben. Derselbe Name bekommt immer dieselbe Farbe, kein Zufall, kein Farbchaos in Listen.
Immer firstName übergeben:: Es ist der einzige Pflichtparameter und bestimmt Initialen und Farbe. Schon ein einzelner Buchstabe reicht.
imageUrl darf leer bleiben:: Ist die URL leer oder lädt nicht, fällt das Widget still auf Initialen zurück, ohne kaputte Bild-Icons.
Für Chat-Apps:: Nutze Größe 36–40 für Avatare in Nachrichtenlisten und 56–64 für Profil-Header. Das Widget skaliert in jeder Größe sauber.
Kostenlos und Open Source
Flüssige Shimmer-Ladeanimation für FlutterFlow. Ersetzt langweilige Lade-Spinner ohne Umbau.
Eine schlanke Custom Function für FlutterFlow, die jeden String sicher auf eine maximale Zeichenzahl kürzt und bei Bedarf automatisch „...“ anhängt.
Ein Custom Widget für FlutterFlow, das jedes Child-Widget mit einem frei anpassbaren gepunkteten oder gestrichelten Rahmen umgibt. Der native Container von FlutterFlow kann das nicht, dieses Widget schließt die Lücke.
Ein 3D-Flip-Card-Widget mit flüssigen Animationen für FlutterFlow. Ein Tipp dreht die Karte und zeigt die Rückseite. Farben, Text, Richtung und Tempo sind frei anpassbar.
Du planst eine App?
Wir bauen komplette Apps für Gründer und Unternehmen, mit Festpreis pro Meilenstein. Buch dir ein kostenloses 30-Minuten-Gespräch über deine Idee.
Erstgespräch buchen