Flutter-如何打开电话画廊?

时间:2018-07-27 09:19:40

标签: dart flutter

我想在这里打开图库目录,我正在使用此代码,但显示了所有内容 选项,我想打开唯一的图库。

List images;
  int maxImageNo = 10;
  bool selectSingleImage = false;
  File _imageFile;
  _pickImageFromGallery() async {
    File file;
    String result;
    try {
      result = await FlutterImagePickCrop.pickAndCropImage(_gallery);
    } on PlatformException catch (e) {
      result = e.message;
      print(e.message);
    }
    if (!mounted) return;

    setState(() {
      imageFile = new File(result);
      _platformMessage = result;
    });}

enter image description here

1 个答案:

答案 0 :(得分:1)

如果只想从图库中选择,则可以使用image_picker插件。如果裁切对您很重要,那么我将重新考虑我的答案,因为image_picker没有提供答案。

import 'package:image_picker/image_picker.dart';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  File _image;

  Future getImage() async {
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);

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

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Image Picker Example'),
      ),
      body: new Center(
        child: _image == null
            ? new Text('No image selected.')
            : new Image.file(_image),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: getImage,
        tooltip: 'Pick Image',
        child: new Icon(Icons.add_a_photo),
      ),
    );
  }
}