Skip to content

Commit

Permalink
create and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
RaghvindYadav committed Dec 10, 2023
1 parent 399922d commit 6b11d19
Show file tree
Hide file tree
Showing 9 changed files with 523 additions and 30 deletions.
38 changes: 38 additions & 0 deletions lib/controllers/user_controller.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:convert';

import 'package:afghan_net/services/app_remote_routes.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';

Expand All @@ -6,7 +9,10 @@ import '../services/api_provider.dart';

class UserController extends GetxController{
final apiProvider = ApiProvider();
GetStorage sr = GetStorage();
late User user;
late List<User> listUsers;

userLogin({required String email, required String pass}) async{
final data = await apiProvider.post("login",{"email":email,"password":pass});
user = User.fromJson(data);
Expand All @@ -15,4 +21,36 @@ class UserController extends GetxController{
await sr.write("user", user);
print(sr.read('user'));
}

Future<String> createUser(Map<String,dynamic> userData)async{
final data = await apiProvider.post('createUser', userData);

print("message ${data['message']}");
return data['message'];
}

User getUser(){
if(sr.read('user') is User){
user = sr.read('user');
}else{
user = User.fromJson(sr.read('user'));
}
return user;
}

deleteUser({required String userId}) async{
final data = await apiProvider.delete("user", {"id":userId});
print(data);

}

Future<List<User>> getAllUsers()async{
// listUsers.clear();
final data = await apiProvider.get(AppRemoteRoutes.allUser);
final List listData = json.decode(jsonEncode(data['data']));
listUsers =
listData.map((x)=>User.fromJson(x)).toList();
return listUsers;
}

}
6 changes: 5 additions & 1 deletion lib/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:afghan_net/Afgan_List_UI/internet_bundle/vpn_plans/vpn_all.dart';
import 'package:afghan_net/Afgan_List_UI/status/dialog_box.dart';
import 'package:afghan_net/catagory.dart';
import 'package:afghan_net/controllers/user_controller.dart';
import 'package:afghan_net/order.dart';
import 'package:afghan_net/web/drawer/drawer.dart';
import 'package:afghan_net/widgets/tab_navigate.dart';
import 'package:afghan_net/widgets/text.dart';
import 'package:afghan_net/widgets/transaction.dart';
Expand All @@ -24,6 +26,8 @@ class HomePage extends StatelessWidget {

@override
Widget build(BuildContext context) {
final userController = Get.find<UserController>();
userController.getUser();
return Scaffold(
appBar: AppBar(
title: Row(
Expand Down Expand Up @@ -57,7 +61,7 @@ class HomePage extends StatelessWidget {
),
),
key: _scaffoldKey,
drawer: All_Drawer(),
drawer: All_Drawer_Web(),
body: SingleChildScrollView(
child: Column(children: [
SizedBox(
Expand Down
21 changes: 11 additions & 10 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:afghan_net/home_page.dart';
import 'package:afghan_net/login.dart';
import 'package:afghan_net/screens/admin_management.dart';
import 'package:afghan_net/splash.dart';
import 'package:afghan_net/web/HomePageWeb.dart';

Expand Down Expand Up @@ -31,16 +32,16 @@ class MyApp extends StatelessWidget {
}

Widget _buildHomePage(double screenWidth, DeviceType deviceType) {
if (screenWidth < 400 || deviceType == DeviceType.mobile) {
// if (screenWidth < 400 || deviceType == DeviceType.mobile) {
// return Splash();
// } else if (screenWidth < 400) {
// return Splash();
// } else if (screenWidth >= 700 && screenWidth < 1024) {
// return Splash();
// } else if (screenWidth >= 1024 && screenWidth <= 1920) {
// return Splash();
// } else {
return Splash();
} else if (screenWidth < 400) {
return Splash();
} else if (screenWidth >= 700 && screenWidth < 1024) {
return Splash();
} else if (screenWidth >= 1024 && screenWidth <= 1920) {
return Splash();
} else {
return Splash();
}
// }
}
}
4 changes: 2 additions & 2 deletions lib/modal/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class User {
DateTime createdAt;
DateTime updatedAt;
int v;
String token;
String? token;

User({
required this.id,
Expand All @@ -30,7 +30,7 @@ class User {
required this.createdAt,
required this.updatedAt,
required this.v,
required this.token,
this.token,
});

factory User.fromJson(Map<String, dynamic> json) => User(
Expand Down
129 changes: 129 additions & 0 deletions lib/screens/admin_management.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import 'package:afghan_net/controllers/user_controller.dart';
import 'package:afghan_net/screens/create_user.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class AdminManagement extends StatefulWidget {
const AdminManagement({super.key});

@override
State<AdminManagement> createState() => _AdminManagementState();
}

class _AdminManagementState extends State<AdminManagement> {
final userController = Get.put(UserController());

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Admin Management"),
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
borderRadius:
const BorderRadius.all(Radius.circular(5)),
border: Border.all(color: Colors.green)),
child: ListTile(
leading: const Icon(
Icons.create,
color: Colors.green,
),
title: const Text(
"Create User",
style: TextStyle(
color: Colors.green, fontWeight: FontWeight.bold),
),
onTap: () {
Get.to(()=>CreateUser());
},
),
),
),
const SizedBox(
width: 40,
),
Expanded(
child: Container(
decoration: BoxDecoration(
borderRadius:
const BorderRadius.all(Radius.circular(5)),
border: Border.all(color: Colors.red)),
child: ListTile(
leading: const Icon(
Icons.delete,
color: Colors.red,
),
title: const Text(
"Deleted User",
style: TextStyle(color: Colors.red),
),
onTap: () {
// route for delete user page
},
),
),
),
],
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0),
child: FutureBuilder(
future: userController.getAllUsers(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: userController.listUsers.length,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return Dismissible(
background: Container(color: Colors.red,),
onDismissed: (direction) async{
await userController.deleteUser(userId: userController.listUsers[index].id);

setState(() {
userController.listUsers.removeAt(index);
});

ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('${userController.listUsers[index].firstName} deleted'),
),
);
},
key: Key(userController.listUsers[index].id),
child: ListTile(
leading: const Icon(Icons.account_circle),
title: Text(
"${userController.listUsers[index].firstName} ${userController.listUsers[index].lastName}"),
subtitle: Text(
userController.listUsers[index].email ??
userController.listUsers[index].mobile
.toString()),
trailing: Text(
userController.listUsers[index].userType),
),
);
});
}
return const Center(child: CircularProgressIndicator());
}),
)
],
),
),
),
);
}
}
Loading

0 comments on commit 6b11d19

Please sign in to comment.