flutter图片选择

包来自:https://pub.dev/packages/image_picker
git来自:https://github.com/flutter/plugins/tree/master/packages/image_picker/image_picker
示例

1、引入

  image_picker: ^0.8.4 #图片选择

2、使用

import 'package:image_picker/image_picker.dart';

3、代码

  //事先声明
  final ImagePicker _picker = ImagePicker();
  //拍照
  _takePhoto() async {
    final XFile? image = await _picker.pickImage(source: ImageSource.camera);
    setState(() {
      _imgPath = File(image!.path);
    });
  }
  
  // 打开相册-单张
  _openGallery() async {
    final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
    setState(() {
      _imgPath = File(image!.path);
    });
  }

  // 打开相册-多张
  _openMultiImage() async {
    final List<XFile>? images = await _picker.pickMultiImage();

    setState(() {
      images!.forEach((value) {
        imagesList!.add(File(value.path));
      });
      print('选择了-${images.length}-张照片');
    });
  }

  //拍视频
  _takeVideo() async {
    final XFile? image = await _picker.pickVideo(source: ImageSource.camera);
    setState(() {
      // TODO 未做视频播放
      // _imgPath = File(image!.path);
    });
  }

  //选择视频
  _openVideo() async {
    final XFile? image = await _picker.pickVideo(source: ImageSource.gallery);
    setState(() {
      // TODO 未做视频播放
      // _imgPath = File(image!.path);
    });
  }

完整代码

import 'dart:io';

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

///@作者: Q.L
///@创建日期: 2021-09-03 11:28
///@描述: {照片选择}
class ImagesChoosePage extends StatefulWidget {
  const ImagesChoosePage({Key? key}) : super(key: key);

  @override
  _ImagesChoosePageState createState() => _ImagesChoosePageState();
}

class _ImagesChoosePageState extends State<ImagesChoosePage> {
  final ImagePicker _picker = ImagePicker();
  File? _imgPath;
  List? imagesList = [];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('图片选择'),
      ),
      body: ListView(
        children: [
          Container(
            width: 100,
            height: 100,
            child: _imgPath != null
                ? Image.file(
                    _imgPath!,
                    fit: BoxFit.fill,
                    width: 100,
                    height: 100,
                  )
                : Text('请选择单张图片'),
          ),
          ElevatedButton(
            onPressed: _takePhoto,
            child: Text("拍照"),
          ),
          ElevatedButton(
            onPressed: _openGallery,
            child: Text("选择照片-单张"),
          ),
          ElevatedButton(
            onPressed: _openMultiImage,
            child: Text("选择照片-多张"),
          ),
          ElevatedButton(
            onPressed: _takeVideo,
            child: Text("拍视频"),
          ),
          ElevatedButton(
            onPressed: _openVideo,
            child: Text("选择视频"),
          ),
          Container(
            width: double.infinity,
            height: 1000,
            child: GridView.builder(
                padding: EdgeInsets.all(0),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3, crossAxisSpacing: 2, mainAxisSpacing: 2),
                itemBuilder: (BuildContext context, int index) {
                  if (imagesList != null) {
                    return Image.file(
                      imagesList![index]!,
                      fit: BoxFit.fill,
                      width: 100,
                      height: 100,
                    );
                  } else {
                    return Container();
                  }
                },
                itemCount: imagesList!.length),
          ),
        ],
      ),
    );
  }

  //拍照
  _takePhoto() async {
    final ImagePicker _picker = ImagePicker();
    final XFile? image = await _picker.pickImage(source: ImageSource.camera);
    setState(() {
      _imgPath = File(image!.path);
    });
  }

  // 打开相册-单张
  _openGallery() async {
    final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
    setState(() {
      _imgPath = File(image!.path);
    });
  }

  // 打开相册-多张
  _openMultiImage() async {
    final List<XFile>? images = await _picker.pickMultiImage();

    setState(() {
      images!.forEach((value) {
        imagesList!.add(File(value.path));
      });
      print('选择了-${images.length}-张照片');
    });
  }

  //拍视频
  _takeVideo() async {
    final XFile? image = await _picker.pickVideo(source: ImageSource.camera);
    setState(() {
      // TODO 未做视频播放
      // _imgPath = File(image!.path);
    });
  }

  //选择视频
  _openVideo() async {
    final XFile? image = await _picker.pickVideo(source: ImageSource.gallery);
    setState(() {
      // TODO 未做视频播放
      // _imgPath = File(image!.path);
    });
  }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
acility); } void Playground::removeFacility(int index) { facilities_.erase(facilities_.begin() + index); } double Playground::getFacilityIncomeOnDate(int index, const std::string &date) const { return facilities_[index].getFlutter 中可以使用 image_picker 插件来实现图片选择器功能,同时可以根据自己的需求对图库样IncomeOnDate(date); } double Playground::getTotalIncomeOnDate(const std::string &date) const { double income = 式进行修改。 下面是一个基于 image_picker 插件的图片选择器示例,其中使用自定义的图库样0; for (auto facility : facilities_) { income += facility.getIncomeOnDate(date); } return income; } 式: ```dart import 'dart:io'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; void maindouble Playground::getTotalIncome() const { double income = 0; for (auto facility : facilities_) { income +=() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: facility.getTotalIncome(); } return income; } std::vector<PlayFacility> Playground::getSortedFacilitiesByName() const 'Image Picker Demo', home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _My { std::vector<PlayFacility> facilities = facilities_; std::sort(facilities.begin(), facilities.end(), PlayFacHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { File _image; Futureility::sortByName); return facilities; } std::vector<PlayFacility> Playground::getSortedFacilitiesByIncome() const getImage() async { var image = await ImagePicker.pickImage(source: ImageSource.gallery); setState(() { _image = image; { std::vector<PlayFacility> facilities = facilities_; std::sort(facilities.begin(), facilities.end(), PlayFac }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('ility::sortByIncome); return facilities; } void Playground::saveToFile(const std::string &filename) const { FileIO::Image Picker Demo'), ), body: Center( child: _image == null ? Text('No image selected.') :writeToFile(filename, facilities_); } ``` 主函数(main.cpp): ```c++ #include <iostream> #include "Playground.h" int main() { Playground playground("playground.txt"); int option = 0; do { std::cout << " Image.file(_image), ), floatingActionButton: FloatingActionButton( onPressed: getImage, tooltip: 'Pick Image', child:1. Add a new facility" << std::endl; std::cout << "2. Remove a facility" << std::endl; Icon(Icons.add_a_photo), ), ); } } ``` 在这个示例中,我们使用 ImagePicker.pickImage() std::cout << "3. Get income of a facility on a date" << std::endl; std::cout << "4 方法来选择图片,通过传递 ImageSource.gallery 参数来打开图库。你可以根据自己的需求来修改图库的样式,比如使用自定义的按钮、图标、字体等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值