dart - Flutter-如何打开手机图库?

标签 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

最佳答案

如果您只想从图库中选择,那么您可以使用 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),
      ),
    );
  }
}

关于dart - Flutter-如何打开手机图库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51554755/

相关文章:

android - Flutter应用程序无法在真实设备或仿真器上运行

asynchronous - 如何使用异步函数异步监听 Firestore 中的值?

Flutter:如何检查对象是否是类的实例(有状态或无状态小部件)

flutter - Dart/Flutter 中 Intl 包的 NumberFormat 的反直觉结果

forms - Dart不允许我使用POST方法

flutter - 如何在 Flutter web 中使用 .env?

flutter : Custom Painter draw path with different fill and stroke color

flutter - 如何为我的 flutter web 应用程序配置图标?

dart - 如何使列垂直滚动?

android - Flutter Listview 在使用 FutureBuilder 加载缩略图时卡住