Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:image_picker/image_picker.dart';
- void main() => runApp(MyApp());
- class MyApp extends StatelessWidget {
- // This widget is the root of your application.
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Flutter Demo',
- theme: ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: MyHomePage(title: 'Flutter Demo Home Page'),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- MyHomePage({Key key, this.title}) : super(key: key);
- final String title;
- @override
- _MyHomePageState createState() => _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> {
- File _image;
- getImageFromGallary() {
- getImage(ImageSource.gallery);
- }
- getImageFromCamera() {
- getImage(ImageSource.camera);
- }
- Future getImage(ImageSource source) async {
- var image = await ImagePicker.pickImage(source: source);
- setState((){
- _image = image;
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text(widget.title),
- ),
- body: Center(
- child: _image == null ? Text('画像が選択されていません'): Image.file(_image)
- ),
- floatingActionButton: Column(
- crossAxisAlignment: CrossAxisAlignment.end,
- mainAxisAlignment: MainAxisAlignment.end,
- children: <Widget> [
- FloatingActionButton(
- onPressed: getImageFromCamera,
- tooltip: 'カメラ撮影',
- child:
- Icon(Icons.add_a_photo),
- ),
- FloatingActionButton(
- onPressed: getImageFromGallary,
- tooltip: '写真',
- child:
- Icon(Icons.photo_album),
- ),
- ],
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement