如何在flutter中使用image_picker

时间:2019-04-19 13:26:28

标签: flutter

我对如何使用image_picker感到困惑,这就是我在应用程序中使用它的方式(就像在许多教程中一样):

//first is serialize
$result = serialize($array);
//second method 
$result = json_encode($array);

问题是,上面的代码无法启动ImagePicker,当我单击FlatButton时,上面的代码只是产生一个错误class AddDialogState extends State<AddDialog> { File galleryFile; Widget _onlyStatus() { getLocalImage() async { var _galleryFile = await ImagePicker.pickImage( source: ImageSource.gallery }; setState(() { galleryFile = _galleryFile; }); print(_galleryFile.path); } return Column( ........ FlatButton.icon( onPressed: () { getLocalImage(); } ) ) } @override Widget build(BuildContext context) { // fullscreen dialog ......... body: _onlyStatus() } } ,它没有启动与图库相关的任何新活动,所以怎么了用我的代码?

3 个答案:

答案 0 :(得分:0)

这里是我的示例,您只需要调用SelectionItem并传递所需的参数即可完成,它还包括对Android和iOS的支持。

class SelectionItem {
  String text;
  GestureTapCallback action;

  SelectionItem({
    this.text,
    this.action,
  });
}

Widget _getOptionWidget(SelectionItem item) {
  return SimpleDialogOption(
    onPressed: item.action,
    child: Text(item.text),
  );
}

Widget _getCupertinoOptionWidget(SelectionItem item) {
  return CupertinoActionSheetAction(
    onPressed: item.action,
    child: Text(item.text),
  );
}

showSelector(
  BuildContext context, {
  String title,
  List<SelectionItem> actions,
}) {
  bool isAndroid = Injector.instance.platform == Platform.ANDROID;

  if (isAndroid) {
    final items = actions.map((action) => _getOptionWidget(action)).toList();
    return SimpleDialog(
      title: Text(title),
      children: items,
    );
  } else {
    final items =
        actions.map((action) => _getCupertinoOptionWidget(action)).toList();

    return Column(
      mainAxisAlignment: MainAxisAlignment.end,
      children: <Widget>[
        CupertinoActionSheet(
          title: Text(title),
          actions: items,
          cancelButton: CupertinoActionSheetAction(
            onPressed: () {
              NavigationUtils.pop(context);
            },
            child: Text("Cancel"),
          ),
        ),
      ],
    );
  }
}

答案 1 :(得分:0)

您的showDialog应该是这样的:

Container(
          width: 300,
          height: 300,
          child: GestureDetector(
            onTap: () {
              showDialog(
                context: context,
                builder: (BuildContext context) => showSelector(
                      context,
                      "Select",
                      actions: [
                        SelectionItem(
                          "Camera",
                          action: () {
                            getImage(ImageSource.camera);
                            Navigator.of(context).pop();
                          },
                        ),
                        SelectionItem(
                          "Gallery",
                          action: () {
                            getImage(ImageSource.gallery);
                            Navigator.of(context).pop();
                          },
                        )
                      ],
                    ),
              );
            },
          ),
        )

答案 2 :(得分:0)

flutter imagepicker- pickimage deprecated.

实际上不推荐使用pickImage()。因此,您必须使用ImagePicker.getImage(source:ImageSource.gallery)

Click here for more

void getImage(ImageSource imageSource) async {
    PickedFile imageFile = await picker.getImage(source: imageSource);
    if (imageFile == null) return;
    File tmpFile = File(imageFile.path);
    final appDir = await getApplicationDocumentsDirectory();
    final fileName = basename(imageFile.path);
    tmpFile = await tmpFile.copy('${appDir.path}/$fileName');
    setState(() {
      _image = tmpFile;
    
    });
  }

该代码还将图像文件存储在设备目录中。还使用路径换行符。