598 lines
24 KiB
Dart
598 lines
24 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import '../../../core/responsive/responsive_layout.dart';
|
|
import '../../../core/theme/app_typography.dart';
|
|
import '../../../core/theme/theme_extensions.dart';
|
|
import '../../../services/auth/auth_providers.dart';
|
|
import '../../../services/clubs/club_models.dart';
|
|
import '../../../services/clubs/club_providers.dart';
|
|
import '../../../services/clubs/club_service.dart';
|
|
import 'widgets/slug_input_field.dart';
|
|
|
|
/// Screen allowing authenticated riders to create a new Club.
|
|
/// Features live slug availability verification and atomic creation.
|
|
class CreateClubScreen extends ConsumerStatefulWidget {
|
|
const CreateClubScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<CreateClubScreen> createState() => _CreateClubScreenState();
|
|
}
|
|
|
|
class _CreateClubScreenState extends ConsumerState<CreateClubScreen> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _nameController = TextEditingController();
|
|
final _slugController = TextEditingController();
|
|
final _descController = TextEditingController();
|
|
final _regionController = TextEditingController();
|
|
final _focusController = TextEditingController();
|
|
|
|
ClubVisibility _visibility = ClubVisibility.public;
|
|
ClubAdmissionPolicy _admissionPolicy = ClubAdmissionPolicy.request;
|
|
|
|
bool _isSlugValidAndAvailable = false;
|
|
bool _isSubmitting = false;
|
|
String? _submissionError;
|
|
|
|
@override
|
|
void dispose() {
|
|
_nameController.dispose();
|
|
_slugController.dispose();
|
|
_descController.dispose();
|
|
_regionController.dispose();
|
|
_focusController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _submitForm() async {
|
|
if (_isSubmitting) return;
|
|
|
|
if (!_formKey.currentState!.validate()) return;
|
|
|
|
if (!_isSlugValidAndAvailable) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Please choose a valid and available club handle.'),
|
|
behavior: SnackBarBehavior.floating,
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_isSubmitting = true;
|
|
_submissionError = null;
|
|
});
|
|
|
|
try {
|
|
final user = ref.read(currentUserProvider);
|
|
if (user == null || user.isAnonymous) {
|
|
throw const ClubOperationException(
|
|
'You must be signed in with a registered account to create a club.',
|
|
code: 'unauthenticated',
|
|
);
|
|
}
|
|
|
|
final service = ref.read(clubServiceProvider);
|
|
final createdClub = await service.createClub(
|
|
name: _nameController.text.trim(),
|
|
slug: _slugController.text.trim(),
|
|
description: _descController.text.trim(),
|
|
visibility: _visibility,
|
|
admissionPolicy: _admissionPolicy,
|
|
homeRegion: _regionController.text.trim().isEmpty ? null : _regionController.text.trim(),
|
|
motorcycleFocus: _focusController.text.trim().isEmpty ? null : _focusController.text.trim(),
|
|
callerUid: user.uid,
|
|
);
|
|
|
|
// Invalidate relevant providers to reflect new club
|
|
ref.invalidate(myClubsProvider);
|
|
ref.invalidate(publicClubsProvider);
|
|
|
|
if (!mounted) return;
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Club "${createdClub.name}" created successfully!'),
|
|
behavior: SnackBarBehavior.floating,
|
|
),
|
|
);
|
|
|
|
context.pushReplacement('/social/clubs/${createdClub.id}');
|
|
} on ClubOperationException catch (e) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_isSubmitting = false;
|
|
if (e.code == 'slug-taken') {
|
|
_submissionError = 'This handle was just taken by another club. Please pick a different handle.';
|
|
} else {
|
|
_submissionError = e.message;
|
|
}
|
|
});
|
|
} catch (e) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_isSubmitting = false;
|
|
_submissionError = 'An unexpected error occurred while creating the club: $e';
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final colors = context.motorColors;
|
|
final theme = Theme.of(context);
|
|
final user = ref.watch(currentUserProvider);
|
|
final isAuthenticated = user != null && !user.isAnonymous;
|
|
|
|
if (!isAuthenticated) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Create Club')),
|
|
body: Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(24.0),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Icon(Icons.shield_outlined, size: 48, color: colors.cyberCyan),
|
|
const SizedBox(height: 16),
|
|
const Text(
|
|
'Account Required',
|
|
style: TextStyle(
|
|
fontFamily: AppTypography.fontHeadline,
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.w700,
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
'Sign in or create a MotorSignal account to join and manage riding clubs.',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
fontFamily: AppTypography.fontBody,
|
|
fontSize: 13,
|
|
color: theme.colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
FilledButton.icon(
|
|
onPressed: () => context.push('/settings/account'),
|
|
style: FilledButton.styleFrom(
|
|
backgroundColor: colors.cyberCyan,
|
|
foregroundColor: Colors.black,
|
|
),
|
|
icon: const Icon(Icons.login, size: 18),
|
|
label: const Text('Sign In / Register'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text(
|
|
'Create Club',
|
|
style: TextStyle(
|
|
fontFamily: AppTypography.fontHeadline,
|
|
fontWeight: FontWeight.w700,
|
|
),
|
|
),
|
|
),
|
|
body: SafeArea(
|
|
child: ResponsiveLayout(
|
|
builder: (context, responsive) {
|
|
return Center(
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: 700),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: ListView(
|
|
padding: const EdgeInsets.all(18.0),
|
|
children: [
|
|
// Header card explaining Club identity
|
|
Container(
|
|
padding: const EdgeInsets.all(14.0),
|
|
decoration: BoxDecoration(
|
|
color: colors.surfaceContainer,
|
|
borderRadius: BorderRadius.circular(12),
|
|
border: Border.all(color: colors.outlineVariant, width: 0.8),
|
|
),
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Icon(Icons.shield_outlined, color: colors.cyberCyan, size: 24),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text(
|
|
'Found a Motorcycle Club',
|
|
style: TextStyle(
|
|
fontFamily: AppTypography.fontHeadline,
|
|
fontSize: 14,
|
|
fontWeight: FontWeight.w700,
|
|
),
|
|
),
|
|
const SizedBox(height: 3),
|
|
Text(
|
|
'As the founder, you will be designated the Club Owner with administrative authority over admissions, group rides, and club settings.',
|
|
style: TextStyle(
|
|
fontFamily: AppTypography.fontBody,
|
|
fontSize: 12,
|
|
color: theme.colorScheme.onSurfaceVariant,
|
|
height: 1.3,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
|
|
// Submission error alert if present
|
|
if (_submissionError != null) ...[
|
|
Container(
|
|
padding: const EdgeInsets.all(12),
|
|
decoration: BoxDecoration(
|
|
color: colors.raceRed.withValues(alpha: 0.12),
|
|
borderRadius: BorderRadius.circular(10),
|
|
border: Border.all(color: colors.raceRed.withValues(alpha: 0.4)),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
Icon(Icons.error_outline, color: colors.raceRed, size: 20),
|
|
const SizedBox(width: 8),
|
|
Expanded(
|
|
child: Text(
|
|
_submissionError!,
|
|
style: TextStyle(
|
|
fontFamily: AppTypography.fontBody,
|
|
fontSize: 12.5,
|
|
color: colors.raceRed,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
],
|
|
|
|
// Club Name
|
|
TextFormField(
|
|
controller: _nameController,
|
|
enabled: !_isSubmitting,
|
|
textCapitalization: TextCapitalization.words,
|
|
decoration: InputDecoration(
|
|
labelText: 'Club Name *',
|
|
hintText: 'e.g. Ridge Runners MC',
|
|
filled: true,
|
|
fillColor: colors.surfaceContainer,
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide(color: colors.outlineVariant, width: 0.8),
|
|
),
|
|
enabledBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide(color: colors.outlineVariant, width: 0.8),
|
|
),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide(color: colors.cyberCyan, width: 1.5),
|
|
),
|
|
),
|
|
validator: (val) {
|
|
final clean = val?.trim() ?? '';
|
|
if (clean.length < 3) return 'Name must be at least 3 characters';
|
|
if (clean.length > 50) return 'Name cannot exceed 50 characters';
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 18),
|
|
|
|
// Slug / Handle with live debounce & availability checking
|
|
SlugInputField(
|
|
controller: _slugController,
|
|
enabled: !_isSubmitting,
|
|
onValidationChanged: (isValid) {
|
|
setState(() {
|
|
_isSlugValidAndAvailable = isValid;
|
|
});
|
|
},
|
|
),
|
|
const SizedBox(height: 18),
|
|
|
|
// Description
|
|
TextFormField(
|
|
controller: _descController,
|
|
enabled: !_isSubmitting,
|
|
maxLines: 3,
|
|
maxLength: 500,
|
|
textCapitalization: TextCapitalization.sentences,
|
|
decoration: InputDecoration(
|
|
labelText: 'Description (Optional)',
|
|
hintText: 'Share your club mission, riding style, or club guidelines...',
|
|
filled: true,
|
|
fillColor: colors.surfaceContainer,
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide(color: colors.outlineVariant, width: 0.8),
|
|
),
|
|
enabledBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide(color: colors.outlineVariant, width: 0.8),
|
|
),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide(color: colors.cyberCyan, width: 1.5),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 14),
|
|
|
|
// Home Region & Focus Row
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: TextFormField(
|
|
controller: _regionController,
|
|
enabled: !_isSubmitting,
|
|
textCapitalization: TextCapitalization.words,
|
|
decoration: InputDecoration(
|
|
labelText: 'Home Region',
|
|
hintText: 'e.g. NorCal',
|
|
prefixIcon: const Icon(Icons.location_on_outlined, size: 18),
|
|
filled: true,
|
|
fillColor: colors.surfaceContainer,
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide(color: colors.outlineVariant, width: 0.8),
|
|
),
|
|
enabledBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide(color: colors.outlineVariant, width: 0.8),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: TextFormField(
|
|
controller: _focusController,
|
|
enabled: !_isSubmitting,
|
|
textCapitalization: TextCapitalization.words,
|
|
decoration: InputDecoration(
|
|
labelText: 'Motorcycle Focus',
|
|
hintText: 'e.g. Touring, Adventure, Commuting',
|
|
prefixIcon: const Icon(Icons.two_wheeler_outlined, size: 18),
|
|
filled: true,
|
|
fillColor: colors.surfaceContainer,
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide(color: colors.outlineVariant, width: 0.8),
|
|
),
|
|
enabledBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide(color: colors.outlineVariant, width: 0.8),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 24),
|
|
|
|
// Visibility Selection
|
|
Text(
|
|
'Club Visibility',
|
|
style: TextStyle(
|
|
fontFamily: AppTypography.fontHeadline,
|
|
fontSize: 13,
|
|
fontWeight: FontWeight.w700,
|
|
color: colors.cyberCyan,
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
_buildVisibilitySelector(colors, theme),
|
|
const SizedBox(height: 20),
|
|
|
|
// Admission Policy Selection
|
|
Text(
|
|
'Admission Policy',
|
|
style: TextStyle(
|
|
fontFamily: AppTypography.fontHeadline,
|
|
fontSize: 13,
|
|
fontWeight: FontWeight.w700,
|
|
color: colors.cyberCyan,
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
_buildAdmissionSelector(colors, theme),
|
|
const SizedBox(height: 32),
|
|
|
|
// Submit Button
|
|
SizedBox(
|
|
height: 50,
|
|
child: FilledButton(
|
|
key: const ValueKey('create_club_submit_button'),
|
|
onPressed: (_isSubmitting || !_isSlugValidAndAvailable)
|
|
? null
|
|
: _submitForm,
|
|
style: FilledButton.styleFrom(
|
|
backgroundColor: colors.cyberCyan,
|
|
foregroundColor: Colors.black,
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
|
),
|
|
child: _isSubmitting
|
|
? const SizedBox(
|
|
width: 24,
|
|
height: 24,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2.5,
|
|
valueColor: AlwaysStoppedAnimation<Color>(Colors.black),
|
|
),
|
|
)
|
|
: const Text(
|
|
'Create Club',
|
|
style: TextStyle(
|
|
fontFamily: AppTypography.fontHeadline,
|
|
fontSize: 15,
|
|
fontWeight: FontWeight.w800,
|
|
letterSpacing: 0.3,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 32),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildVisibilitySelector(MotorSignalThemeColors colors, ThemeData theme) {
|
|
return Column(
|
|
children: [
|
|
_buildChoiceTile(
|
|
title: 'Public',
|
|
subtitle: 'Discoverable in search and public directory. Any rider can find the club.',
|
|
selected: _visibility == ClubVisibility.public,
|
|
colors: colors,
|
|
theme: theme,
|
|
onTap: () => setState(() => _visibility = ClubVisibility.public),
|
|
),
|
|
const SizedBox(height: 8),
|
|
_buildChoiceTile(
|
|
title: 'Unlisted',
|
|
subtitle: 'Hidden from public discovery. Accessible via direct handle or invitation.',
|
|
selected: _visibility == ClubVisibility.unlisted,
|
|
colors: colors,
|
|
theme: theme,
|
|
onTap: () => setState(() => _visibility = ClubVisibility.unlisted),
|
|
),
|
|
const SizedBox(height: 8),
|
|
_buildChoiceTile(
|
|
title: 'Private',
|
|
subtitle: 'Completely hidden. Visible only to active members and leadership.',
|
|
selected: _visibility == ClubVisibility.private,
|
|
colors: colors,
|
|
theme: theme,
|
|
onTap: () => setState(() => _visibility = ClubVisibility.private),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildAdmissionSelector(MotorSignalThemeColors colors, ThemeData theme) {
|
|
return Column(
|
|
children: [
|
|
_buildChoiceTile(
|
|
title: 'Request to Join (Recommended)',
|
|
subtitle: 'Riders submit a join request for leadership review before becoming active members.',
|
|
selected: _admissionPolicy == ClubAdmissionPolicy.request,
|
|
colors: colors,
|
|
theme: theme,
|
|
onTap: () => setState(() => _admissionPolicy = ClubAdmissionPolicy.request),
|
|
),
|
|
const SizedBox(height: 8),
|
|
_buildChoiceTile(
|
|
title: 'Open to Join',
|
|
subtitle: 'Any rider can instantly join and become an active member without prior approval.',
|
|
selected: _admissionPolicy == ClubAdmissionPolicy.open,
|
|
colors: colors,
|
|
theme: theme,
|
|
onTap: () => setState(() => _admissionPolicy = ClubAdmissionPolicy.open),
|
|
),
|
|
const SizedBox(height: 8),
|
|
_buildChoiceTile(
|
|
title: 'Invite Only',
|
|
subtitle: 'Riders cannot submit join requests. Only club leadership can invite members.',
|
|
selected: _admissionPolicy == ClubAdmissionPolicy.inviteOnly,
|
|
colors: colors,
|
|
theme: theme,
|
|
onTap: () => setState(() => _admissionPolicy = ClubAdmissionPolicy.inviteOnly),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildChoiceTile({
|
|
required String title,
|
|
required String subtitle,
|
|
required bool selected,
|
|
required MotorSignalThemeColors colors,
|
|
required ThemeData theme,
|
|
required VoidCallback onTap,
|
|
}) {
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
color: selected
|
|
? colors.cyberCyan.withValues(alpha: 0.08)
|
|
: colors.surfaceContainer,
|
|
borderRadius: BorderRadius.circular(12),
|
|
border: Border.all(
|
|
color: selected ? colors.cyberCyan : colors.outlineVariant,
|
|
width: selected ? 1.5 : 0.8,
|
|
),
|
|
),
|
|
child: Material(
|
|
color: Colors.transparent,
|
|
child: InkWell(
|
|
borderRadius: BorderRadius.circular(12),
|
|
onTap: _isSubmitting ? null : onTap,
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
|
|
child: Row(
|
|
children: [
|
|
Icon(
|
|
selected ? Icons.radio_button_checked : Icons.radio_button_off,
|
|
color: selected ? colors.cyberCyan : colors.outline,
|
|
size: 20,
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
title,
|
|
style: TextStyle(
|
|
fontFamily: AppTypography.fontHeadline,
|
|
fontSize: 13.5,
|
|
fontWeight: FontWeight.w600,
|
|
color: selected ? colors.cyberCyan : null,
|
|
),
|
|
),
|
|
const SizedBox(height: 2),
|
|
Text(
|
|
subtitle,
|
|
style: TextStyle(
|
|
fontFamily: AppTypography.fontBody,
|
|
fontSize: 11.5,
|
|
color: theme.colorScheme.onSurfaceVariant,
|
|
height: 1.25,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|