Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.6 kB
1
Indexable
Never
class AccountScreenAvatar extends HookWidget {
  const AccountScreenAvatar({
    Key? key,
  }) : super(key: key);

  static final imageCubit = getIt<SpsChecklistImageCubit>();

  @override
  Widget build(BuildContext context) {
    useEffect(() {
      imageCubit.getToken();
      return;
    }, []);

    var localStorage = getIt<LocalStorage>();
    Future<String> getToken() async {
      var token = await getIt<AuthLocalDataSource>().getAccessToken();
      return token;
    }

    Future<dynamic> getUserLocal() async {
      var box = await localStorage.box('user');
      var user = box.get('user');
      return user;
    }

    var imageProfile = useState<File?>(null);
    var authToken = useState<String?>('');
    var user = useState<dynamic>(null);

    useEffect(() {
      getToken().then((value) => authToken.value = value);
      getUserLocal().then((value) => user.value = value);
    }, []);

    return BlocBuilder<UserCubit, UserState>(
      builder: (context, state) {
        return Badge(
          badgeColor: Colors.white,
          position: BadgePosition.bottomEnd(),
          toAnimate: false,
          badgeContent: SizedBox(
            height: 34,
            width: 34,
            child: IconButton(
              padding: EdgeInsets.zero,
              onPressed: () async {
                FilePickerResult? result = await FilePicker.platform.pickFiles(
                  type: FileType.image,
                  withData: true,
                );

                if (result == null) return;

                imageProfile.value = File(result.files.single.path!);
                var dataId =
                    state.user == null ? user.value['id_user'] : state.user?.id;
                onChange(imageProfile.value!, dataId);
              },
              icon: Icon(
                Icons.photo_camera_rounded,
                color: Colors.grey[800],
                size: 24.sp,
              ),
            ),
          ),
          child: Container(
            width: 110.w,
            height: 110.w,
            alignment: Alignment.center,
            decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: AppColor.background,
                boxShadow: [
                  BoxShadow(
                    blurRadius: 4,
                    spreadRadius: 1,
                    offset: const Offset(0, 2),
                    color: Colors.black.withOpacity(0.05),
                  )
                ]),
            child: ClipOval(
              child: imageProfile.value != null
                  ? Image.file(
                      imageProfile.value!,
                      fit: BoxFit.cover,
                    )
                  : state.user?.userFile?.isNotEmpty ??
                          user.value?['userFile'].isNotEmpty
                      ? Image.network(
                          '${NetworkURL.base}/data/gambar/${user.value?['userFile'][0]['path']}',
                          headers: {
                            'cookie': 'key=${authToken.value}',
                            'referer': 'survey.ops'
                          },
                        )
                      : Text(
                          'U',
                          style: TextStyle(
                            fontSize: 45.sp,
                            color: AppColor.primary,
                          ),
                        ),
            ),
          ),
        );
      },
    );
  }

  void onChange(File file, int dataId) async {
    var cubit = getIt<UserCubit>();
    await cubit.updatePhoto(dataId, file);
  }
}