【问题标题】:Flutter display image picked from gallery从图库中挑选的颤振显示图像
【发布时间】:2021-06-30 21:52:35
【问题描述】:

我只是想选择一张图片并将其显示在我的应用程序中。为此,我使用Flutter Image Picker。我添加了所有依赖项,我可以选择一个图像但我​​无法显示它......

这是我尝试过的:

class _AddMemoryPageState extends State<AddMemoryPage> {
  final picker = ImagePicker();
  late Future<PickedFile?> pickedFile;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.secondary,
      body: SafeArea(
        child: Column(
          children: [
            Row(
              children: [
                Padding(
                  padding: EdgeInsets.only(
                    top: 15,
                    left: 25,
                  ),
                  child: IconButtonWithExpandedTouchTarget(
                    onTapped: () {
                      Navigator.pop(context);
                    },
                    svgPath: 'assets/icons/down.svg',
                  ),
                ),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                IconButtonWithExpandedTouchTarget(
                  onTapped: () async {
                    pickedFile = picker
                        .getImage(
                          source: ImageSource.camera,
                        )
                        .whenComplete(() => {setState(() {})});
                  },
                  svgPath: 'assets/icons/camera.svg',
                ),
                SizedBox(
                  width: scaleWidth(50),
                ),
                IconButtonWithExpandedTouchTarget(
                  onTapped: () async {
                    pickedFile = picker
                        .getImage(
                          source: ImageSource.gallery,
                        )
                        .whenComplete(() => {setState(() {})});
                  },
                  svgPath: 'assets/icons/gallery.svg',
                ),
              ],
            ),
            FutureBuilder(
              future: pickedFile,
              builder: (context, data) {
                if (data.hasData) {
                  return Container(
                    height: 200.0,
                    child: Image.file(
                      data.data as File,
                      fit: BoxFit.contain,
                      height: 200.0,
                    ),
                    color: Colors.blue,
                  );
                }
                return Container(
                  height: 200.0,
                  color: Colors.blue,
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}

但这不起作用:

LateInitializationError: 字段 'pickedFile' 尚未初始化。

我在这里缺少什么?处理这个问题的正确方法是什么?我找不到任何关于此的最新教程/文档。如果您需要更多信息,请告诉我!

【问题讨论】:

    标签: flutter dart image-gallery flutter-futurebuilder imagepicker


    【解决方案1】:

    您基本上忘记在文件中转换 de PickerFile 并初始化变量pickFile late Future&lt;PickedFile?&gt; pickedFile = Future.value(null);

    要在文件中转换 PickerFile,您只需:

    File(data.data!.path)
    

    我对您的代码进行了一些修改,现在可以显示图像:

      final picker = ImagePicker();
      late Future<PickedFile?> pickedFile = Future.value(null);
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.blueGrey,
          body: SafeArea(
            child: Column(
              children: [
                Row(
                  children: [
                    Padding(
                      padding: EdgeInsets.only(
                        top: 15,
                        left: 25,
                      ),
                      child: IconButton(
                        onPressed: () {
                          Navigator.pop(context);
                        },
                        icon: Icon(Icons.download_rounded),
                      ),
                    ),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    IconButton(
                      onPressed: () async {
                        pickedFile = picker.getImage(source: ImageSource.camera).whenComplete(() => {setState(() {})});
                      },
                      icon:Icon(Icons.add)
                    ),
                    SizedBox(
                      width: 100,
                    ),
                    IconButton(
                      onPressed: () async {
                        pickedFile = picker
                            .getImage(
                          source: ImageSource.gallery,
                        )
                            .whenComplete(() => {setState(() {})});
                      },
                      icon:Icon(Icons.photo_camera_back),
                    ),
                  ],
                ),
                FutureBuilder<PickedFile?>(
                  future: pickedFile,
                  builder: (context, snap) {
                    if (snap.hasData) {
                      return Container(
                        child: Image.file(
                          File(snap.data!.path),
                          fit: BoxFit.contain,
                        ),
                        color: Colors.blue,
                      );
                    }
                    return Container(
                      height: 200.0,
                      color: Colors.blue,
                    );
                  },
                ),
              ],
            ),
          ),
        );
      }
    

    【讨论】:

    • 这是否适合您?如果我选择一个图像,我不会在我的屏幕上看到它。蓝色的容器停在那里
    【解决方案2】:

    你能尝试在 setState 中初始化 'pickedFile' 吗?并且 PickedFile 类型不是文件类型,因此请像 image_picker 文档 final pickedFile = await _picker.getImage(...); final File file = File(pickedFile.path); 中所述的“Image.file(data.data.path)”进行转换。

    【讨论】:

    • 我应该如何在 setState 中初始化?无法使其与 FutureBuilder 一起使用
    【解决方案3】:

    这是一个简单的图像拾取和在小部件上显示图像的版本。希望这对你有用

    ImagePicker picker = ImagePicker();
    var imageFile;
    _getFromGallery() async {
    var pickedFile = (await picker.pickImage(
     source: ImageSource.gallery,
      ));
    if (pickedFile != null) {
     setState(() {
     imageFile = File(pickedFile.path);
      });
    }
    }
    Widget _imageSection() {
      return (imageFile == null)
         ? Container(
               width:
               MediaQuery.of(context).size.width,
               height: 220,
               child: Card(
                      elevation: 3.0,
                       color: Colors.white,
                       shadowColor: Colors.grey,
                       child: Text("No image selected"),
                      ),
                      )
                 : Container(
                  width: MediaQuery.of(context).size.width,
                   height: 220,
                  child: Column(
                 children: [
                    Image.file(imageFile,
                   fit: BoxFit.cover,
                     ),
                   ],
                 ),
              );
           }
    
         @override
          Widget build(BuildContext context) {
             return Scaffold(
               appBar: AppBar(
              leading: IconButton(
                onPressed: () {},
                icon: const Icon(Icons.exit_to_app),
               ),
                ),
           body: SingleChildScrollView(
            child: Center(
      child: Column(
          mainAxisAlignment:
              MainAxisAlignment.start,
          children: <Widget>[
            Container(
              child: Padding(
                padding: const EdgeInsets.fromLTRB(
                    0, 0, 0, 0),
                child: ElevatedButton(
                  onPressed: () {
                    _getFromGallery();
                    print(imageFile);
                  },
                  child: Text(
                      'Select Photo'),
                ),
              ),
            ),
            _imageSection(),
          ]),
          ),
        ),
       );
      }
    

    【讨论】:

      猜你喜欢
      • 2020-01-12
      • 2020-11-15
      • 2022-01-08
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      • 2019-08-30
      相关资源
      最近更新 更多