Flutterでカメラアプリ

##Flutterとは？
Androidでも、IOSでも動くアプリが作れるプラットフォーム,Google製
Dartという、Javaに近い言語で書きます。

##以下ソース

```java
import 'dart:async';
import 'dart:io';

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';



class Capture extends StatefulWidget {
  @override
  _CaptureState createState() => new _CaptureState();
}

class _CaptureState extends State<Capture> {
  CameraController controller;

  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  @override
  void initState() {
    super.initState();
    availableCameras().then((cameras) {
      CameraDescription rearCamera = cameras.firstWhere(
              (desc) => desc.lensDirection == CameraLensDirection.back, orElse: () => null);
      if (rearCamera == null) {
        return;
      }

      controller = new CameraController(rearCamera, ResolutionPreset.high);
      controller.initialize().then((_) {
        if (!mounted) {
          return;
        }
        setState(() {});
      });
    });
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {

    Widget preview;
    if (!_isReady()) {
      preview = new Container();
    } else {
      preview = new Container(
          child: new Center(
              child: new AspectRatio(
                  aspectRatio: controller.value.aspectRatio,
                  child: new CameraPreview(controller))
          )
      );
    }
    return new Scaffold(

        backgroundColor: Colors.black,

        key: _scaffoldKey,
        appBar: new AppBar(

          title: const Text("画像解析APP",
              style: TextStyle(

                color: Colors.white,
              )


          ),
          backgroundColor: Colors.black,
        ),
        body:
        new Column(
            children: <Widget>[
              new Expanded(
                child: preview,
              ),
              new SizedBox(
                width: 335.0,
                height: 100.0,

                child: new RaisedButton.icon(
                  icon: Icon(
                    Icons.android,
                    color: Colors.white,
                    size: 70,
                  ),
                  label: Text("画像解析",

                    style: TextStyle(

                      fontSize: 20,
                      color: Colors.white,
                    )
                  ),
                  onPressed: () {_onPressedCaptureIcon();},
                  color: Colors.pinkAccent,
                  textColor: Colors.white,

                ),

              )



            ]

        ),

    );
  }

  bool _isReady() {
    return controller != null && controller.value.isInitialized;
  }

  _onPressedCaptureIcon() {
    _takePicture().then((String filePath) {
      _showSnackBar('Picture saved to $filePath');
      Navigator.of(context).pushNamed("/result");

    }).catchError((e) {
      _showSnackBar(e);
    });

  }

  Future<String> _takePicture() async {
    if (!_isReady()) {
      throw("Camera controller is not initialized.");
    }

    final Directory extDir = await getApplicationDocumentsDirectory();
    final String dirPath = '${extDir.path}/Pictures/flutter_test';
    await new Directory(dirPath).create(recursive: true);
    final String filePath = '$dirPath/${_timestamp()}.jpg';

    if (controller.value.isTakingPicture) {
      // A capture is already pending, do nothing.
      throw("Camera is already pending.");
    }

    await controller.takePicture(filePath);

    return filePath;
  }

  String _timestamp() => new DateTime.now().millisecondsSinceEpoch.toString();

  void _showSnackBar(String text) {
    _scaffoldKey.currentState.showSnackBar(new SnackBar(
      content: new Text(text),
    ));
  }
}