flutter_photokit 0.1.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new78

iOS PhotoKit Plugin for Flutter

A Flutter plugin to interact with PhotoKit on iOS.

Usage

To use this plugin, add flutter_photokit as a dependency in your pubspec.yaml file

Then, include the NSPhotoLibraryAddUsageDescription permission in your Info.plist

Example

For an example on how to use this plugin, check out the sample app

For simplicity, the sample app just shows an example of saving a photo, but this plugin will work for videos as well! Usage is exactly the same.

0.1

  • Initial Release

example/lib/main.dart

import 'dart:async';
import 'dart:io';

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

import 'package:flutter_photokit/flutter_photokit.dart';
import 'package:path_provider/path_provider.dart';

enum _SaveStatus { NONE, SAVING, SAVED }

void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  _SaveStatus _cameraRollStatus = _SaveStatus.NONE;
  _SaveStatus _albumStatus = _SaveStatus.NONE;
  TextEditingController _albumTextController;

  static HttpClient _httpClient = HttpClient();
  static String _sampleImageUrl =
      'https://cdn-images-1.medium.com/max/1200/1*5-aoK8IBmXve5whBQM90GA.png';
  // Use this link to test with a video.
  // static String _sampleVideoUrl = 'https://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4';

  @override
  void initState() {
    super.initState();

    _albumTextController = TextEditingController()
      ..addListener(() => setState(() {}));
  }

  Future<File> _downloadFile(String url) async {
    Uri uri = Uri.parse(url);

    HttpClientRequest req = await _httpClient.getUrl(uri);
    HttpClientResponse res = await req.close();

    List<int> bytes = await consolidateHttpClientResponseBytes(res);
    String tempDir = (await getTemporaryDirectory()).path;
    File outputFile = File('$tempDir/${uri.pathSegments.last}');
    await outputFile.writeAsBytes(bytes);

    return outputFile;
  }

  void _saveToCameraRoll() async {
    setState(() => _cameraRollStatus = _SaveStatus.SAVING);

    File file = await _downloadFile(_sampleImageUrl);
    await FlutterPhotokit.saveToCameraRoll(filePath: file.path);

    setState(() => _cameraRollStatus = _SaveStatus.SAVED);
  }

  void _saveToAlbum() async {
    setState(() => _albumStatus = _SaveStatus.SAVING);

    File file = await _downloadFile(_sampleImageUrl);
    await FlutterPhotokit.saveToAlbum(
        filePath: file.path, albumName: _albumTextController.value.text);

    setState(() => _albumStatus = _SaveStatus.SAVED);
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
          appBar: new AppBar(
            title: const Text('Flutter PhotoKit Example'),
          ),
          body: _appBody()),
    );
  }

  Widget _appBody() {
    if (Platform.isIOS) {
      return SingleChildScrollView(
          child: new Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          Image.network(_sampleImageUrl),
          SizedBox(height: 16.0),
          _saveToCameraRollRow(),
          SizedBox(height: 16.0),
          _saveToAlbumRow()
        ],
      ));
    } else {
      return Center(
          child: Text('The PhotoKit plugin is only available for iOS!'));
    }
  }

  Widget _saveToCameraRollRow() {
    switch (_cameraRollStatus) {
      case _SaveStatus.NONE:
        return RaisedButton(
            onPressed: _saveToCameraRoll, child: Text('Save to Camera Roll'));
      case _SaveStatus.SAVING:
        return CircularProgressIndicator();
      case _SaveStatus.SAVED:
        return Icon(Icons.check);
    }
  }

  Widget _saveToAlbumRow() {
    switch (_albumStatus) {
      case _SaveStatus.NONE:
        return Row(
          children: <Widget>[
            Expanded(
              child: TextField(
                  controller: _albumTextController,
                  decoration: InputDecoration(labelText: 'Album name')),
            ),
            RaisedButton(
              onPressed: _albumTextController.value.text.trim().isEmpty
                  ? null
                  : _saveToAlbum,
              child: Text('Save to album'),
            )
          ],
        );
      case _SaveStatus.SAVING:
        return CircularProgressIndicator();
      case _SaveStatus.SAVED:
        return Icon(Icons.check);
    }
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_photokit: ^0.1.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_photokit/flutter_photokit.dart';
  
Version Uploaded Documentation Archive
0.1.1 Sep 5, 2018 Go to the documentation of flutter_photokit 0.1.1 Download flutter_photokit 0.1.1 archive
0.1.0 Sep 5, 2018 Go to the documentation of flutter_photokit 0.1.0 Download flutter_photokit 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
55
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
78
Learn more about scoring.

We analyzed this package on Sep 18, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.3
  • Flutter: 0.8.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
path_provider 0.4.1