17일차 과제 - rlatkddbs99/Flutter GitHub Wiki
강의 전 실패 현황 -> 갤러리는 떴으나 저장이 안돼
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
File? image;
Future getImageFromGallery() async {
try {
final image = await ImagePicker().pickImage(source: ImageSource.gallery);
if (image == null) return;
final imageTemp = File(image.path);
setState(() {
this.image = imageTemp;
});
} on PlatformException catch (e) {
print('failed to pick image');
}
}
Future refresh() async {
try {
image = null;
setState(() {
this.image = null;
});
} catch (e) {
print('후..');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
themeMode: ThemeMode.dark,
darkTheme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(
title: Text(
"포토네컷",
),
centerTitle: true,
),
body: Padding(
padding: const EdgeInsets.only(left: 45),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
child: Container(
color: Colors.blue,
width: 300,
height: 150,
),
onTap: () {
getImageFromGallery();
},
onDoubleTap: () {
refresh();
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
child: Container(
color: Colors.blue,
width: 300,
height: 150,
),
onTap: () {
getImageFromGallery();
},
onDoubleTap: () {
refresh();
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
child: Container(
color: Colors.blue,
width: 300,
height: 150,
),
onTap: () {
getImageFromGallery();
},
onDoubleTap: () {
refresh();
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
child: Container(
color: Colors.blue,
width: 300,
height: 150,
),
onTap: () {
getImageFromGallery();
},
onDoubleTap: () {
refresh();
},
),
),
],
),
)),
);
}
}
이건 뭐 걍 잘못한거
InkWell(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(selectedImage!.path),
fit: BoxFit.cover)),
),
onTap: () async {
var image = await imagePicker.pickImage(
source: ImageSource.gallery);
if (image != null) {
print("이미지 선택");
selectedImage = image;
setState(() {});
} else {
print("선택하세요");
}
},
),
강의 후 listview.builder로 한거
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var _imagePicker = ImagePicker();
List<XFile?> images = [null, null, null, null];
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.transparent,
foregroundColor: Colors.white,
title: Text("포토네컷"),
centerTitle: true,
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 48),
child: ListView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: images.length,
itemBuilder: (context, index) => InkWell(
onDoubleTap: () => setState(() => images[index] = null),
onTap: () async {
var xfile = await _imagePicker.pickImage(
source: ImageSource.gallery);
if (xfile != null) {
images[index] = xfile;
setState(() {});
}
},
child: Container(
height: 200,
margin: EdgeInsets.symmetric(vertical: 4),
decoration: BoxDecoration(
image: images[index] != null
? DecorationImage(
image: AssetImage(images[index]!.path),
fit: BoxFit.cover)
: null,
color: Colors.white12,
),
),
),
))),
),
);
}
}
강의 후 container에 다 때려 넣기
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var _imagePicker = ImagePicker();
var image1 = null;
var image2 = null;
var image3 = null;
var image4 = null;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.transparent,
foregroundColor: Colors.white,
title: Text("포토네컷"),
centerTitle: true,
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 48),
child: Column(
children: [
Expanded(
child: InkWell(
onDoubleTap: () => setState(() => image1 = null),
onTap: () async {
var xfile = await _imagePicker.pickImage(
source: ImageSource.gallery);
if (xfile != null) {
image1 = xfile;
setState(() {});
}
},
child: Container(
margin: EdgeInsets.symmetric(vertical: 4),
decoration: BoxDecoration(
image: image1 != null
? DecorationImage(
image: AssetImage(image1.path), fit: BoxFit.cover)
: null,
color: Colors.white12,
),
),
)),
Expanded(
child: InkWell(
onDoubleTap: () => setState(() => image2 = null),
onTap: () async {
var xfile = await _imagePicker.pickImage(
source: ImageSource.gallery);
if (xfile != null) {
image2 = xfile;
setState(() {});
}
},
child: Container(
margin: EdgeInsets.symmetric(vertical: 4),
decoration: BoxDecoration(
image: image2 != null
? DecorationImage(
image: AssetImage(image2.path), fit: BoxFit.cover)
: null,
color: Colors.white12,
),
),
)),
Expanded(
child: InkWell(
onDoubleTap: () => setState(() => image3 = null),
onTap: () async {
var xfile = await _imagePicker.pickImage(
source: ImageSource.gallery);
if (xfile != null) {
image3 = xfile;
setState(() {});
}
},
child: Container(
margin: EdgeInsets.symmetric(vertical: 4),
decoration: BoxDecoration(
image: image3 != null
? DecorationImage(
image: AssetImage(image3.path), fit: BoxFit.cover)
: null,
color: Colors.white12,
),
),
)),
Expanded(
child: InkWell(
onDoubleTap: () => setState(() => image4 = null),
onTap: () async {
var xfile = await _imagePicker.pickImage(
source: ImageSource.gallery);
if (xfile != null) {
image4 = xfile;
setState(() {});
}
},
child: Container(
margin: EdgeInsets.symmetric(vertical: 4),
decoration: BoxDecoration(
image: image4 != null
? DecorationImage(
image: AssetImage(image4.path), fit: BoxFit.cover)
: null,
color: Colors.white12,
),
),
)),
],
),
),
),
),
);
}
}