import 'dart:io'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; // リスト 6.3 void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: ImagePage() ); } } // リスト 6.4 class ImagePage extends StatefulWidget { @override _ImagePageState createState() => _ImagePageState(); } class _ImagePageState extends State<ImagePage> { File? _imageFile; final _picker = ImagePicker(); Future getImage(ImageSource imageSource) async { final pickedFile = await _picker.pickImage(source: imageSource); if (pickedFile != null) { setState(() { _imageFile = File(pickedFile.path); }); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('ImagePage') ), body: Stack(children: [ if (_imageFile != null) Container( decoration: BoxDecoration( image: DecorationImage( image: Image.file(_imageFile!).image, fit: BoxFit.cover ) ) ), Align( alignment: Alignment.bottomCenter, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ IconButton( onPressed: () => getImage(ImageSource.gallery), icon: Icon(Icons.photo_library) ), IconButton( onPressed: () => getImage(ImageSource.camera), icon: Icon(Icons.camera_alt) ) ] ) ) ]) ); } }