-1

I wanna ask some help with my codes. I am lost in which part pf the code I am going to put this image: DecorationImage(image: FileImage(imageFile!), in the scanscreen.dart. I am trying to follow this tutorial from Pick Images from Gallery and Camera in Flutter (Quality Compression) . I am trying to replace the image imgRectangle2362 with the captured image from camera, here is my whole code attached

Here is my scan_screen.dart codes

import 'dart:io';
import 'controller/scan_controller.dart';
import 'package:flutter/material.dart';
import 'package:grabbit/core/app_export.dart';
import 'package:image_picker/image_picker.dart';
import 'package:grabbit/widgets/custom_button.dart';


class ScanScreen extends StatefulWidget {
  const ScanScreen({Key? key}) : super(key: key);
  @override
  State<ScanScreen> createState() => _ScanScreenState();
}

class _ScanScreenState extends State<ScanScreen> {
  File? imageFile;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: ColorConstant.whiteA700,
        body: Container(
          width: size.width,
          child: SingleChildScrollView(
            child: Container(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Container(
                    height: getVerticalSize(
                      434.00,
                    ),
                    width: getHorizontalSize(
                      326.00,
                    ),
                    margin: getMargin(
                      left: 17,
                      top: 135,
                      right: 17,
                    ),
                    child: Stack(
                      alignment: Alignment.topCenter,
                      children: [
                        Align(
                          alignment: Alignment.center,
                          child: Container(
                            height: getVerticalSize(
                              434.00,
                            ),
                            width: getHorizontalSize(
                              325.00,
                            ),
                            margin: getMargin(
                              left: 1,
                            ),
                            child: Card(
                              clipBehavior: Clip.antiAlias,
                              elevation: 0,
                              margin: EdgeInsets.all(0),
                              color: ColorConstant.bluegray100,
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(
                                  getHorizontalSize(
                                    15.00,
                                  ),
                                ),
                              ),
                              child: Stack(
                                children: [
                                  Align(
                                    alignment: Alignment.bottomCenter,
                                    child: Padding(
                                      padding: getPadding(
                                        left: 121,
                                        top: 27,
                                        right: 121,
                                        bottom: 27,
                                      ),
                                      child: CommonImageView(
                                        imagePath: ImageConstant.imgCameraicon,
                                        height: getVerticalSize(
                                          77.00,
                                        ),
                                        width: getHorizontalSize(
                                          82.00,
                                        ),
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),
                        Align(
                          alignment: Alignment.topCenter,
                          child: Padding(
                            padding: getPadding(
                              top: 26,
                              right: 1,
                              bottom: 26,
                            ),

                            child: CommonImageView(
                             image: DecorationImage(
                             image: FileImage(imageFile!),
                             fit: BoxFit.cover
                              ),
                              imagePath: ImageConstant.imgRectangle2362,
                              height: getVerticalSize(
                                281.00,
                              ),
                              width: getHorizontalSize(
                                325.00,
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  CustomButton(
                    width: 267,
                    text: "Next",
                    margin: getMargin(
                      left: 17,
                      top: 135,
                      right: 17,
                      bottom: 20,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  
  onTapImgCameraicon() async {
    await PermissionManager.askForPermission(Permission.camera);
    await PermissionManager.askForPermission(Permission.storage);
    List<String?>? imageList = [];
//TODO: Permission - use imageList for using selected images
    await FileManager().showModelSheetForImage(getImages: (value) async {
      imageList = value;
    });
  }

  void getImage({required ImageSource source}) async {
    final file = await ImagePicker().pickImage(
        source: source,
        maxWidth: 640,
        maxHeight: 480,
        imageQuality: 70 //0 - 100
        );

    if (file?.path != null) {
      setState(() {
        imageFile = File(file!.path);
      });
    }
  }
}

  onTap() {
    Get.toNamed(AppRoutes.homepageScreen);
  }

common_image_view.dart

// ignore_for_file: must_be_immutable

import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

class CommonImageView extends StatelessWidget {
  ///[url] is required parameter for fetching network image
  String? url;
  String? imagePath;
  String? svgPath;
  File? file;
  double? height;
  double? width;
  final BoxFit fit;
  final String placeHolder;

  ///a [CommonNetworkImageView] it can be used for showing any network images
  /// it will shows the placeholder image if image is not found on network
  CommonImageView({
    this.url,
    this.imagePath,
    this.svgPath,
    this.file,
    this.height,
    this.width,
    this.fit = BoxFit.fill,
    this.placeHolder = 'assets/images/image_not_found.png',
  });

  @override
  Widget build(BuildContext context) {
    return _buildImageView();
  }

  Widget _buildImageView() {
    if (svgPath != null && svgPath!.isNotEmpty) {
      return Container(
        height: height,
        width: width,
        child: SvgPicture.asset(
          svgPath!,
          height: height,
          width: width,
          fit: fit,
        ),
      );
    } else if (file != null && file!.path.isNotEmpty) {
      return Image.file(
        file!,
        height: height,
        width: width,
        fit: fit,
      );
    } else if (url != null && url!.isNotEmpty) {
      return CachedNetworkImage(
        height: height,
        width: width,
        fit: fit,
        imageUrl: url!,
        placeholder: (context, url) => Container(
          height: 30,
          width: 30,
          child: LinearProgressIndicator(
            color: Colors.grey.shade200,
            backgroundColor: Colors.grey.shade100,
          ),
        ),
        errorWidget: (context, url, error) => Image.asset(
          placeHolder,
          height: height,
          width: width,
          fit: fit,
        ),
      );
    } else if (imagePath != null && imagePath!.isNotEmpty) {
      return Image.asset(
        imagePath!,
        height: height,
        width: width,
        fit: fit,
      );
    }
    return SizedBox();
  }
}
John Michael Tan
  • 129
  • 3
  • 10

1 Answers1

0

According to error, there is no image parameters on your CommonImageView

File? imageFile;

You can directly provide nullable file on CommonImageView(file: imageFile)

CommonImageView(
  file: imageFile,
  imagePath: ImageConstant.imgRectangle2362,
  height: getVerticalSize(
    281.00,
  ),
  width: getHorizontalSize(
    325.00,
  ),

More about null-safety

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56