204 lines
8.1 KiB
Dart
204 lines
8.1 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart'; // Untuk mendapatkan email user yang login
|
|
|
|
class CreateEventPage extends StatefulWidget {
|
|
@override
|
|
_CreateEventPageState createState() => _CreateEventPageState();
|
|
}
|
|
|
|
class _CreateEventPageState extends State<CreateEventPage> {
|
|
final _nameController = TextEditingController();
|
|
final _lapController = TextEditingController();
|
|
final _formKey = GlobalKey<FormState>(); // Tambahkan FormKey
|
|
|
|
static const int MIN_LAPS = 3; // Konstanta untuk minimal putaran
|
|
|
|
// Warna utama yang terinspirasi dari gambar Anda
|
|
final Color primaryBlue = Color(0xFF42A5F5); // Biru muda yang cerah
|
|
final Color lightBlue = Color(0xFFBBDEFB); // Biru yang lebih terang untuk background
|
|
final Color darkBlue = Color(0xFF1976D2); // Biru yang lebih gelap untuk teks/ikon
|
|
|
|
void _createEvent() async {
|
|
// Validasi form secara keseluruhan
|
|
if (!_formKey.currentState!.validate()) {
|
|
return; // Hentikan jika ada error validasi
|
|
}
|
|
|
|
final eventName = _nameController.text.trim();
|
|
final totalLaps = int.parse(_lapController.text.trim()); // Sudah dipastikan valid oleh validator
|
|
|
|
// Validasi tambahan untuk minimal putaran
|
|
if (totalLaps < MIN_LAPS) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: Text('Jumlah Putaran Tidak Valid', style: TextStyle(color: darkBlue)),
|
|
content: Text('Minimal jumlah putaran adalah $MIN_LAPS.', style: TextStyle(color: Colors.grey[800])),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: Text('OK', style: TextStyle(color: primaryBlue)),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
return; // Hentikan proses jika putaran kurang dari minimal
|
|
}
|
|
|
|
final totalDistance = totalLaps * 400; // 1 putaran = 400 meter
|
|
|
|
String createdBy = 'unknown_user@example.com'; // Default value
|
|
User? currentUser = FirebaseAuth.instance.currentUser;
|
|
if (currentUser != null && currentUser.email != null) {
|
|
createdBy = currentUser.email!;
|
|
} else {
|
|
print("Peringatan: Email user tidak ditemukan. Menggunakan email default.");
|
|
}
|
|
|
|
try {
|
|
await FirebaseFirestore.instance.collection('events').add({
|
|
'name': eventName,
|
|
'created_at': Timestamp.now(),
|
|
'created_by': createdBy,
|
|
'start_status': false,
|
|
'participants': {},
|
|
'total_laps': totalLaps,
|
|
'total_distance': totalDistance,
|
|
'type': 'track',
|
|
'is_started_permanently': false, // Tambahkan field ini
|
|
'is_finished_permanently': false, // Tambahkan field ini
|
|
});
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
content: Text('Event "$eventName" berhasil dibuat ($totalLaps putaran = ${totalDistance / 1000} KM).'),
|
|
backgroundColor: Colors.green, // Warna hijau untuk sukses
|
|
));
|
|
|
|
Navigator.pop(context);
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
content: Text('Gagal membuat event: $e'),
|
|
backgroundColor: Colors.red, // Warna merah untuk error
|
|
));
|
|
print('Error creating event: $e');
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_nameController.dispose();
|
|
_lapController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: Colors.grey[50], // Background lembut
|
|
appBar: AppBar(
|
|
title: Text(
|
|
'Buat Event Baru',
|
|
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
|
|
),
|
|
centerTitle: true,
|
|
backgroundColor: primaryBlue, // Warna AppBar
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.only(
|
|
bottomLeft: Radius.circular(20), // Sudut melengkung di kiri bawah
|
|
bottomRight: Radius.circular(20), // Sudut melengkung di kanan bawah
|
|
),
|
|
),
|
|
iconTheme: IconThemeData(color: Colors.white), // Warna ikon back
|
|
elevation: 5, // Tambahkan sedikit bayangan
|
|
),
|
|
body: SingleChildScrollView( // Agar bisa di-scroll jika keyboard muncul
|
|
child: Padding(
|
|
padding: EdgeInsets.all(24), // Padding lebih besar
|
|
child: Form( // Wrap dengan Form widget
|
|
key: _formKey, // Pasang GlobalKey
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch, // Agar elemen mengisi lebar
|
|
children: [
|
|
Center( // Wrap dengan Center agar gambar di tengah
|
|
child: Image.asset(
|
|
'images/run.png', // Path ke gambar logo Anda
|
|
height: 80, // Sesuaikan tinggi gambar
|
|
width: 80, // Sesuaikan lebar gambar
|
|
// Anda bisa menambahkan fit: BoxFit.contain, jika perlu
|
|
),
|
|
),
|
|
SizedBox(height: 24),
|
|
TextFormField( // Ganti TextField dengan TextFormField
|
|
controller: _nameController,
|
|
decoration: InputDecoration(
|
|
labelText: 'Nama Event',
|
|
hintText: 'Misal: Jember Marathon',
|
|
border: OutlineInputBorder( // Border melengkung
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
focusedBorder: OutlineInputBorder( // Warna border saat fokus
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide(color: primaryBlue, width: 2),
|
|
),
|
|
// prefixIcon: Icon(Icons.event, color: primaryBlue), // Ikon
|
|
),
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Nama event tidak boleh kosong';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
SizedBox(height: 20), // Spasi antar input
|
|
TextFormField( // Ganti TextField dengan TextFormField
|
|
controller: _lapController,
|
|
keyboardType: TextInputType.number,
|
|
decoration: InputDecoration(
|
|
labelText: 'Jumlah Putaran (lap)',
|
|
hintText: 'Minimal 3 putaran',
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide(color: primaryBlue, width: 2),
|
|
),
|
|
// prefixIcon: Icon(Icons.sports_score, color: primaryBlue), // Ikon
|
|
),
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Jumlah putaran tidak boleh kosong';
|
|
}
|
|
final n = int.tryParse(value);
|
|
if (n == null || n <= 0) {
|
|
return 'Masukkan angka lebih dari 0';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
SizedBox(height: 32), // Spasi sebelum tombol
|
|
ElevatedButton.icon(
|
|
onPressed: _createEvent,
|
|
// icon: Icon(Icons.add_circle, color: Colors.white),
|
|
label: Text(
|
|
'Buat Event',
|
|
style: TextStyle(fontSize: 18, color: Colors.white, fontWeight: FontWeight.bold),
|
|
),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: primaryBlue, // Warna tombol biru
|
|
padding: EdgeInsets.symmetric(vertical: 16), // Padding lebih besar
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(12), // Sudut melengkung
|
|
),
|
|
elevation: 5, // Bayangan tombol
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |