2025-09-10 15:17:28 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import '../../formula_models.dart';
|
|
|
|
|
import '../../corpus.dart';
|
|
|
|
|
|
|
|
|
|
class UnitDropdown extends StatelessWidget {
|
2025-09-20 14:46:21 +00:00
|
|
|
final Corpus corpus;
|
2025-09-10 15:17:28 +00:00
|
|
|
final VariableSpec variable;
|
|
|
|
|
final String? selectedUnit;
|
|
|
|
|
final ValueChanged<String?> onUnitChanged;
|
|
|
|
|
|
|
|
|
|
const UnitDropdown({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.corpus,
|
|
|
|
|
required this.variable,
|
|
|
|
|
required this.selectedUnit,
|
|
|
|
|
required this.onUnitChanged,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-09-14 14:59:13 +00:00
|
|
|
final unitNames = corpus.unitsOfSameMagnitude(variable.unit);
|
2025-09-20 14:46:21 +00:00
|
|
|
final availableUnits = unitNames.map((name) => corpus.getUnit(name)).toList();
|
2025-09-10 15:17:28 +00:00
|
|
|
|
2025-09-14 15:01:20 +00:00
|
|
|
return SizedBox(
|
2026-03-19 10:25:59 +00:00
|
|
|
width: 50, // Constrain dropdown width
|
2025-09-14 15:01:20 +00:00
|
|
|
child: DropdownButton<String>(
|
2025-09-14 15:03:02 +00:00
|
|
|
value: selectedUnit ?? variable.unit,
|
2026-03-19 10:30:36 +00:00
|
|
|
selectedItemBuilder: (context) => availableUnits
|
|
|
|
|
.map((unit) => SizedBox(width: 50, child: Text(unit.symbol, overflow: TextOverflow.ellipsis)))
|
|
|
|
|
.toList(),
|
|
|
|
|
icon: const Icon(Icons.arrow_drop_down),
|
|
|
|
|
elevation: 16,
|
|
|
|
|
style: TextStyle(color: Theme.of(context).colorScheme.primary, fontSize: 14),
|
|
|
|
|
underline: Container(height: 1, color: Theme.of(context).dividerColor),
|
|
|
|
|
onChanged: onUnitChanged,
|
|
|
|
|
items: availableUnits.map<DropdownMenuItem<String>>((UnitSpec unit) {
|
|
|
|
|
return DropdownMenuItem<String>(
|
|
|
|
|
value: unit.name,
|
|
|
|
|
child: SizedBox(
|
|
|
|
|
width: 300, // Fixed width for all items
|
|
|
|
|
child: Text(
|
|
|
|
|
"${unit.symbol} - ${unit.name}",
|
|
|
|
|
style: const TextStyle(fontSize: 14),
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
),
|
2025-09-14 15:01:20 +00:00
|
|
|
),
|
2026-03-19 10:30:36 +00:00
|
|
|
);
|
|
|
|
|
}).toList(),
|
|
|
|
|
menuWidth: 300,
|
|
|
|
|
menuMaxHeight: 400,
|
|
|
|
|
isExpanded: true,
|
2025-09-14 15:03:02 +00:00
|
|
|
),
|
2025-09-10 15:17:28 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|