Files
flutter/lib/widgets/exercises/forms.dart
Roland Geider 65dbf426cf Use ValueKey instead of accessing uuid directly
This would crash on objects that don't have any uuid attribute, such as the Language.
ValueKey works on all of them, as long as they are equatable.

Closes #1236
2026-06-23 15:32:42 +02:00

90 lines
2.7 KiB
Dart

/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 wger Team
*
* wger Workout Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:flutter/material.dart';
/// Input widget for exercise category objects
///
/// Can be used with a Setting or a Log object
class ExerciseCategoryInputWidget<T> extends StatefulWidget {
late final String _title;
late final Function _callback;
late final List<T> _entries;
late final Function _getDisplayName;
late final Function? _validator;
ExerciseCategoryInputWidget({
super.key,
required String title,
required List<T> entries,
required Function callback,
required Function displayName,
Function? validator,
}) {
_entries = entries;
_title = title;
_callback = callback;
_getDisplayName = displayName;
_validator = validator;
}
@override
_ExerciseCategoryInputWidgetState createState() => _ExerciseCategoryInputWidgetState<T>();
}
class _ExerciseCategoryInputWidgetState<T> extends State<ExerciseCategoryInputWidget> {
@override
Widget build(BuildContext context) {
T? selectedEntry;
return Padding(
padding: const EdgeInsets.all(8.0),
child: DropdownButtonFormField(
key: widget.key,
initialValue: selectedEntry,
decoration: InputDecoration(
labelText: widget._title,
contentPadding: const EdgeInsets.all(8.0),
border: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10)),
),
),
onChanged: (T? newValue) {
setState(() {
selectedEntry = newValue as T;
widget._callback(newValue);
});
},
validator: (T? value) {
if (widget._validator != null) {
return widget._validator!(value);
}
return null;
},
items: widget._entries.map<DropdownMenuItem<T>>((value) {
return DropdownMenuItem<T>(
key: ValueKey(value),
value: value,
child: Text(widget._getDisplayName(value)),
);
}).toList(),
),
);
}
}