import 'dart:io'; import 'dart:convert'; import 'package:dart_vlc/dart_vlc.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 onChanged; const UploadImageContainer({ Key key, this.onChanged, }) : super(key: key); @override _UploadImageContainerState createState() => _UploadImageContainerState(); } class _UploadImageContainerState extends State 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 snapshot) { if (snapshot.connectionState == ConnectionState.done) { return Container( height: 300, child: DartVLC(file: snapshot.data) ); } else if (snapshot.connectionState == ConnectionState.none) { return Text("No data"); } else { return Center(child: Container(height: 200, child: Text('LOADING TODO FRAISE'))); } } ); /*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: [ ClipRRect( borderRadius: BorderRadius.only( topLeft: Radius.circular(8.0), topRight: Radius.circular(8.0), ), child: showFile() ), ListTile( title: Text(getFileName(filePath)), subtitle: Text(filePath), ), ], ), ), ), ); } } Upload(File imageFile) async { var stream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead())); var length = await imageFile.length(); var uri = Uri.parse("upload URL TEST"); var request = new http.MultipartRequest("POST", uri); var multipartFile = new http.MultipartFile('file', stream, length, filename: basename(imageFile.path)); //contentType: new MediaType('image', 'png')); request.files.add(multipartFile); var response = await request.send(); print(response.statusCode); response.stream.transform(utf8.decoder).listen((value) { print(value); }); }