manager-app/lib/Screens/Resources/resources_screen.dart
2021-05-22 00:04:59 +02:00

157 lines
5.3 KiB
Dart

import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:manager_app/Components/fetch_resource_icon.dart';
import 'package:manager_app/Screens/Resources/new_resource_popup.dart';
import 'package:manager_app/Screens/Resources/show_resource_popup.dart';
import 'package:manager_app/app_context.dart';
import 'package:manager_app/constants.dart';
import 'package:managerapi/api.dart';
import 'package:provider/provider.dart';
class ResourcesScreen extends StatefulWidget {
final Function onGetResult; //return ResourceDTO
final bool isImage;
const ResourcesScreen({
Key key,
this.isImage,
this.onGetResult,
}) : super(key: key);
@override
_ResourcesScreenState createState() => _ResourcesScreenState();
}
class _ResourcesScreenState extends State<ResourcesScreen> {
@override
Widget build(BuildContext context) {
final appContext = Provider.of<AppContext>(context);
Size size = MediaQuery.of(context).size;
return Container(
child: FutureBuilder(
future: getResources(widget.onGetResult, widget.isImage, appContext),
builder: (context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
var tempOutput = new List<ResourceDTO>.from(snapshot.data);
tempOutput.add(ResourceDTO(id: null));
return bodyGrid(tempOutput, size, appContext);
} else if (snapshot.connectionState == ConnectionState.none) {
return Text("No data");
} else {
return Center(child: Container(height: size.height * 0.2, child: Text('LOADING TODO FRAISE')));
}
}
),
);
}
Widget bodyGrid(data, Size size, AppContext appContext) {
return GridView.builder(
shrinkWrap: true,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 6),
itemCount: data.length,
itemBuilder: (BuildContext context, int index) {
return
InkWell(
onTap: () {
if (widget.onGetResult == null) {
// Main screen
if (data[index].id == null) {
showNewResource(appContext, context);
} else {
showResource(data[index], appContext, context, size);
}
} else {
if (data[index].id == null) {
showNewResource(appContext, context);
} else {
// Result for select modal
widget.onGetResult(data[index]);
}
}
},
child: Container(
decoration: boxDecoration(data[index], appContext),
padding: const EdgeInsets.all(15),
margin: EdgeInsets.symmetric(vertical: 15, horizontal: 15),
child: Align(
alignment: Alignment.center,
child: getElement(data[index], size, appContext)
),
),
);
}
);
}
getElement(ResourceDTO resource, Size size, AppContext appContext) {
if (resource.id != null) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(
height: size.width *0.01,
),
Align(
alignment: Alignment.center,
child: AutoSizeText(
resource.label == null ? "" : resource.label,
style: new TextStyle(fontSize: 20),
maxLines: 1,
),
),
Align(
alignment: Alignment.bottomRight,
child: Icon(
getResourceIcon(resource.type),
color: kPrimaryColor,
size: 25,
),
),
],
);
} else {
return Icon(
Icons.add,
color: kTextLightColor,
size: 80.0,
);
}
}
}
boxDecoration(dynamic resourceDetailDTO, appContext) {
return BoxDecoration(
color: resourceDetailDTO.id == null ? Colors.lightGreen : kBackgroundColor,
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(30.0),
image: resourceDetailDTO.id != null && resourceDetailDTO.type != ResourceType.videoUrl ? new DecorationImage(
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.3), BlendMode.dstATop),
image: new NetworkImage(
resourceDetailDTO.type == ResourceType.image ? appContext.getContext().clientAPI.resourceApi.apiClient.basePath+"/api/Resource/"+ resourceDetailDTO.id : resourceDetailDTO.data,
),
) : null,
boxShadow: [
BoxShadow(
color: kSecond,
spreadRadius: 0.5,
blurRadius: 5,
offset: Offset(0, 1.5), // changes position of shadow
),
],
);
}
Future<List<ResourceDTO>> getResources(Function onGetResult, bool isImage, dynamic appContext) async {
List<ResourceDTO> resources = await appContext.getContext().clientAPI.resourceApi.resourceGet();
if (onGetResult != null && isImage) {
resources = resources.where((element) => element.type == ResourceType.image || element.type == ResourceType.imageUrl).toList();
}
return resources;
}