manager-app/lib/Screens/Resources/new_ressource_popup.dart

124 lines
4.3 KiB
Dart

import 'package:file_picker/file_picker.dart';
import 'package:filepicker_windows/filepicker_windows.dart';
import 'package:flutter/material.dart';
import 'package:manager_app/Components/message_notification.dart';
import 'package:manager_app/Components/rounded_button.dart';
import 'package:manager_app/Components/string_input_container.dart';
import 'package:manager_app/Models/managerContext.dart';
import 'package:manager_app/app_context.dart';
import 'package:manager_app/constants.dart';
import 'package:managerapi/api.dart';
void showNewRessource(AppContext appContext, BuildContext context) {
RessourceDetailDTO ressourceDetailDTO = new RessourceDetailDTO();
dynamic url = null;
showDialog(
builder: (BuildContext context) => AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0))
),
content: SingleChildScrollView(
child: Column(
children: [
Text("Nouvelle ressource", style: new TextStyle(fontSize: 25, fontWeight: FontWeight.w400)),
Column(
children: [
StringContainer(
label: "Nom :",
initialValue: ressourceDetailDTO.label,
onChanged: (value) {
ressourceDetailDTO.label = value;
},
),
if (url != null) Image.network(url)
],
),
InkWell(
onTap: () async {
print("onTap upload");
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) {
print(result.path);
print(result.uri);
// Read the file.
dynamic contents = await result.readAsBytes();
print(contents);
url = result.path;
}
},
child: Text("UPLOAD !"),
),
],
),
),
actions: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Align(
alignment: AlignmentDirectional.bottomEnd,
child: Container(
width: 175,
height: 70,
child: RoundedButton(
text: "Annuler",
icon: Icons.undo,
color: kSecond,
press: () {
Navigator.of(context).pop();
},
fontSize: 20,
),
),
),
Align(
alignment: AlignmentDirectional.bottomEnd,
child: Container(
width: 150,
height: 70,
child: RoundedButton(
text: "Créer",
icon: Icons.check,
color: kPrimaryColor,
textColor: kWhite,
press: () {
create(ressourceDetailDTO, appContext, context);
},
fontSize: 20,
),
),
),
],
),
],
), context: context
);
}
void create(RessourceDetailDTO ressourceDetailDTO, AppContext appContext, context) async {
if (ressourceDetailDTO.label != null) {
Navigator.of(context).pop();
RessourceDetailDTO newRessource = await appContext.getContext().clientAPI.ressourceApi.ressourceCreate(ressourceDetailDTO);
print("newRessource");
print(newRessource);
// To refresh only (UGLY COOOOODE)
ManagerAppContext managerAppContext = appContext.getContext();
appContext.setContext(managerAppContext);
showNotification(Colors.green, kWhite, 'La ressource a été créée avec succès', context);
}
}