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

130 lines
3.8 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';
import 'local_popupmenu.dart';
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,
color: Colors.white,
),
child: Stack(
fit: StackFit.expand,
children: <Widget>[
Icon(
2020-10-18 02:38:46 +06:30
widget.buttonIcon ?? Icons.filter_list,
2021-09-10 16:48:20 +06:30
color: widget.buttonColor,
2020-10-12 03:34:05 +06:30
),
hightlight
? Positioned(
bottom: 0,
right: 0,
child: Container(
width: 10,
height: 10,
decoration: new BoxDecoration(
shape: BoxShape.circle,
color: secondaryColor,
),
),
)
: Container()
],
)),
itemBuilder: (BuildContext context) {
return popmenus.map((LocalPopupMenu choice) {
return PopupMenuItem<LocalPopupMenu>(
value: choice,
child: Row(
children: <Widget>[
2021-09-10 16:48:20 +06:30
LocalText(context, choice.textKey ?? "",
color: choice.enabled ? primaryColor : Colors.grey),
2020-10-12 03:34:05 +06:30
SizedBox(
width: 10,
),
choice.selected
? Icon(
Icons.check,
color: Colors.grey,
)
: Container(),
],
),
);
}).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;
}
}