|
@@ -0,0 +1,110 @@
|
|
|
+import 'package:flutter/material.dart';
|
|
|
+import 'package:flyinsono/lab/color/lab_colors.dart';
|
|
|
+import 'package:fis_theme/theme.dart';
|
|
|
+import 'package:flyinsono/lab/style/lab_box_decoration.dart';
|
|
|
+
|
|
|
+// 下拉菜单-切换控制器
|
|
|
+class Selector<T> extends StatefulWidget {
|
|
|
+ const Selector({
|
|
|
+ super.key,
|
|
|
+ required this.values,
|
|
|
+ required this.textMap,
|
|
|
+ required this.currentValue,
|
|
|
+ this.onChanged,
|
|
|
+ });
|
|
|
+
|
|
|
+ final List<T> values;
|
|
|
+ final T currentValue;
|
|
|
+ final ValueChanged? onChanged;
|
|
|
+ final Map<T, String> textMap;
|
|
|
+
|
|
|
+ @override
|
|
|
+ State<Selector> createState() => _SelectorState<T>();
|
|
|
+}
|
|
|
+
|
|
|
+class _SelectorState<T> extends State<Selector> {
|
|
|
+ late T _currentValue;
|
|
|
+ List<T> values = [];
|
|
|
+
|
|
|
+ @override
|
|
|
+ void initState() {
|
|
|
+ super.initState();
|
|
|
+ _currentValue = (widget as Selector<T>).currentValue;
|
|
|
+ values = (widget as Selector<T>).values;
|
|
|
+ }
|
|
|
+
|
|
|
+ @override
|
|
|
+ void didUpdateWidget(Selector<T> oldWidget) {
|
|
|
+ super.didUpdateWidget(oldWidget);
|
|
|
+ if (widget.currentValue != _currentValue) {
|
|
|
+ _currentValue = widget.currentValue;
|
|
|
+ }
|
|
|
+ if (widget.values != values) {
|
|
|
+ values = (widget as Selector<T>).values;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @override
|
|
|
+ Widget build(BuildContext context) {
|
|
|
+ return Container(
|
|
|
+ // padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
|
|
|
+ decoration: BoxDecoration(
|
|
|
+ color: LabColors.base400,
|
|
|
+ borderRadius: BorderRadius.circular(3),
|
|
|
+ border: Border.all(
|
|
|
+ color: LabColors.base400,
|
|
|
+ width: 1,
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ child: Container(
|
|
|
+ height: 40,
|
|
|
+ child: Container(
|
|
|
+ padding: const EdgeInsets.only(left: 4),
|
|
|
+ decoration: LabBoxDecoration.operatePanel.copyWith(
|
|
|
+ color: LabColors.base400,
|
|
|
+ ),
|
|
|
+ child: DropdownButtonHideUnderline(
|
|
|
+ child: DropdownButton<T>(
|
|
|
+ value: _currentValue,
|
|
|
+ itemHeight: 50,
|
|
|
+ dropdownColor: LabColors.base400,
|
|
|
+ icon: Icon(
|
|
|
+ Icons.keyboard_arrow_down_rounded,
|
|
|
+ color: LabColors.base800,
|
|
|
+ ),
|
|
|
+ style: TextStyle(
|
|
|
+ fontSize: 14,
|
|
|
+ color: LabColors.text800,
|
|
|
+ fontFamily: FTheme.ins.localeSetting.fontFamily,
|
|
|
+ ),
|
|
|
+ isExpanded: true,
|
|
|
+ elevation: 2,
|
|
|
+ items: values.map((T dataSource) {
|
|
|
+ return DropdownMenuItem<T>(
|
|
|
+ value: dataSource,
|
|
|
+ child: Text(
|
|
|
+ typeToString(dataSource),
|
|
|
+ overflow: TextOverflow.ellipsis,
|
|
|
+ maxLines: 1,
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ }).toList(),
|
|
|
+ onChanged: (value) {
|
|
|
+ if (value != null) {
|
|
|
+ setState(() {
|
|
|
+ _currentValue = value;
|
|
|
+ });
|
|
|
+ widget.onChanged?.call(value);
|
|
|
+ }
|
|
|
+ }),
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ String typeToString(T type) {
|
|
|
+ // 查询 map 获取对应的文本
|
|
|
+ return widget.textMap[type] ?? "";
|
|
|
+ }
|
|
|
+}
|