feat(invite): 添加邀请码功能模块
This commit is contained in:
@@ -0,0 +1,521 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../../../../l10n/app_localizations.dart';
|
||||
import '../../../../shared/theme/app_color_palette.dart';
|
||||
import '../../../../shared/theme/design_tokens.dart';
|
||||
import '../../../../shared/widgets/toast/toast.dart';
|
||||
import '../../../../shared/widgets/toast/toast_type.dart';
|
||||
|
||||
class InviteScreen extends StatefulWidget {
|
||||
const InviteScreen({super.key});
|
||||
|
||||
@override
|
||||
State<InviteScreen> createState() => _InviteScreenState();
|
||||
}
|
||||
|
||||
class _InviteScreenState extends State<InviteScreen> {
|
||||
final _bindCodeController = TextEditingController();
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
bool _isBinding = false;
|
||||
bool _isGenerating = false;
|
||||
|
||||
// Mock data - will be replaced with API calls
|
||||
final String _myInviteCode = 'ABC123';
|
||||
final int _invitedCount = 3;
|
||||
final bool _hasInviter = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_bindCodeController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
bool get _hasMyInviteCode => _myInviteCode.isNotEmpty;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: colors.surfaceContainerLow,
|
||||
appBar: AppBar(
|
||||
title: Text(l10n.settingsInviteTitle),
|
||||
centerTitle: true,
|
||||
backgroundColor: colors.surfaceContainerLow,
|
||||
surfaceTintColor: colors.surfaceContainerLow,
|
||||
),
|
||||
body: ListView(
|
||||
padding: const EdgeInsets.all(AppSpacing.lg),
|
||||
children: [
|
||||
if (_hasMyInviteCode) ...[
|
||||
_InviteCodeCard(inviteCode: _myInviteCode, onCopy: _copyInviteCode),
|
||||
const SizedBox(height: AppSpacing.lg),
|
||||
_InviteStatsCard(count: _invitedCount),
|
||||
const SizedBox(height: AppSpacing.xl),
|
||||
_BindCodeSection(
|
||||
controller: _bindCodeController,
|
||||
formKey: _formKey,
|
||||
isBinding: _isBinding,
|
||||
onBind: _bindInviteCode,
|
||||
),
|
||||
] else ...[
|
||||
_EmptyStateCard(
|
||||
controller: _bindCodeController,
|
||||
formKey: _formKey,
|
||||
isBinding: _isBinding,
|
||||
isGenerating: _isGenerating,
|
||||
hasInviter: _hasInviter,
|
||||
onBind: _bindInviteCode,
|
||||
onGenerate: _generateInviteCode,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _copyInviteCode() {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
Clipboard.setData(ClipboardData(text: _myInviteCode));
|
||||
Toast.show(
|
||||
context,
|
||||
l10n.settingsInviteCopySuccess,
|
||||
type: ToastType.success,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _bindInviteCode() async {
|
||||
if (!_formKey.currentState!.validate()) return;
|
||||
|
||||
setState(() => _isBinding = true);
|
||||
|
||||
// Simulate API call
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
Toast.show(
|
||||
context,
|
||||
l10n.settingsInviteBindSuccess,
|
||||
type: ToastType.success,
|
||||
);
|
||||
|
||||
setState(() => _isBinding = false);
|
||||
}
|
||||
|
||||
Future<void> _generateInviteCode() async {
|
||||
setState(() => _isGenerating = true);
|
||||
|
||||
// Simulate API call
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
Toast.show(
|
||||
context,
|
||||
l10n.settingsInviteGenerateSuccess,
|
||||
type: ToastType.success,
|
||||
);
|
||||
|
||||
setState(() => _isGenerating = false);
|
||||
}
|
||||
}
|
||||
|
||||
class _InviteCodeCard extends StatelessWidget {
|
||||
const _InviteCodeCard({required this.inviteCode, required this.onCopy});
|
||||
|
||||
final String inviteCode;
|
||||
final VoidCallback onCopy;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
final palette = Theme.of(context).extension<AppColorPalette>()!;
|
||||
|
||||
return Stack(
|
||||
children: [
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(AppSpacing.xl),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(AppRadius.xl),
|
||||
gradient: LinearGradient(
|
||||
colors: [colors.primary, palette.accentPurple],
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.card_giftcard_rounded,
|
||||
color: colors.onPrimary,
|
||||
size: 40,
|
||||
),
|
||||
const SizedBox(height: AppSpacing.md),
|
||||
Text(
|
||||
l10n.settingsInviteMyCode,
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: colors.onPrimary.withValues(alpha: 0.88),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: AppSpacing.md),
|
||||
Text(
|
||||
inviteCode,
|
||||
style: Theme.of(context).textTheme.headlineLarge?.copyWith(
|
||||
color: colors.onPrimary,
|
||||
fontWeight: FontWeight.w700,
|
||||
letterSpacing: 6,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
top: AppSpacing.sm,
|
||||
right: AppSpacing.sm,
|
||||
child: Material(
|
||||
color: colors.onPrimary.withValues(alpha: 0.18),
|
||||
borderRadius: BorderRadius.circular(AppRadius.full),
|
||||
child: InkWell(
|
||||
onTap: onCopy,
|
||||
borderRadius: BorderRadius.circular(AppRadius.full),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(AppSpacing.sm),
|
||||
child: Icon(
|
||||
Icons.copy_rounded,
|
||||
color: colors.onPrimary,
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _InviteStatsCard extends StatelessWidget {
|
||||
const _InviteStatsCard({required this.count});
|
||||
|
||||
final int count;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
|
||||
return Card(
|
||||
margin: EdgeInsets.zero,
|
||||
elevation: 0,
|
||||
color: colors.surface,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(AppRadius.xl),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(AppSpacing.lg),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 48,
|
||||
height: 48,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.primary.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(AppRadius.md),
|
||||
),
|
||||
child: Icon(Icons.people_outline_rounded, color: colors.primary),
|
||||
),
|
||||
const SizedBox(width: AppSpacing.md),
|
||||
Expanded(
|
||||
child: Text(
|
||||
l10n.settingsInviteStats(count),
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _BindCodeSection extends StatelessWidget {
|
||||
const _BindCodeSection({
|
||||
required this.controller,
|
||||
required this.formKey,
|
||||
required this.isBinding,
|
||||
required this.onBind,
|
||||
});
|
||||
|
||||
final TextEditingController controller;
|
||||
final GlobalKey<FormState> formKey;
|
||||
final bool isBinding;
|
||||
final Future<void> Function() onBind;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
|
||||
return Card(
|
||||
margin: EdgeInsets.zero,
|
||||
elevation: 0,
|
||||
color: colors.surface,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(AppRadius.xl),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(AppSpacing.lg),
|
||||
child: Form(
|
||||
key: formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(Icons.link_rounded, color: colors.primary, size: 20),
|
||||
const SizedBox(width: AppSpacing.sm),
|
||||
Text(
|
||||
l10n.settingsInviteBindCode,
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: AppSpacing.md),
|
||||
TextFormField(
|
||||
controller: controller,
|
||||
textCapitalization: TextCapitalization.characters,
|
||||
decoration: InputDecoration(
|
||||
hintText: l10n.settingsInviteBindPlaceholder,
|
||||
filled: true,
|
||||
fillColor: colors.surfaceContainerHighest,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(AppRadius.md),
|
||||
borderSide: BorderSide.none,
|
||||
),
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: AppSpacing.md,
|
||||
vertical: AppSpacing.md,
|
||||
),
|
||||
),
|
||||
inputFormatters: [
|
||||
FilteringTextInputFormatter.allow(RegExp(r'[A-Za-z0-9]')),
|
||||
LengthLimitingTextInputFormatter(6),
|
||||
],
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return null; // Optional field
|
||||
}
|
||||
if (value.length != 6) {
|
||||
return l10n.settingsInviteInvalidCode;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: AppSpacing.md),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: FilledButton(
|
||||
onPressed: isBinding ? null : onBind,
|
||||
style: FilledButton.styleFrom(
|
||||
elevation: 0,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: AppSpacing.md,
|
||||
),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(AppRadius.full),
|
||||
),
|
||||
),
|
||||
child: isBinding
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
color: Colors.white,
|
||||
),
|
||||
)
|
||||
: Text(l10n.settingsInviteBindButton),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _EmptyStateCard extends StatelessWidget {
|
||||
const _EmptyStateCard({
|
||||
required this.controller,
|
||||
required this.formKey,
|
||||
required this.isBinding,
|
||||
required this.isGenerating,
|
||||
required this.hasInviter,
|
||||
required this.onBind,
|
||||
required this.onGenerate,
|
||||
});
|
||||
|
||||
final TextEditingController controller;
|
||||
final GlobalKey<FormState> formKey;
|
||||
final bool isBinding;
|
||||
final bool isGenerating;
|
||||
final bool hasInviter;
|
||||
final Future<void> Function() onBind;
|
||||
final Future<void> Function() onGenerate;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
final palette = Theme.of(context).extension<AppColorPalette>()!;
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(AppSpacing.xxl),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface,
|
||||
borderRadius: BorderRadius.circular(AppRadius.xl),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Container(
|
||||
width: 80,
|
||||
height: 80,
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
colors.primary.withValues(alpha: 0.1),
|
||||
palette.accentPurple.withValues(alpha: 0.1),
|
||||
],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(AppRadius.xl),
|
||||
),
|
||||
child: Icon(
|
||||
Icons.celebration_rounded,
|
||||
color: colors.primary,
|
||||
size: 40,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: AppSpacing.lg),
|
||||
Text(
|
||||
l10n.settingsInviteEmptyTitle,
|
||||
style: Theme.of(
|
||||
context,
|
||||
).textTheme.titleLarge?.copyWith(fontWeight: FontWeight.w700),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: AppSpacing.sm),
|
||||
Text(
|
||||
l10n.settingsInviteEmptyDescription,
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: colors.onSurfaceVariant,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: AppSpacing.lg),
|
||||
if (!hasInviter) ...[
|
||||
Card(
|
||||
margin: EdgeInsets.zero,
|
||||
elevation: 0,
|
||||
color: colors.surface,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(AppRadius.xl),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(AppSpacing.lg),
|
||||
child: Form(
|
||||
key: formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
l10n.settingsInviteInputLabel,
|
||||
style: Theme.of(context).textTheme.titleSmall?.copyWith(
|
||||
color: colors.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: AppSpacing.sm),
|
||||
TextFormField(
|
||||
controller: controller,
|
||||
textCapitalization: TextCapitalization.characters,
|
||||
decoration: InputDecoration(
|
||||
hintText: l10n.settingsInviteInputHint,
|
||||
filled: true,
|
||||
fillColor: colors.surfaceContainerHighest,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(AppRadius.md),
|
||||
borderSide: BorderSide.none,
|
||||
),
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: AppSpacing.md,
|
||||
vertical: AppSpacing.md,
|
||||
),
|
||||
),
|
||||
inputFormatters: [
|
||||
FilteringTextInputFormatter.allow(
|
||||
RegExp(r'[A-Za-z0-9]'),
|
||||
),
|
||||
LengthLimitingTextInputFormatter(6),
|
||||
],
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
if (value.length != 6) {
|
||||
return l10n.settingsInviteInvalidCode;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: AppSpacing.md),
|
||||
],
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: FilledButton(
|
||||
onPressed: isGenerating ? null : onGenerate,
|
||||
style: FilledButton.styleFrom(
|
||||
elevation: 0,
|
||||
padding: const EdgeInsets.symmetric(vertical: AppSpacing.md),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(AppRadius.full),
|
||||
),
|
||||
),
|
||||
child: isGenerating
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
color: Colors.white,
|
||||
),
|
||||
)
|
||||
: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.add_rounded, size: 20),
|
||||
const SizedBox(width: AppSpacing.sm),
|
||||
Text(l10n.settingsInviteGenerateButton),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user