77 lines
2.3 KiB
Dart
77 lines
2.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:manager_app/Components/collection_editor.dart';
|
|
import 'package:manager_app/Models/managerContext.dart';
|
|
import 'package:manager_app/Screens/Configurations/Section/SubSection/Slider/content_fields.dart';
|
|
import 'package:manager_app/app_context.dart';
|
|
import 'package:manager_app/l10n/app_localizations.dart';
|
|
import 'package:manager_api_new/api.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class SliderConfig extends StatefulWidget {
|
|
final String? color;
|
|
final String? label;
|
|
final SliderDTO initialValue;
|
|
final ValueChanged<SliderDTO> onChanged;
|
|
const SliderConfig({
|
|
Key? key,
|
|
this.color,
|
|
this.label,
|
|
required this.initialValue,
|
|
required this.onChanged,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
_SliderConfigState createState() => _SliderConfigState();
|
|
}
|
|
|
|
class _SliderConfigState extends State<SliderConfig> {
|
|
late SliderDTO sliderDTO;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
sliderDTO = widget.initialValue;
|
|
sliderDTO.contents = List<ContentDTO>.from(sliderDTO.contents!);
|
|
sliderDTO.contents!.sort((a, b) => a.order!.compareTo(b.order!));
|
|
}
|
|
|
|
/// Le backend ne veut que les contenus qui portent réellement une ressource —
|
|
/// c'est ce que faisait déjà le filtre de l'ancien `onChanged`.
|
|
void _persist() {
|
|
var i = 0;
|
|
for (final content in sliderDTO.contents!) {
|
|
content.order = i;
|
|
i++;
|
|
}
|
|
|
|
final toSend = SliderDTO();
|
|
toSend.contents = sliderDTO.contents!
|
|
.where((content) => content.resource != null && content.resource?.url != null)
|
|
.toList();
|
|
widget.onChanged(toSend);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l = AppLocalizations.of(context)!;
|
|
final languages = (Provider.of<AppContext>(context).getContext()
|
|
as ManagerAppContext)
|
|
.selectedConfiguration!
|
|
.languages!;
|
|
|
|
return CollectionEditor<ContentDTO>(
|
|
items: sliderDTO.contents!,
|
|
addLabel: l.addContentLabel,
|
|
itemLabel: (content, index) => contentLabel(l, content, index),
|
|
createItem: () => emptyContent(languages),
|
|
onChanged: (_) => _persist(),
|
|
detailBuilder: (content, index) => ContentFields(
|
|
key: ValueKey(identityHashCode(content)),
|
|
content: content,
|
|
onChanged: _persist,
|
|
),
|
|
);
|
|
}
|
|
}
|