Untitled

Anonymous
Plain Text
10/03/2025 6:53 AM
9.2 KB
13
Indexable
Never
import 'package:flutter_test_case/models/user_model.dart';
import 'package:get/get.dart';

class UserDetailController extends GetxController {
  final Rx<UserModel?> user = Rx<UserModel?>(null);
  final RxBool isLoading = RxBool(false);
  final RxString errorMessage = RxString('');

  @override
  void onInit() {
    super.onInit();
    _loadUserData();
  }

  void _loadUserData() {
    try {
      isLoading.value = true;
      errorMessage.value = '';
      
      // Get user data from arguments
      final arguments = Get.arguments;
      if (arguments != null && arguments is UserModel) {
        user.value = arguments;
      } else {
        errorMessage.value = 'User data not found';
      }
    } catch (e) {
      errorMessage.value = 'Failed to load user data';
    } finally {
      isLoading.value = false;
    }
  }

  void goBack() {
    Get.back();
  }
}

import 'package:flutter_test_case/controllers/user_detail_controller.dart';
import 'package:get/get.dart';

class UserDetailBinding extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut<UserDetailController>(() => UserDetailController());
  }
}

import 'package:flutter/material.dart';
import 'package:flutter_test_case/controllers/user_detail_controller.dart';
import 'package:get/get.dart';

class UserDetailPage extends GetView<UserDetailController> {
  const UserDetailPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('User Detail'),
        backgroundColor: Colors.blue,
        foregroundColor: Colors.white,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: controller.goBack,
        ),
      ),
      body: Obx(() {
        if (controller.isLoading.value) {
          return const Center(
            child: CircularProgressIndicator(),
          );
        }

        if (controller.errorMessage.value.isNotEmpty) {
          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(
                  Icons.error_outline,
                  size: 64,
                  color: Colors.red.shade300,
                ),
                const SizedBox(height: 16),
                Text(
                  controller.errorMessage.value,
                  style: TextStyle(
                    fontSize: 16,
                    color: Colors.red.shade600,
                  ),
                ),
                const SizedBox(height: 16),
                ElevatedButton(
                  onPressed: controller.goBack,
                  child: const Text('Go Back'),
                ),
              ],
            ),
          );
        }

        final user = controller.user.value;
        if (user == null) {
          return const Center(
            child: Text('No user data available'),
          );
        }

        return SingleChildScrollView(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // Profile Picture Section
              Center(
                child: Container(
                  width: 120,
                  height: 120,
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    border: Border.all(
                      color: Colors.blue.shade200,
                      width: 3,
                    ),
                  ),
                  child: ClipOval(
                    child: user.avatar != null && user.avatar!.isNotEmpty
                        ? Image.network(
                            user.avatar!,
                            fit: BoxFit.cover,
                            errorBuilder: (context, error, stackTrace) {
                              return _buildDefaultAvatar();
                            },
                          )
                        : _buildDefaultAvatar(),
                  ),
                ),
              ),
              const SizedBox(height: 24),

              // User Information Card
              Card(
                elevation: 4,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        'Personal Information',
                        style: Theme.of(context).textTheme.headlineSmall?.copyWith(
                          fontWeight: FontWeight.bold,
                          color: Colors.blue.shade700,
                        ),
                      ),
                      const Divider(height: 24),
                      
                      _buildInfoRow(
                        icon: Icons.person,
                        label: 'Full Name',
                        value: '${user.firstName ?? ''} ${user.lastName ?? ''}',
                      ),
                      
                      _buildInfoRow(
                        icon: Icons.email,
                        label: 'Email',
                        value: user.email ?? 'N/A',
                      ),
                      
                      _buildInfoRow(
                        icon: Icons.badge,
                        label: 'User ID',
                        value: user.id?.toString() ?? 'N/A',
                      ),
                    ],
                  ),
                ),
              ),
              const SizedBox(height: 24),

              // Action Buttons
              Row(
                children: [
                  Expanded(
                    child: ElevatedButton.icon(
                      onPressed: controller.goBack,
                      icon: const Icon(Icons.arrow_back),
                      label: const Text('Back to Home'),
                      style: ElevatedButton.styleFrom(
                        backgroundColor: Colors.blue,
                        foregroundColor: Colors.white,
                        padding: const EdgeInsets.symmetric(vertical: 12),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        );
      }),
    );
  }

  Widget _buildDefaultAvatar() {
    return Container(
      color: Colors.grey.shade200,
      child: Icon(
        Icons.person,
        size: 60,
        color: Colors.grey.shade400,
      ),
    );
  }

  Widget _buildInfoRow({
    required IconData icon,
    required String label,
    required String value,
  }) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 16.0),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Icon(
            icon,
            size: 20,
            color: Colors.blue.shade600,
          ),
          const SizedBox(width: 12),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  label,
                  style: const TextStyle(
                    fontSize: 12,
                    fontWeight: FontWeight.w500,
                    color: Colors.grey,
                  ),
                ),
                const SizedBox(height: 4),
                Text(
                  value,
                  style: const TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class AppRoutes {
  static const String home = '/home';
  static const String userDetail = '/user-detail';
}

import 'package:flutter_test_case/bindings/home_binding.dart';
import 'package:flutter_test_case/bindings/user_detail_binding.dart';
import 'package:flutter_test_case/pages/home_page.dart';
import 'package:flutter_test_case/pages/user_detail_page.dart';
import 'package:flutter_test_case/utils/app_routes.dart';
import 'package:get/get.dart';

class AppPages {
  static const initial = AppRoutes.home;

  static final routes = [
    GetPage(
      name: AppRoutes.home,
      page: () => const HomePage(),
      binding: HomeBinding(),
    ),
    GetPage(
      name: AppRoutes.userDetail,
      page: () => const UserDetailPage(),
      binding: UserDetailBinding(),
    ),
  ];
}

// ...existing code...

// Di dalam ListView.builder, tambahkan onTap pada ListTile atau Container
ListTile(
  // ...existing properties...
  onTap: () {
    Get.toNamed(
      AppRoutes.userDetail,
      arguments: controller.usersState[index],
    );
  },
  // ...existing code...
)
Editor is loading...
Leave a Comment