import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vitalapp/architecture/utils/prompt_box.dart';
import 'package:vitalapp/global.dart';

class HomeAvatarWidget extends StatelessWidget {
  final double size;
  final String headImageToken;

  const HomeAvatarWidget({
    super.key,
    required this.size,
    required this.headImageToken,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: _buildAvatar(),
      onTap: () {
        if (kIsOnline) {
          Get.toNamed(
            '/home/personal_center',
          );
        } else {
          PromptBox.toast("此功能仅网络在线时可用");
        }
      },
    );
  }

  Widget _buildAvatar() {
    // return SizedBox();
    if (headImageToken.isNotEmpty) {
      return ClipRRect(
        borderRadius: BorderRadius.circular(50 / 2),
        child: Image.network(
          headImageToken,
          width: 50,
          height: 50,
          fit: BoxFit.cover,
          loadingBuilder: (context, child, progress) {
            if (progress == null ||
                progress.cumulativeBytesLoaded == progress.expectedTotalBytes) {
              return Image.network(
                headImageToken,
                width: 50,
                height: 50,
                fit: BoxFit.cover,
              );
            }

            return const CircularProgressIndicator(color: Colors.white);
          },
          errorBuilder: (context, error, stackTrace) {
            return Image.asset(
              "assets/images/avatar.png",
              width: 50,
              height: 50,
              fit: BoxFit.cover,
            );
          },
        ),
      );
    }
    return ClipRRect(
      borderRadius: BorderRadius.circular(50 / 2),
      child: Image.asset(
        'assets/images/avatar.png',
        width: 50,
        height: 50,
        fit: BoxFit.cover,
      ),
    );
  }
}
