【问题标题】:How to add an image from camera in flutter?如何在颤动中添加来自相机的图像?
【发布时间】:2019-05-15 11:28:29
【问题描述】:

我在我的应用中为图像创建了一个占位符,单击时我希望能够打开相机,并显示在占位符中单击的图像。我为此使用了 image_picker 包。我用 GestureDetector 包裹了占位符容器,但是在点击容器时没有任何反应。我该如何解决这个问题?

在包含无状态小部件的主文件中,我添加了有状态小部件 addImage()

Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[

                  addImage(),  // my stateful widget 
                TextField(

                  )
                ),              
                Row(
                 //other implementation here 
                  ],
                )

              ]
            )

我的 addImage 有状态小部件如下所示

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';


class addImage extends StatefulWidget{
  @override
  _addImageState createState() => _addImageState();
}

class _addImageState extends State<addImage> {

  File _image;

  Future getImagefromCamera() async{

    var image = await ImagePicker.pickImage(source: ImageSource.camera);

    setState(() {
      _image = image;
    });
  }


  @override
  Widget build(BuildContext context) {

    return GestureDetector(
      onTap: getImagefromCamera,
      child: _image == null ? Container(decoration: BoxDecoration(color: Colors.red[50],border: Border.all(color: Colors.red[200], width: 1.0),borderRadius: BorderRadius.circular(10.0)),
          child: Column(
            children: <Widget>[
              SizedBox(height:30.0),
              Icon(Icons.camera_alt, color: Colors.red),
              SizedBox(height: 10.0),
              Text('Take Image of the Item', style: TextStyle(color: Colors.red)),
              SizedBox(height: 30.0)
            ],
          )) : Image.file(_image),
    );
  }

}

我什至在 pubspec.yaml 中添加了依赖项,但是仍然不起作用

 image_picker: ^0.4.5

【问题讨论】:

    标签: flutter


    【解决方案1】:

    我可以使用下面的代码查看相机拍摄的图像:

    import 'dart:async';
    import 'dart:io';
    import 'package:flutter/material.dart';
    import 'package:image_picker/image_picker.dart';
    
    void main() {
      runApp(new MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return MaterialApp(
          home: Scaffold(
            body: addImage()
          )
        );
      }
    }
    
    class addImage extends StatefulWidget{
      @override
      _addImageState createState() => _addImageState();
    }
    
    class _addImageState extends State<addImage> {
    
      File _image;
    
      Future getImagefromCamera() async{
    
        var image = await ImagePicker.pickImage(source: ImageSource.camera);
    
        setState(() {
          _image = image;
        });
      }
    
    
      @override
      Widget build(BuildContext context) {
    
        return Scaffold(
    
            body: GestureDetector(
              onTap: getImagefromCamera,
              child: _image == null ? Container(decoration: BoxDecoration(color: Colors.red[50],border: Border.all(color: Colors.red[200], width: 1.0),borderRadius: BorderRadius.circular(10.0)),
                  child: Column(
                    children: <Widget>[
                      SizedBox(height:30.0),
                      Icon(Icons.camera_alt, color: Colors.red),
                      SizedBox(height: 10.0),
                      Text('Take Image of the Item', style: TextStyle(color: Colors.red)),
                      SizedBox(height: 30.0)
                    ],
                  )) : Image.file(_image),
            )
    
        );
    
      }
    
    }
    

    在 Android 模拟器上测试。

    【讨论】:

    • 点击后如何让图片替换容器?它超越了限制
    【解决方案2】:

    试试这个代码..

     image_picker: ^0.5.2
    

    尝试此代码后..

    Future<File> imageFile;
    
    pickImageFromGallery(ImageSource source) {
    setState(() {
      imageFile = ImagePicker.pickImage(source: source);
    });
    }
    
     Widget showImage() {
     return FutureBuilder<File>(
      future: imageFile,
      builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
        if (snapshot.connectionState == ConnectionState.done &&
            snapshot.data != null) {
          return Image.file(
            snapshot.data,
            width: 300,
            height: 300,
          );
        } else if (snapshot.error != null) {
          return const Text(
            'Error Picking Image',
            textAlign: TextAlign.center,
          );
        } else {
          return const Text(
            'No Image Selected',
            textAlign: TextAlign.center,
          );
        }
      },
    );
    }
    
    @override
    Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            showImage(),
            RaisedButton(
              child: Text("Select Image from Gallery"),
              onPressed: () {
                pickImageFromGallery(ImageSource.camera);
              },
            ),
          ],
        ),
      ),
    );
     }
    

    【讨论】:

    • 这仍然无法正常工作...它调用 pickImageFromGallery() 函数,甚至没有单击容器并在 ios 模拟器中显示“错误选择图像”。在安卓模拟器上,只要我导航到屏幕,相机就会打开,然后显示错误
    • 我这样编辑我的代码,没有得到想要的结果```
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2022-11-30
    • 2013-05-14
    • 2020-03-17
    • 1970-01-01
    • 2020-10-25
    • 2019-11-05
    相关资源
    最近更新 更多