feat: add quiz controller

This commit is contained in:
akhdanre 2025-04-26 20:58:16 +07:00
parent dbb3bd8c90
commit 39ab35b2a8
7 changed files with 55 additions and 30 deletions

View File

@ -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>()));
}
}

View File

@ -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()}");
}
}
}

View File

@ -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());
}
}

View File

@ -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;
}

View File

@ -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),

View File

@ -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;

View File

@ -43,11 +43,19 @@ class ProfileView extends GetView<ProfileController> {
}
Widget _buildAvatar() {
return const CircleAvatar(
radius: 45,
backgroundColor: Colors.blueAccent,
child: Icon(Icons.person, size: 50, color: Colors.white),
);
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() {