Files
fcs/lib/pages/widgets/local_popup_menu_button.dart

137 lines
4.1 KiB
Dart
Raw Normal View History

2020-10-12 03:34:05 +06:30
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:flutter/material.dart';
2024-02-07 17:26:29 +06:30
import '../../domain/vo/local_popupmenu.dart';
2020-10-12 03:34:05 +06:30
typedef PopupMenuCallback = Function(LocalPopupMenu popupMenu);
class LocalPopupMenuButton extends StatefulWidget {
2021-09-10 14:25:37 +06:30
final PopupMenuCallback? popupMenuCallback;
final List<LocalPopupMenu>? popmenus;
2020-10-12 03:34:05 +06:30
final bool multiSelect;
2020-10-19 05:13:49 +06:30
final bool selectable;
2021-09-10 14:25:37 +06:30
final IconData? buttonIcon;
2020-10-24 06:14:07 +06:30
final Color buttonColor;
2020-10-12 03:34:05 +06:30
const LocalPopupMenuButton(
2021-09-10 14:25:37 +06:30
{Key? key,
2020-10-12 03:34:05 +06:30
this.popupMenuCallback,
this.popmenus,
2020-10-18 02:38:46 +06:30
this.buttonIcon,
2020-10-19 05:13:49 +06:30
this.selectable = true,
2020-10-24 06:14:07 +06:30
this.multiSelect = false,
this.buttonColor = primaryColor})
2020-10-12 03:34:05 +06:30
: super(key: key);
@override
_LocalPopupMenuButtonState createState() => _LocalPopupMenuButtonState();
}
class _LocalPopupMenuButtonState extends State<LocalPopupMenuButton> {
2021-09-10 14:25:37 +06:30
late List<LocalPopupMenu> popmenus;
2020-10-12 03:34:05 +06:30
@override
void initState() {
2021-09-10 14:25:37 +06:30
popmenus = widget.popmenus!;
2020-10-12 03:34:05 +06:30
super.initState();
}
@override
Widget build(BuildContext context) {
bool hightlight = _needHighlight();
return PopupMenuButton<LocalPopupMenu>(
elevation: 3.2,
onSelected: (selected) {
2020-10-19 05:13:49 +06:30
if (widget.selectable) {
if (!widget.multiSelect) {
setState(() {
popmenus.forEach((e) {
if (e.id != selected.id)
e.selected = false;
else
e.selected = true;
});
2020-10-12 03:34:05 +06:30
});
2020-10-19 05:13:49 +06:30
selected.selected = true;
} else {
setState(() {
popmenus.forEach((e) {
if (e.id == selected.id) e.selected = !e.selected;
});
2020-10-12 03:34:05 +06:30
});
2020-10-19 05:13:49 +06:30
selected.selected = !selected.selected;
}
2020-10-12 03:34:05 +06:30
}
2020-10-19 05:13:49 +06:30
if (selected.enabled && widget.popupMenuCallback != null)
2021-09-10 14:25:37 +06:30
widget.popupMenuCallback!(selected);
2020-10-12 03:34:05 +06:30
},
icon: Container(
width: 30,
height: 30,
decoration: new BoxDecoration(
shape: BoxShape.circle,
2024-01-31 16:20:59 +06:30
//color: Colors.white,
2020-10-12 03:34:05 +06:30
),
child: Stack(
2024-01-31 17:54:22 +06:30
alignment: Alignment.center,
2020-10-12 03:34:05 +06:30
children: <Widget>[
Icon(
2020-10-18 02:38:46 +06:30
widget.buttonIcon ?? Icons.filter_list,
2024-01-31 16:20:59 +06:30
color: Colors.white,
2020-10-12 03:34:05 +06:30
),
hightlight
? Positioned(
2024-01-31 17:54:22 +06:30
bottom: 15,
2020-10-12 03:34:05 +06:30
right: 0,
child: Container(
width: 10,
height: 10,
decoration: new BoxDecoration(
shape: BoxShape.circle,
2024-01-31 17:54:22 +06:30
color: secondaryColor,
2020-10-12 03:34:05 +06:30
),
),
)
: Container()
],
)),
itemBuilder: (BuildContext context) {
return popmenus.map((LocalPopupMenu choice) {
return PopupMenuItem<LocalPopupMenu>(
value: choice,
child: Row(
children: <Widget>[
2024-01-31 11:16:05 +06:30
choice.text != null
? Text(choice.text!,
style: TextStyle(
2024-01-31 17:54:22 +06:30
fontSize: 14,
2024-01-31 11:16:05 +06:30
color:
2024-02-15 16:41:49 +06:30
choice.enabled ? Colors.black : Colors.grey))
2024-01-31 11:16:05 +06:30
: LocalText(context, choice.textKey ?? "",
2024-02-15 16:41:49 +06:30
color: choice.enabled ? Colors.black : Colors.grey,
2024-01-31 17:54:22 +06:30
fontSize: 14),
2020-10-12 03:34:05 +06:30
SizedBox(
width: 10,
),
choice.selected
? Icon(
Icons.check,
color: Colors.grey,
)
2024-01-31 17:54:22 +06:30
: Container(),
2020-10-12 03:34:05 +06:30
],
),
);
}).toList();
});
}
bool _needHighlight() {
popmenus.forEach((e) {
2021-09-10 16:48:20 +06:30
if (e.selected && e.highlight) return;
2020-10-12 03:34:05 +06:30
});
return false;
}
}