148 lines
4.7 KiB
Dart
148 lines
4.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:manager_app/Components/string_input_container.dart';
|
|
import 'package:manager_app/Screens/Resources/select_resource_modal.dart';
|
|
import 'package:manager_app/constants.dart';
|
|
import 'package:manager_api_new/api.dart';
|
|
|
|
enum _VideoSourceMode { youtube, vimeo, resource }
|
|
|
|
_VideoSourceMode _detectMode(String? url) {
|
|
if (url == null || url.isEmpty) return _VideoSourceMode.youtube;
|
|
if (url.contains('youtube.com') || url.contains('youtu.be')) return _VideoSourceMode.youtube;
|
|
if (url.contains('vimeo.com')) return _VideoSourceMode.vimeo;
|
|
return _VideoSourceMode.resource;
|
|
}
|
|
|
|
class VideoConfig extends StatefulWidget {
|
|
final String? color;
|
|
final String? label;
|
|
final VideoDTO initialValue;
|
|
final ValueChanged<VideoDTO> onChanged;
|
|
const VideoConfig({
|
|
Key? key,
|
|
this.color,
|
|
this.label,
|
|
required this.initialValue,
|
|
required this.onChanged,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
_VideoConfigState createState() => _VideoConfigState();
|
|
}
|
|
|
|
class _VideoConfigState extends State<VideoConfig> {
|
|
late VideoDTO resourceSource;
|
|
late _VideoSourceMode _mode;
|
|
String? _selectedResourceLabel;
|
|
|
|
@override
|
|
void initState() {
|
|
resourceSource = widget.initialValue;
|
|
_mode = _detectMode(resourceSource.source_);
|
|
super.initState();
|
|
}
|
|
|
|
void _onModeChanged(_VideoSourceMode mode) {
|
|
setState(() {
|
|
_mode = mode;
|
|
resourceSource.source_ = null;
|
|
_selectedResourceLabel = null;
|
|
});
|
|
widget.onChanged(resourceSource);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
SegmentedButton<_VideoSourceMode>(
|
|
segments: const [
|
|
ButtonSegment(value: _VideoSourceMode.youtube, label: Text('YouTube'), icon: Icon(Icons.smart_display)),
|
|
ButtonSegment(value: _VideoSourceMode.vimeo, label: Text('Vimeo'), icon: Icon(Icons.play_circle)),
|
|
ButtonSegment(value: _VideoSourceMode.resource, label: Text('Ressource'), icon: Icon(Icons.video_file)),
|
|
],
|
|
selected: {_mode},
|
|
onSelectionChanged: (selection) => _onModeChanged(selection.first),
|
|
),
|
|
const SizedBox(height: 12),
|
|
if (_mode == _VideoSourceMode.youtube)
|
|
StringInputContainer(
|
|
label: 'URL YouTube :',
|
|
initialValue: _mode == _VideoSourceMode.youtube ? resourceSource.source_ : null,
|
|
onChanged: (url) {
|
|
resourceSource.source_ = url;
|
|
widget.onChanged(resourceSource);
|
|
},
|
|
isUrl: true,
|
|
maxLength: 200,
|
|
),
|
|
if (_mode == _VideoSourceMode.vimeo)
|
|
StringInputContainer(
|
|
label: 'URL Vimeo :',
|
|
initialValue: _mode == _VideoSourceMode.vimeo ? resourceSource.source_ : null,
|
|
onChanged: (url) {
|
|
resourceSource.source_ = url;
|
|
widget.onChanged(resourceSource);
|
|
},
|
|
isUrl: true,
|
|
maxLength: 200,
|
|
),
|
|
if (_mode == _VideoSourceMode.resource)
|
|
_ResourceVideoPicker(
|
|
selectedLabel: _selectedResourceLabel,
|
|
selectedUrl: _mode == _VideoSourceMode.resource ? resourceSource.source_ : null,
|
|
onChanged: (resource) {
|
|
setState(() {
|
|
resourceSource.source_ = resource.url;
|
|
_selectedResourceLabel = resource.label;
|
|
});
|
|
widget.onChanged(resourceSource);
|
|
},
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ResourceVideoPicker extends StatelessWidget {
|
|
final String? selectedLabel;
|
|
final String? selectedUrl;
|
|
final ValueChanged<ResourceDTO> onChanged;
|
|
|
|
const _ResourceVideoPicker({
|
|
required this.selectedLabel,
|
|
required this.selectedUrl,
|
|
required this.onChanged,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Row(
|
|
children: [
|
|
const Text('Vidéo :', style: TextStyle(fontSize: 18)),
|
|
const SizedBox(width: 10),
|
|
ElevatedButton.icon(
|
|
icon: const Icon(Icons.video_file),
|
|
label: Text(selectedLabel ?? (selectedUrl != null ? 'Vidéo sélectionnée' : 'Choisir')),
|
|
style: ElevatedButton.styleFrom(backgroundColor: kPrimaryColor, foregroundColor: Colors.white),
|
|
onPressed: () async {
|
|
final result = await showSelectResourceModal(
|
|
'Sélectionner une vidéo',
|
|
1,
|
|
[ResourceType.Video, ResourceType.VideoUrl],
|
|
context,
|
|
true,
|
|
true,
|
|
true,
|
|
);
|
|
if (result != null && result is ResourceDTO) {
|
|
onChanged(result);
|
|
}
|
|
},
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|