Files
Ontime/flutter_merchant_clone/lib/main.dart
2026-04-01 11:55:47 +07:00

287 lines
9.3 KiB
Dart

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'ontime_auth.dart';
const String _kLoginPath = 'merchant/login';
const String _kDial = '62';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MerchantCloneApp());
}
class MerchantCloneApp extends StatelessWidget {
const MerchantCloneApp({super.key});
static const Color primary = Color(0xFF5BC8DA);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'id.ontime.merchant',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: primary),
useMaterial3: true,
),
home: const MerchantLoginPage(),
);
}
}
class MerchantLoginPage extends StatefulWidget {
const MerchantLoginPage({super.key});
@override
State<MerchantLoginPage> createState() => _MerchantLoginPageState();
}
class _MerchantLoginPageState extends State<MerchantLoginPage> {
final _phone = TextEditingController();
final _password = TextEditingController();
bool _busy = false;
@override
void dispose() {
_phone.dispose();
_password.dispose();
super.dispose();
}
Future<void> _signIn() async {
final pass = _password.text;
final digits = _phone.text.trim();
if (pass.isEmpty) {
_toast('Isi password');
return;
}
if (digits.isEmpty) {
_toast('Isi nomor telepon');
return;
}
setState(() => _busy = true);
try {
await FirebaseMessaging.instance.requestPermission();
final token = await FirebaseMessaging.instance.getToken();
if (token == null || token.length < 50) {
_toast(
'Token FCM belum siap. Izinkan notifikasi & pastikan google-services.json / API key Firebase valid.',
);
return;
}
final result = await OntimeAuth.login(
path: _kLoginPath,
password: pass,
phoneDigits: digits,
dialCodeWithoutPlus: _kDial,
fcmToken: token,
);
if (!mounted) return;
if (result.success) {
_toast('Login sukses, token FCM Firebase sudah dikirim ke backend (FCM v1).');
Navigator.of(context).pushReplacement(
MaterialPageRoute<void>(
builder: (_) => const _HomePlaceholder(title: 'id.ontime.merchant'),
),
);
} else {
_toast(result.message);
}
} catch (e) {
if (mounted) _toast('$e');
} finally {
if (mounted) setState(() => _busy = false);
}
}
void _toast(String msg) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(msg)));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Stack(
children: [
SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(bottom: 92),
child: Column(
children: [
const SizedBox(height: 20),
const SizedBox(
height: 250,
child: Center(
child: Icon(Icons.storefront, size: 120, color: MerchantCloneApp.primary),
),
),
Container(
margin: const EdgeInsets.fromLTRB(15, 0, 15, 20),
padding: const EdgeInsets.all(15),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
boxShadow: const [BoxShadow(color: Color(0x22000000), blurRadius: 8, offset: Offset(0, 4))],
),
child: Column(
children: [
_MerchantInput(
hint: 'Nomor Telepon',
isIcon: false,
controller: _phone,
obscure: false,
),
const SizedBox(height: 10),
_MerchantInput(
hint: 'Password',
isIcon: true,
controller: _password,
obscure: true,
),
const SizedBox(height: 20),
const Text('Lupa Password?', style: TextStyle(color: MerchantCloneApp.primary, fontSize: 14)),
const SizedBox(height: 10),
SizedBox(
width: double.infinity,
height: 50,
child: ElevatedButton(
onPressed: _busy ? null : _signIn,
style: ElevatedButton.styleFrom(
backgroundColor: MerchantCloneApp.primary,
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
),
child: _busy
? const SizedBox(
height: 22,
width: 22,
child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white),
)
: const Text('Sign In', style: TextStyle(fontSize: 18)),
),
),
const SizedBox(height: 16),
const Text(
'Dengan masuk, Anda menyetujui kebijakan privasi.',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 13),
),
],
),
),
],
),
),
),
const Positioned(
top: 15,
left: 15,
child: CircleAvatar(
radius: 20,
backgroundColor: Colors.white,
child: Icon(Icons.arrow_back, color: Colors.black),
),
),
Positioned(
left: 0,
right: 0,
bottom: 0,
child: Container(
height: 80,
color: Colors.white,
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
child: const Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Belum punya akun? ', style: TextStyle(fontSize: 14)),
Text('Daftar disini', style: TextStyle(fontSize: 16, color: MerchantCloneApp.primary, fontWeight: FontWeight.w600)),
],
),
SizedBox(height: 8),
Text(
'Note: Jika sudah mendaftar akun baru, tunggu konfirmasi admin untuk info lebih lanjut.',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 12),
),
],
),
),
),
],
),
),
);
}
}
class _HomePlaceholder extends StatelessWidget {
const _HomePlaceholder({required this.title});
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: const Center(child: Text('Login berhasil')),
);
}
}
class _MerchantInput extends StatelessWidget {
const _MerchantInput({
required this.hint,
required this.isIcon,
required this.controller,
required this.obscure,
});
final String hint;
final bool isIcon;
final TextEditingController controller;
final bool obscure;
@override
Widget build(BuildContext context) {
return Container(
height: 50,
decoration: BoxDecoration(border: Border.all(color: const Color(0xFFC4C4C4)), borderRadius: BorderRadius.circular(8)),
child: Row(
children: [
SizedBox(
width: 80,
child: Center(
child: isIcon
? const Icon(Icons.lock_outline, color: MerchantCloneApp.primary)
: const Text('+62', style: TextStyle(color: MerchantCloneApp.primary, fontSize: 18)),
),
),
Container(width: 1, margin: const EdgeInsets.symmetric(vertical: 6), color: const Color(0xFFC4C4C4)),
Expanded(
child: TextField(
controller: controller,
obscureText: obscure,
keyboardType: isIcon ? TextInputType.visiblePassword : TextInputType.phone,
decoration: InputDecoration(
hintText: hint,
border: InputBorder.none,
contentPadding: const EdgeInsets.symmetric(horizontal: 10),
),
),
),
],
),
);
}
}