manager-app/lib/Components/upload_image_container.dart
2021-07-01 17:05:47 +02:00

173 lines
4.6 KiB
Dart

import 'dart:io';
import 'dart:convert';
import 'package:dart_vlc/dart_vlc.dart';
import 'package:manager_app/Components/loading.dart';
import 'package:manager_app/Components/vlc_viewer.dart';
import 'package:manager_app/constants.dart';
import 'package:path/path.dart';
import 'package:async/async.dart';
import 'package:http/http.dart' as http;
import 'package:filepicker_windows/filepicker_windows.dart';
import 'package:flutter/material.dart';
class UploadImageContainer extends StatefulWidget {
final ValueChanged<File> onChanged;
const UploadImageContainer({
Key key,
this.onChanged,
}) : super(key: key);
@override
_UploadImageContainerState createState() => _UploadImageContainerState();
}
class _UploadImageContainerState extends State<UploadImageContainer> with SingleTickerProviderStateMixin {
var filePath;
File fileToShow;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Container(
width: size.width *0.5,
height: size.height *0.5,
child: displayElement(),
);
}
String getFileName(filePath) {
return (filePath.toString().split('\\').last);
}
void filePicker() {
final file = OpenFilePicker()
..filterSpecification = {
'Images (*.jpg; *.png)': '*.jpg;*.png',
//'Video (*.mp4)': '*.mp4',
//'All Files': '*.*'
}
..defaultFilterIndex = 0
..title = 'Sélectionner un fichier';
final result = file.getFile();
if (result != null) {
setState(() {
filePath = result.path;
fileToShow = result;
widget.onChanged(result);
});
}
}
showFile() {
if (getFileName(filePath).contains(".mp4")) {
return FutureBuilder(
future: loadFile(fileToShow),
builder: (context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Container(
height: 300,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text("La prévisualisation de la vidéo n'est pas disponible"),
),
Icon(
Icons.ondemand_video_sharp,
size: 35,
),
],
),
//DartVLC(file: snapshot.data)
);
} else if (snapshot.connectionState == ConnectionState.none) {
return Text("No data");
} else {
return Center(
child: Container(
height: 200,
child: Loading()
)
);
}
}
);
/*await Media.file(widget.file)*/
} else {
return Image.file(
fileToShow,
height: 200,
fit:BoxFit.scaleDown
);
}
}
loadFile(File fileToShow) async {
return await Media.file(fileToShow);
}
displayElement() {
if (fileToShow == null) return Center(
child: InkWell(
onTap: () async {
filePicker();
},
child: Container(
decoration: BoxDecoration(
color: kPrimaryColor,
borderRadius: BorderRadius.circular(15)
),
child: Padding(
padding: const EdgeInsets.only(left: 25.0, right: 25.0, top: 15.0, bottom: 15.0),
child: Text(
"Ajouter un fichier",
style: new TextStyle(color: kWhite),
),
)
),
),
);
if (fileToShow != null)
return Container(
margin: EdgeInsets.all(8.0),
child: Card(
color: kBackgroundColor,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
child: InkWell(
onTap: () async {
filePicker();
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8.0),
topRight: Radius.circular(8.0),
),
child: showFile()
),
ListTile(
title: Text(getFileName(filePath)),
subtitle: Text(filePath),
),
],
),
),
),
);
}
}