feat: add quiz controller
This commit is contained in:
parent
dbb3bd8c90
commit
39ab35b2a8
|
@ -1,4 +1,5 @@
|
|||
import 'package:get/get.dart';
|
||||
import 'package:quiz_app/data/controllers/user_controller.dart';
|
||||
import 'package:quiz_app/data/providers/dio_client.dart';
|
||||
import 'package:quiz_app/data/services/user_storage_service.dart';
|
||||
|
||||
|
@ -7,5 +8,6 @@ class InitialBindings extends Bindings {
|
|||
void dependencies() {
|
||||
Get.put<UserStorageService>(UserStorageService());
|
||||
Get.putAsync(() => ApiClient().init());
|
||||
Get.put<UserController>(UserController(Get.find<UserStorageService>()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
import 'package:get/get.dart';
|
||||
import 'package:quiz_app/data/services/user_storage_service.dart';
|
||||
|
||||
class UserController extends GetxController {
|
||||
final UserStorageService _userStorageService;
|
||||
|
||||
UserController(this._userStorageService);
|
||||
|
||||
Rx<String> userName = "".obs;
|
||||
Rx<String?> userImage = Rx<String?>(null);
|
||||
Rx<String> email = "".obs;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
loadUser();
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
Future<void> loadUser() async {
|
||||
final data = await _userStorageService.loadUser();
|
||||
if (data != null) {
|
||||
userName.value = data.name;
|
||||
userImage.value = data.picUrl;
|
||||
email.value = data.email;
|
||||
print("Loaded user: ${data.toJson()}");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,6 +5,6 @@ import 'package:quiz_app/feature/home/controller/home_controller.dart';
|
|||
class HomeBinding extends Bindings {
|
||||
@override
|
||||
void dependencies() {
|
||||
Get.lazyPut<HomeController>(() => HomeController(Get.find<UserStorageService>()));
|
||||
Get.lazyPut<HomeController>(() => HomeController());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +1,9 @@
|
|||
import 'package:get/get.dart';
|
||||
import 'package:quiz_app/data/models/login/login_response_model.dart';
|
||||
import 'package:quiz_app/data/services/user_storage_service.dart';
|
||||
import 'package:quiz_app/data/controllers/user_controller.dart';
|
||||
|
||||
class HomeController extends GetxController {
|
||||
final UserStorageService _userStorageService;
|
||||
final UserController _userController = Get.find<UserController>();
|
||||
|
||||
HomeController(this._userStorageService);
|
||||
|
||||
Rx<String> userName = "Dani".obs;
|
||||
String? userImage;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
getUserData();
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
Future<void> getUserData() async {
|
||||
LoginResponseModel? data = await _userStorageService.loadUser();
|
||||
if (data == null) return;
|
||||
print("User data: ${data.toJson()}");
|
||||
userName.value = data.name;
|
||||
userImage = data.picUrl;
|
||||
}
|
||||
Rx<String> get userName => _userController.userName;
|
||||
Rx<String?> get userImage => _userController.userImage;
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ class HomeView extends GetView<HomeController> {
|
|||
Obx(
|
||||
() => UserGretingsComponent(
|
||||
userName: controller.userName.value,
|
||||
userImage: controller.userImage,
|
||||
userImage: controller.userImage.value,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
import 'package:get/get.dart';
|
||||
import 'package:quiz_app/data/controllers/user_controller.dart';
|
||||
|
||||
class ProfileController extends GetxController {
|
||||
final userName = 'Alhidan Robbani'.obs;
|
||||
final email = 'alhidan@example.com'.obs;
|
||||
final UserController _userController = Get.find<UserController>();
|
||||
|
||||
Rx<String> get userName => _userController.userName;
|
||||
Rx<String> get email => _userController.email;
|
||||
Rx<String?> get userImage => _userController.userImage;
|
||||
|
||||
final totalQuizzes = 12.obs;
|
||||
final avgScore = 85.obs;
|
||||
|
|
|
@ -43,12 +43,20 @@ class ProfileView extends GetView<ProfileController> {
|
|||
}
|
||||
|
||||
Widget _buildAvatar() {
|
||||
if (controller.userImage.value != null) {
|
||||
return CircleAvatar(
|
||||
radius: 45,
|
||||
backgroundColor: Colors.blueAccent,
|
||||
backgroundImage: NetworkImage(controller.userImage.value!),
|
||||
);
|
||||
} else {
|
||||
return const CircleAvatar(
|
||||
radius: 45,
|
||||
backgroundColor: Colors.blueAccent,
|
||||
child: Icon(Icons.person, size: 50, color: Colors.white),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildStats() {
|
||||
return Container(
|
||||
|
|
Loading…
Reference in New Issue