환경 설정
image_picker 설치
flutter pub add image_picker
https://pub.dev/packages/image_picker/install
image_picker | Flutter Package
Flutter plugin for selecting images from the Android and iOS image library, and taking new pictures with the camera.
pub.dev
실행코드
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:image_picker/image_picker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.purple,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
File? _image;
void Photo(ImageSource source) async {
XFile? file = await ImagePicker().pickImage(source: source);
setState(() => _image = File(file!.path));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('gallery test'),
),
body: ListView(
children: [
const SizedBox(
height: 50,
),
_image != null
? Container(
child: Image.file(_image!),
)
: Container(
child: const Text('이미지'),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
height: 40,
child: ElevatedButton(
child: const Text(
'사진 가져오기',
style: TextStyle(fontSize: 20),
),
onPressed: () => Photo(ImageSource.gallery),
),
),
Container(
height: 40,
child: ElevatedButton(
child: const Text(
'사진 찍기',
style: TextStyle(fontSize: 20),
),
onPressed: () => Photo(ImageSource.camera),
),
)
],
),
],
),
);
}
}
참고자료
[j Flutter] 갤러리/앨범에서 사진가져오기 & 카메라 실행
갤러리에서 사진 가져오기 / 카메라 실행을 해보자. 둘 다 실행시킬 수 있는 공통 패키지를 설치해준다. 1. image_picker 패키지 pubspec.yaml 에 설치 dependencies: image_picker: ^0.6.7+17 pub.dev/packages/i..
yj95.tistory.com
'개발 > 기타' 카테고리의 다른 글
DB:: DB 설계 고민 (1) | 2023.11.12 |
---|---|
기타:: 230116 일기2 - 자동 채점기 리팩토링(Python) (0) | 2023.01.16 |
Windows :: 윈도우에 등록된 와이파이 비번 알아내기 (0) | 2022.05.05 |
Django:: Post model (0) | 2022.02.17 |
Django:: 장고에서 앱 개발하기 (0) | 2022.02.14 |