2024-03-14 14:41:55 +01:00

252 lines
10 KiB
Dart

import 'package:flutter/material.dart';
import 'package:manager_app/Components/check_input_container.dart';
import 'package:manager_app/Components/multi_string_input_container.dart';
import 'package:manager_app/Screens/Configurations/Section/SubSection/Slider/listView_card_image.dart';
import 'package:manager_app/Screens/Configurations/Section/SubSection/Slider/new_update_image_slider.dart';
import 'package:manager_app/app_context.dart';
import 'package:manager_app/constants.dart';
import 'package:manager_api_new/api.dart';
import 'dart:convert';
import 'package:provider/provider.dart';
class ArticleConfig extends StatefulWidget {
final String? color;
final String? label;
final String initialValue;
final ValueChanged<String> onChanged;
const ArticleConfig({
Key? key,
this.color,
this.label,
required this.initialValue,
required this.onChanged,
}) : super(key: key);
@override
_ArticleConfigState createState() => _ArticleConfigState();
}
class _ArticleConfigState extends State<ArticleConfig> {
late ArticleDTO articleDTO;
@override
void initState() {
super.initState();
articleDTO = ArticleDTO.fromJson(json.decode(widget.initialValue))!;
List<ContentDTO> test = new List<ContentDTO>.from(articleDTO.contents!);
articleDTO.contents = test;
articleDTO.contents!.sort((a, b) => a.order!.compareTo(b.order!));
}
@override
Widget build(BuildContext context) {
final appContext = Provider.of<AppContext>(context);
Size size = MediaQuery.of(context).size;
void _onReorder(int oldIndex, int newIndex) {
setState(
() {
if (newIndex > oldIndex) {
newIndex -= 1;
}
final ContentDTO item = articleDTO.contents!.removeAt(oldIndex);
articleDTO.contents!.insert(newIndex, item);
var i = 0;
articleDTO.contents!.forEach((image) {
image.order = i;
i++;
});
widget.onChanged(jsonEncode(articleDTO).toString());
},
);
}
return
SingleChildScrollView(
child: Column(
children: [
Container(
height: size.height * 0.15,
//width: size.width * 0.5,//,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Column(
children: [
MultiStringInputContainer(
label: "Contenu affiché :",
modalLabel: "Contenu",
color: kPrimaryColor,
isHTML: true,
initialValue: articleDTO != null ? articleDTO.content! : [],
isTitle: false,
onGetResult: (value) {
setState(() {
if (articleDTO.content! != value) {
articleDTO.content = value;
//save(true, articleDTO, appContext);
widget.onChanged(jsonEncode(articleDTO).toString());
}
});
},
maxLines: 1,
),
CheckInputContainer(
label: "Contenu au-dessus :",
isChecked: articleDTO.isContentTop,
onChanged: (value) {
setState(() {
//print(value);
articleDTO.isContentTop = value;
widget.onChanged(jsonEncode(articleDTO).toString());
});
},
),
],
),
Column(
children: [
MultiStringInputContainer(
label: "Audio :",
resourceTypes: [ResourceType.Audio],
modalLabel: "Audio",
color: kPrimaryColor,
initialValue: articleDTO != null ? articleDTO.audioIds! : [],
isTitle: false,
onGetResult: (value) {
setState(() {
if (articleDTO.audioIds != value) {
articleDTO.audioIds = value;
//save(true, articleDTO, appContext);
widget.onChanged(jsonEncode(articleDTO).toString());
}
});
},
maxLines: 1,
),
CheckInputContainer(
label: "Lecture audio auto :",
isChecked: articleDTO.isReadAudioAuto,
onChanged: (value) {
setState(() {
//print(value);
articleDTO.isReadAudioAuto = value;
widget.onChanged(jsonEncode(articleDTO).toString());
});
},
),
],
),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25),
border: Border.all(width: 1.5, color: kSecond)
),
child: Stack(
children: [
Padding(
padding: const EdgeInsets.only(top: 40, left: 10, right: 10, bottom: 10),
child: Container(
height: 250,
width: size.width * 0.95,
child: ReorderableListView(
onReorder: _onReorder,
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.symmetric(vertical: 20.0),
children: List.generate(
articleDTO.contents!.length,
(index) {
return ListViewCardContent(
articleDTO.contents!,
index,
Key('$index'),
appContext,
(images) {
setState(() {
List<ContentDTO> test = new List<ContentDTO>.from(images);
articleDTO.contents = test;
List<ContentDTO> testToSend = new List<ContentDTO>.from(images);
testToSend = testToSend.where((element) => element.resourceUrl != null).toList();
var articleToSend = new ArticleDTO();
articleToSend.contents = testToSend;
articleToSend.audioIds = articleDTO.audioIds;
articleToSend.isContentTop = articleDTO.isContentTop;
articleToSend.content = articleDTO.content;
articleToSend.isReadAudioAuto = articleDTO.isReadAudioAuto;
widget.onChanged(jsonEncode(articleToSend).toString());
});
},
true, // don't show titles
false // show description
);
},
),
),
),
),
Positioned(
top: 10,
left: 10,
child: Text(
"Images",
style: TextStyle(fontSize: 15, fontWeight: FontWeight.w500),
),
),
Positioned(
bottom: 15,
right: 15,
child: InkWell(
onTap: () async {
var result = await showNewOrUpdateContentSlider(null, appContext, context, true, false);
if(result != null) {
setState(() {
result.order = articleDTO.contents!.length;
articleDTO.contents!.add(result);
widget.onChanged(jsonEncode(articleDTO).toString());
});
}
},
child: Container(
height: MediaQuery.of(context).size.width * 0.04,
width: MediaQuery.of(context).size.width * 0.04,
child: Icon(
Icons.add,
color: kTextLightColor,
size: 30.0,
),
decoration: BoxDecoration(
color: kSuccess,
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(20.0),
boxShadow: [
BoxShadow(
color: kSecond,
spreadRadius: 0.5,
blurRadius: 5,
offset: Offset(0, 1.5), // changes position of shadow
),
],
),
),
),
),
]),
),
),
],
),
);
}
}