demo transaksi
This commit is contained in:
@@ -0,0 +1,286 @@
|
||||
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),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class LoginResult {
|
||||
LoginResult({required this.success, required this.message});
|
||||
|
||||
final bool success;
|
||||
final String message;
|
||||
}
|
||||
|
||||
/// Matches native `ServiceGenerator.createService` + login POST (Basic auth + JSON body).
|
||||
class OntimeAuth {
|
||||
OntimeAuth._();
|
||||
|
||||
static const String baseUrl = 'https://apitest.semestaterpadu.my.id/api/';
|
||||
|
||||
static Future<LoginResult> login({
|
||||
required String path,
|
||||
required String password,
|
||||
String email = '',
|
||||
required String phoneDigits,
|
||||
required String dialCodeWithoutPlus,
|
||||
required String fcmToken,
|
||||
}) async {
|
||||
final cleanedDial = dialCodeWithoutPlus.replaceAll('+', '').trim();
|
||||
final digitsOnly = phoneDigits.replaceAll(RegExp(r'\D'), '');
|
||||
final local = digitsOnly.replaceFirst(RegExp(r'^0+'), '');
|
||||
final fullPhone = digitsOnly.startsWith(cleanedDial)
|
||||
? digitsOnly
|
||||
: '$cleanedDial$local';
|
||||
final useEmail = email.trim().isNotEmpty;
|
||||
final basicUser = useEmail ? email.trim() : fullPhone;
|
||||
|
||||
final body = <String, dynamic>{
|
||||
'password': password,
|
||||
// Send all common aliases expected by backend FCM v1 helper.
|
||||
'firebase_token': fcmToken,
|
||||
'fcm_token': fcmToken,
|
||||
'reg_id': fcmToken,
|
||||
'token': fcmToken,
|
||||
if (useEmail) ...{
|
||||
'email': email.trim(),
|
||||
'no_telepon': '',
|
||||
} else ...{
|
||||
'email': null,
|
||||
'no_telepon': fullPhone,
|
||||
},
|
||||
};
|
||||
|
||||
final uri = Uri.parse('$baseUrl$path');
|
||||
final auth = base64Encode(utf8.encode('$basicUser:$password'));
|
||||
|
||||
final resp = await http
|
||||
.post(
|
||||
uri,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
'Authorization': 'Basic $auth',
|
||||
},
|
||||
body: jsonEncode(body),
|
||||
)
|
||||
.timeout(const Duration(seconds: 60));
|
||||
|
||||
Map<String, dynamic>? json;
|
||||
try {
|
||||
if (resp.body.isNotEmpty) {
|
||||
final decoded = jsonDecode(resp.body);
|
||||
if (decoded is Map<String, dynamic>) json = decoded;
|
||||
}
|
||||
} catch (_) {}
|
||||
|
||||
final msg = json?['message']?.toString() ?? '';
|
||||
final data = json?['data'];
|
||||
final hasData = data is List && data.isNotEmpty;
|
||||
final ok = resp.statusCode >= 200 &&
|
||||
resp.statusCode < 300 &&
|
||||
msg.toLowerCase() == 'found' &&
|
||||
hasData;
|
||||
|
||||
if (ok) return LoginResult(success: true, message: msg);
|
||||
|
||||
if (msg.isNotEmpty) {
|
||||
return LoginResult(success: false, message: msg);
|
||||
}
|
||||
if (resp.statusCode == 401) {
|
||||
return LoginResult(success: false, message: 'no hp atau password salah!');
|
||||
}
|
||||
if (resp.body.isNotEmpty && resp.body.length < 400) {
|
||||
return LoginResult(success: false, message: resp.body);
|
||||
}
|
||||
return LoginResult(
|
||||
success: false,
|
||||
message: 'Login gagal (HTTP ${resp.statusCode})',
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user