일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- 관상
- 호랑이상
- 실시간 인공지능 관상 테스트
- 코로나
- 안드로이드
- #자바
- 테스트
- 프로그램
- AR Lipstick Viewer
- 인공지능
- 재물운
- 동물상
- 나른한 오후
- 늑대상
- Lipstick
- 관상이야기
- 관상 이야기
- 연애운
- 관상 테스트
- 인공지능 동물상 관상 테스트
- 쥐상관상
- 거북이상
- 자바
- 인공지능 거북이 관상
- 코틀린
- 인공지능 관상
- Teachable Machine
- 나른한오후
- ==>
- 인공지능 호랑이상
- Today
- Total
주식회사 이웃사촌
이미지파일 다운로드및 FutureBuilder 를 이용한 화면에 나타내기 소스 본문
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:path_provider/path_provider.dart';
class LargeFileMain extends StatefulWidget {
@override
State<StatefulWidget> createState() => _LargeFileMain();
}
class _LargeFileMain extends State<LargeFileMain> {
bool downloading = false;
var progressString = "";
String? file = "";
TextEditingController? _editingController;
@override
void initState() {
super.initState();
_editingController = new TextEditingController(text: 'https://www.motherjones.com/wp-content/uploads/2019/12/Getty121719.jpg?w=1200&h=630&crop=1');
}
Future<void> downloadFile() async {
Dio dio = Dio();
try {
var dir = await getApplicationDocumentsDirectory();
await dio.download(_editingController!.value.text, '${dir.path}/myimage.jpg',
onReceiveProgress: (rec, total) {
print('Rec: $rec , Total: $total');
file = '${dir.path}/myimage.jpg';
setState(() {
downloading = true;
progressString = ((rec / total) * 100).toStringAsFixed(0) + '%';
});
});
} catch (e) {
print(e);
}
setState(() {
downloading = false;
progressString = 'Completed';
});
print('Download completed');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: TextField(
controller: _editingController,
style: TextStyle(color: Colors.white),
keyboardType: TextInputType.text,
decoration: InputDecoration(hintText: 'url 입력하세요'),
),
),
body: Center(
child: downloading
? Container(
height: 120.0,
width: 200.0,
child: Card(
color: Colors.black,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircularProgressIndicator(),
SizedBox(
height: 20.0,
),
Text(
'Downloading File: $progressString',
style: TextStyle(
color: Colors.white,
),
)
],
),
),
)
: FutureBuilder(
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
print('none');
return Text('데이터 없음');
case ConnectionState.waiting:
print('waiting');
return CircularProgressIndicator();
case ConnectionState.active:
print('active');
return CircularProgressIndicator();
case ConnectionState.done:
print('done');
if (snapshot.hasData) {
return snapshot.data as Widget;
}
}
return Text('데이터 없음');
},
future: downloadWidget(file!),
)),
floatingActionButton: FloatingActionButton(
onPressed: () {
downloadFile();
},
child: Icon(Icons.file_download),
),
);
}
Future<Widget> downloadWidget(String filePath) async {
File file = File(filePath);
bool exist = await file.exists();
new FileImage(file).evict();
if (exist) {
return Center(
child: Column(
children: <Widget>[Image.file(file)],
),
);
} else {
return Text('No Data');
}
}
}
'Flutter' 카테고리의 다른 글
List.generate List<Map<String, dynamic>>==> <List<Todo>> (0) | 2021.08.11 |
---|---|
내부저장소에 데이타 읽고쓰기 (0) | 2021.08.10 |
path_provider + File 을 이용한 화일 읽고/쓰기 (0) | 2021.08.10 |
ListView.builder 스크롤로 책정보 가져오기 (0) | 2021.08.10 |
FutureBuilder 사용예제 (0) | 2021.08.09 |