Initial Flutter project added
This commit is contained in:
4
lib/config/api_config.dart
Normal file
4
lib/config/api_config.dart
Normal file
@@ -0,0 +1,4 @@
|
||||
class ApiConfig {
|
||||
// Android emulator (use 10.0.2.2), change for physical device or iOS simulator
|
||||
static const String baseUrl = "http://10.0.2.2:8000/api";
|
||||
}
|
||||
30
lib/main.dart
Normal file
30
lib/main.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'providers/auth_provider.dart';
|
||||
import 'screens/splash_screen.dart';
|
||||
import 'package:google_fonts/google_fonts.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const KentApp());
|
||||
}
|
||||
|
||||
class KentApp extends StatelessWidget {
|
||||
const KentApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ChangeNotifierProvider(
|
||||
create: (_) => AuthProvider(),
|
||||
child: MaterialApp(
|
||||
title: 'Kent Logistics',
|
||||
debugShowCheckedModeBanner: false,
|
||||
theme: ThemeData(
|
||||
useMaterial3: true,
|
||||
textTheme: GoogleFonts.interTextTheme(),
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
|
||||
),
|
||||
home: const SplashScreen(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
24
lib/models/user_model.dart
Normal file
24
lib/models/user_model.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
class UserModel {
|
||||
final int? id;
|
||||
final String? customerId;
|
||||
final String? customerName;
|
||||
final String? email;
|
||||
|
||||
UserModel({this.id, this.customerId, this.customerName, this.email});
|
||||
|
||||
factory UserModel.fromJson(Map<String, dynamic> json) {
|
||||
return UserModel(
|
||||
id: json['id'],
|
||||
customerId: json['customer_id'] ?? json['customerId'],
|
||||
customerName: json['customer_name'] ?? json['name'],
|
||||
email: json['email'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'id': id,
|
||||
'customer_id': customerId,
|
||||
'customer_name': customerName,
|
||||
'email': email,
|
||||
};
|
||||
}
|
||||
81
lib/providers/auth_provider.dart
Normal file
81
lib/providers/auth_provider.dart
Normal file
@@ -0,0 +1,81 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../services/auth_service.dart';
|
||||
|
||||
class AuthProvider extends ChangeNotifier {
|
||||
final AuthService _service = AuthService();
|
||||
|
||||
bool _loading = false;
|
||||
bool get loading => _loading;
|
||||
|
||||
String? _token;
|
||||
Map<String, dynamic>? _user;
|
||||
|
||||
String? get token => _token;
|
||||
Map<String, dynamic>? get user => _user;
|
||||
bool get isLoggedIn => _token != null && _token!.isNotEmpty;
|
||||
|
||||
AuthProvider() {
|
||||
_loadFromPrefs();
|
||||
}
|
||||
|
||||
Future<void> _loadFromPrefs() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
_token = prefs.getString('token');
|
||||
final userJson = prefs.getString('user');
|
||||
if (userJson != null) {
|
||||
try {
|
||||
_user = Map<String, dynamic>.from(jsonDecode(userJson));
|
||||
} catch (_) {
|
||||
_user = null;
|
||||
}
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> login(String loginId, String password) async {
|
||||
_loading = true;
|
||||
notifyListeners();
|
||||
|
||||
final res = await _service.login(loginId, password);
|
||||
|
||||
_loading = false;
|
||||
|
||||
if (res['success'] == true) {
|
||||
final token = res['token']?.toString();
|
||||
final userMap = res['user'] is Map ? Map<String, dynamic>.from(res['user']) : null;
|
||||
|
||||
if (token != null && userMap != null) {
|
||||
await _saveSession(token, userMap);
|
||||
}
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
return res;
|
||||
}
|
||||
|
||||
Future<void> _saveSession(String token, Map<String, dynamic> userMap) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString('token', token);
|
||||
await prefs.setString('user', jsonEncode(userMap));
|
||||
_token = token;
|
||||
_user = userMap;
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
// optional: call API logout if implemented
|
||||
if (_token != null) {
|
||||
try {
|
||||
await _service.logout(_token!);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.remove('token');
|
||||
await prefs.remove('user');
|
||||
_token = null;
|
||||
_user = null;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
36
lib/screens/dashboard_screen.dart
Normal file
36
lib/screens/dashboard_screen.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../providers/auth_provider.dart';
|
||||
import 'welcome_screen.dart';
|
||||
|
||||
class DashboardScreen extends StatelessWidget {
|
||||
const DashboardScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final auth = Provider.of<AuthProvider>(context);
|
||||
final name = auth.user?['customer_name'] ?? 'User';
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text("Dashboard"),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.logout),
|
||||
onPressed: () async {
|
||||
await auth.logout();
|
||||
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (_) => const WelcomeScreen()), (r) => false);
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(18),
|
||||
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
||||
Text("Welcome, ${name}", style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w600)),
|
||||
const SizedBox(height: 12),
|
||||
const Text("Your dashboard will appear here. Build shipment list, tracking, create shipment screens next."),
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
70
lib/screens/login_screen.dart
Normal file
70
lib/screens/login_screen.dart
Normal file
@@ -0,0 +1,70 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../providers/auth_provider.dart';
|
||||
import '../widgets/rounded_input.dart';
|
||||
import '../widgets/primary_button.dart';
|
||||
import 'dashboard_screen.dart';
|
||||
|
||||
class LoginScreen extends StatefulWidget {
|
||||
const LoginScreen({super.key});
|
||||
@override
|
||||
State<LoginScreen> createState() => _LoginScreenState();
|
||||
}
|
||||
|
||||
class _LoginScreenState extends State<LoginScreen> {
|
||||
final cLoginId = TextEditingController();
|
||||
final cPassword = TextEditingController();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
cLoginId.dispose();
|
||||
cPassword.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _login() async {
|
||||
final auth = Provider.of<AuthProvider>(context, listen: false);
|
||||
|
||||
// Basic validation
|
||||
final loginId = cLoginId.text.trim();
|
||||
final password = cPassword.text.trim();
|
||||
if (loginId.isEmpty || password.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Please enter login id and password')));
|
||||
return;
|
||||
}
|
||||
|
||||
final res = await auth.login(loginId, password);
|
||||
|
||||
// Your controller returns { success: true/false, message, token, user }
|
||||
if (res['success'] == true) {
|
||||
// Navigate to dashboard
|
||||
if (!mounted) return;
|
||||
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (_) => const DashboardScreen()));
|
||||
} else {
|
||||
final msg = res['message']?.toString() ?? 'Login failed';
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(msg)));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final auth = Provider.of<AuthProvider>(context);
|
||||
final width = MediaQuery.of(context).size.width;
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Login')),
|
||||
body: Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: width * 0.06, vertical: 20),
|
||||
child: Column(
|
||||
children: [
|
||||
RoundedInput(controller: cLoginId, hint: 'Email / Mobile / Customer ID', keyboardType: TextInputType.text),
|
||||
const SizedBox(height: 12),
|
||||
RoundedInput(controller: cPassword, hint: 'Password', obscure: true),
|
||||
const SizedBox(height: 18),
|
||||
PrimaryButton(label: 'Login', onTap: _login, busy: auth.loading),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
67
lib/screens/otp_screen.dart
Normal file
67
lib/screens/otp_screen.dart
Normal file
@@ -0,0 +1,67 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../widgets/rounded_input.dart';
|
||||
import '../widgets/primary_button.dart';
|
||||
import '../services/request_service.dart';
|
||||
import 'waiting_screen.dart';
|
||||
|
||||
class OtpScreen extends StatefulWidget {
|
||||
final Map<String, dynamic> signupPayload;
|
||||
const OtpScreen({super.key, required this.signupPayload});
|
||||
|
||||
@override
|
||||
State<OtpScreen> createState() => _OtpScreenState();
|
||||
}
|
||||
|
||||
class _OtpScreenState extends State<OtpScreen> {
|
||||
final otpController = TextEditingController();
|
||||
bool verifying = false;
|
||||
|
||||
static const String defaultOtp = '123456'; // default OTP as you said
|
||||
|
||||
void _verifyAndSubmit() async {
|
||||
final entered = otpController.text.trim();
|
||||
if (entered.length != 6) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Enter 6 digit OTP')));
|
||||
return;
|
||||
}
|
||||
|
||||
if (entered != defaultOtp) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Invalid OTP')));
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() => verifying = true);
|
||||
|
||||
// send signup payload to backend
|
||||
final res = await RequestService().sendSignup(widget.signupPayload);
|
||||
|
||||
setState(() => verifying = false);
|
||||
|
||||
if (res['status'] == true || res['status'] == 'success') {
|
||||
// navigate to waiting screen
|
||||
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (_) => const WaitingScreen()));
|
||||
} else {
|
||||
final message = res['message']?.toString() ?? 'Failed';
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message)));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final pad = MediaQuery.of(context).size.width * 0.06;
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text("OTP Verification")),
|
||||
body: Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: pad, vertical: 20),
|
||||
child: Column(children: [
|
||||
const Text("Enter the 6-digit OTP sent to your mobile/email. (Default OTP: 123456)"),
|
||||
const SizedBox(height: 20),
|
||||
RoundedInput(controller: otpController, hint: "Enter OTP", keyboardType: TextInputType.number),
|
||||
const SizedBox(height: 14),
|
||||
PrimaryButton(label: "Verify & Submit", onTap: _verifyAndSubmit, busy: verifying),
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
79
lib/screens/signup_screen.dart
Normal file
79
lib/screens/signup_screen.dart
Normal file
@@ -0,0 +1,79 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../widgets/rounded_input.dart';
|
||||
import '../widgets/primary_button.dart';
|
||||
import 'otp_screen.dart';
|
||||
|
||||
class SignupScreen extends StatefulWidget {
|
||||
const SignupScreen({super.key});
|
||||
@override
|
||||
State<SignupScreen> createState() => _SignupScreenState();
|
||||
}
|
||||
|
||||
class _SignupScreenState extends State<SignupScreen> {
|
||||
final cName = TextEditingController();
|
||||
final cCompany = TextEditingController();
|
||||
final cDesignation = TextEditingController();
|
||||
final cEmail = TextEditingController();
|
||||
final cMobile = TextEditingController();
|
||||
final cAddress = TextEditingController();
|
||||
final cPincode = TextEditingController();
|
||||
|
||||
bool sending = false;
|
||||
|
||||
void _sendOtp() async {
|
||||
// We don't call backend for OTP here per your flow - OTP is default 123456.
|
||||
// Validate minimal
|
||||
if (cName.text.trim().isEmpty || cCompany.text.trim().isEmpty || cEmail.text.trim().isEmpty || cMobile.text.trim().isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Please fill the required fields')));
|
||||
return;
|
||||
}
|
||||
setState(() => sending = true);
|
||||
await Future.delayed(const Duration(milliseconds: 600)); // UI feel
|
||||
setState(() => sending = false);
|
||||
// Navigate to OTP screen with collected data
|
||||
final data = {
|
||||
'customer_name': cName.text.trim(),
|
||||
'company_name': cCompany.text.trim(),
|
||||
'designation': cDesignation.text.trim(),
|
||||
'email': cEmail.text.trim(),
|
||||
'mobile_no': cMobile.text.trim(),
|
||||
'address': cAddress.text.trim(),
|
||||
'pincode': cPincode.text.trim(),
|
||||
};
|
||||
Navigator.of(context).push(MaterialPageRoute(builder: (_) => OtpScreen(signupPayload: data)));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final pad = MediaQuery.of(context).size.width * 0.06;
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text("Create Account")),
|
||||
body: SafeArea(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: pad),
|
||||
child: SingleChildScrollView(
|
||||
child: Column(children: [
|
||||
const SizedBox(height: 16),
|
||||
RoundedInput(controller: cName, hint: "Customer name"),
|
||||
const SizedBox(height: 12),
|
||||
RoundedInput(controller: cCompany, hint: "Company name"),
|
||||
const SizedBox(height: 12),
|
||||
RoundedInput(controller: cDesignation, hint: "Designation (optional)"),
|
||||
const SizedBox(height: 12),
|
||||
RoundedInput(controller: cEmail, hint: "Email", keyboardType: TextInputType.emailAddress),
|
||||
const SizedBox(height: 12),
|
||||
RoundedInput(controller: cMobile, hint: "Mobile", keyboardType: TextInputType.phone),
|
||||
const SizedBox(height: 12),
|
||||
RoundedInput(controller: cAddress, hint: "Address", maxLines: 3),
|
||||
const SizedBox(height: 12),
|
||||
RoundedInput(controller: cPincode, hint: "Pincode", keyboardType: TextInputType.number),
|
||||
const SizedBox(height: 20),
|
||||
PrimaryButton(label: "Send OTP", onTap: _sendOtp, busy: sending),
|
||||
const SizedBox(height: 14),
|
||||
]),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
52
lib/screens/splash_screen.dart
Normal file
52
lib/screens/splash_screen.dart
Normal file
@@ -0,0 +1,52 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../providers/auth_provider.dart';
|
||||
import 'dashboard_screen.dart';
|
||||
import 'welcome_screen.dart';
|
||||
|
||||
class SplashScreen extends StatefulWidget {
|
||||
const SplashScreen({super.key});
|
||||
@override
|
||||
State<SplashScreen> createState() => _SplashScreenState();
|
||||
}
|
||||
|
||||
class _SplashScreenState extends State<SplashScreen> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_init();
|
||||
}
|
||||
|
||||
void _init() async {
|
||||
// small delay to show logo
|
||||
await Future.delayed(const Duration(milliseconds: 900));
|
||||
final auth = Provider.of<AuthProvider>(context, listen: false);
|
||||
// ensure provider has loaded prefs
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
if (auth.isLoggedIn) {
|
||||
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (_) => const DashboardScreen()));
|
||||
} else {
|
||||
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (_) => const WelcomeScreen()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final size = MediaQuery.of(context).size;
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Column(mainAxisSize: MainAxisSize.min, children: [
|
||||
Container(
|
||||
width: size.width * 0.34,
|
||||
height: size.width * 0.34,
|
||||
decoration: BoxDecoration(shape: BoxShape.circle, color: Theme.of(context).primaryColor.withOpacity(0.14)),
|
||||
child: Center(child: Text("K", style: TextStyle(fontSize: 48, fontWeight: FontWeight.bold, color: Theme.of(context).primaryColor))),
|
||||
),
|
||||
const SizedBox(height: 18),
|
||||
const Text("Kent Logistics", style: TextStyle(fontSize: 20, fontWeight: FontWeight.w600)),
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
39
lib/screens/waiting_screen.dart
Normal file
39
lib/screens/waiting_screen.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class WaitingScreen extends StatelessWidget {
|
||||
const WaitingScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text("Request Submitted")),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(18.0),
|
||||
child: Center(
|
||||
child: Column(mainAxisSize: MainAxisSize.min, children: [
|
||||
Icon(Icons.hourglass_top, size: 72, color: Theme.of(context).primaryColor),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
"Signup request submitted successfully.",
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
"Please wait up to 24 hours for admin approval. You will receive an email once approved.",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).popUntil((route) => route.isFirst);
|
||||
},
|
||||
child: const Padding(padding: EdgeInsets.symmetric(horizontal: 14, vertical: 12), child: Text("Back to Home")),
|
||||
style: ElevatedButton.styleFrom(shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12))),
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
43
lib/screens/welcome_screen.dart
Normal file
43
lib/screens/welcome_screen.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'signup_screen.dart';
|
||||
import 'login_screen.dart';
|
||||
|
||||
class WelcomeScreen extends StatelessWidget {
|
||||
const WelcomeScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final w = MediaQuery.of(context).size.width;
|
||||
return Scaffold(
|
||||
body: SafeArea(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: w * 0.06),
|
||||
child: Column(
|
||||
children: [
|
||||
const SizedBox(height: 28),
|
||||
Align(alignment: Alignment.centerLeft, child: Text("Welcome", style: Theme.of(context).textTheme.headlineSmall)),
|
||||
const SizedBox(height: 12),
|
||||
const Text(
|
||||
"Register to access Kent Logistics services. After signup admin will review and approve your request. Approval may take up to 24 hours.",
|
||||
style: TextStyle(fontSize: 15),
|
||||
),
|
||||
const Spacer(),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (_) => const SignupScreen())),
|
||||
child: const SizedBox(width: double.infinity, child: Center(child: Padding(padding: EdgeInsets.all(14.0), child: Text("Create Account")))),
|
||||
style: ElevatedButton.styleFrom(shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12))),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
OutlinedButton(
|
||||
onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (_) => const LoginScreen())),
|
||||
child: const SizedBox(width: double.infinity, child: Center(child: Padding(padding: EdgeInsets.all(14.0), child: Text("Login")))),
|
||||
style: OutlinedButton.styleFrom(shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12))),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
62
lib/services/auth_service.dart
Normal file
62
lib/services/auth_service.dart
Normal file
@@ -0,0 +1,62 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import '../config/api_config.dart';
|
||||
|
||||
class AuthService {
|
||||
final Dio _dio = Dio(BaseOptions(
|
||||
baseUrl: ApiConfig.baseUrl,
|
||||
connectTimeout: const Duration(seconds: 15),
|
||||
receiveTimeout: const Duration(seconds: 15),
|
||||
// You can add headers here if needed:
|
||||
// headers: {'Accept': 'application/json'},
|
||||
));
|
||||
|
||||
/// Calls /api/user/login with login_id and password
|
||||
Future<Map<String, dynamic>> login(String loginId, String password) async {
|
||||
try {
|
||||
final response = await _dio.post('/user/login', data: {
|
||||
'login_id': loginId,
|
||||
'password': password,
|
||||
});
|
||||
|
||||
// Ensure we return a Map<String, dynamic>
|
||||
if (response.data is Map) {
|
||||
return Map<String, dynamic>.from(response.data);
|
||||
} else {
|
||||
return {
|
||||
'success': false,
|
||||
'message': 'Invalid response from server',
|
||||
};
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
// Try to extract message from server response
|
||||
dynamic respData = e.response?.data;
|
||||
String message = 'Login failed';
|
||||
if (respData is Map && respData['message'] != null) {
|
||||
message = respData['message'].toString();
|
||||
} else if (e.message != null) {
|
||||
message = e.message!;
|
||||
}
|
||||
return {
|
||||
'success': false,
|
||||
'message': message,
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
'success': false,
|
||||
'message': e.toString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// Optional: logout (if you have logout endpoint)
|
||||
Future<Map<String, dynamic>> logout(String token) async {
|
||||
try {
|
||||
final Dio dio = Dio(BaseOptions(baseUrl: ApiConfig.baseUrl));
|
||||
dio.options.headers['Authorization'] = 'Bearer $token';
|
||||
final response = await dio.post('/user/logout');
|
||||
return Map<String, dynamic>.from(response.data ?? {'success': true});
|
||||
} catch (e) {
|
||||
return {'success': false, 'message': e.toString()};
|
||||
}
|
||||
}
|
||||
}
|
||||
25
lib/services/request_service.dart
Normal file
25
lib/services/request_service.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import '../config/api_config.dart';
|
||||
|
||||
class RequestService {
|
||||
final Dio _dio = Dio(
|
||||
BaseOptions(
|
||||
baseUrl: ApiConfig.baseUrl,
|
||||
connectTimeout: const Duration(seconds: 15),
|
||||
),
|
||||
);
|
||||
/// Send signup request to backend (after OTP verified)
|
||||
Future<Map<String, dynamic>> sendSignup(Map<String, dynamic> payload) async {
|
||||
try {
|
||||
final resp = await _dio.post('/signup-request', data: payload);
|
||||
return resp.data is Map ? Map<String, dynamic>.from(resp.data) : {'status': true, 'message': 'OK'};
|
||||
} on DioException catch (e) {
|
||||
return {
|
||||
'status': false,
|
||||
'message': e.response?.data ?? e.message
|
||||
};
|
||||
} catch (e) {
|
||||
return {'status': false, 'message': e.toString()};
|
||||
}
|
||||
}
|
||||
}
|
||||
11
lib/services/test_service.dart
Normal file
11
lib/services/test_service.dart
Normal file
@@ -0,0 +1,11 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import '../config/api_config.dart';
|
||||
|
||||
class TestService {
|
||||
final Dio _dio = Dio();
|
||||
|
||||
Future<Map<String, dynamic>> testApi() async {
|
||||
final response = await _dio.get("${ApiConfig.baseUrl}/test");
|
||||
return response.data;
|
||||
}
|
||||
}
|
||||
19
lib/widgets/primary_button.dart
Normal file
19
lib/widgets/primary_button.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
import 'package:flutter/material.dart';
|
||||
class PrimaryButton extends StatelessWidget {
|
||||
final String label;
|
||||
final VoidCallback onTap;
|
||||
final bool busy;
|
||||
const PrimaryButton({super.key, required this.label, required this.onTap, this.busy = false});
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton(
|
||||
onPressed: busy ? null : onTap,
|
||||
style: ElevatedButton.styleFrom(padding: const EdgeInsets.symmetric(vertical: 14), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12))),
|
||||
child: busy ? const SizedBox(height: 18, width: 18, child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white)) : Text(label, style: const TextStyle(fontSize: 16)),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
35
lib/widgets/rounded_input.dart
Normal file
35
lib/widgets/rounded_input.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class RoundedInput extends StatelessWidget {
|
||||
final TextEditingController controller;
|
||||
final String hint;
|
||||
final TextInputType keyboardType;
|
||||
final bool obscure;
|
||||
final int? maxLines;
|
||||
|
||||
const RoundedInput({
|
||||
super.key,
|
||||
required this.controller,
|
||||
required this.hint,
|
||||
this.keyboardType = TextInputType.text,
|
||||
this.obscure = false,
|
||||
this.maxLines = 1,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final radius = BorderRadius.circular(12);
|
||||
return TextField(
|
||||
controller: controller,
|
||||
keyboardType: keyboardType,
|
||||
obscureText: obscure,
|
||||
maxLines: maxLines,
|
||||
decoration: InputDecoration(
|
||||
filled: true,
|
||||
hintText: hint,
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||||
border: OutlineInputBorder(borderRadius: radius, borderSide: BorderSide.none),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user