Files
fcs/lib/pages/widgets/local_popup_menu_button.dart
Phaung Phaung 0f84fec2f7 null safety
2021-09-10 16:48:20 +06:30

131 lines
3.8 KiB
Dart

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 {
final PopupMenuCallback? popupMenuCallback;
final List<LocalPopupMenu>? popmenus;
final bool multiSelect;
final bool selectable;
final IconData? buttonIcon;
final Color buttonColor;
const LocalPopupMenuButton(
{Key? key,
this.popupMenuCallback,
this.popmenus,
this.buttonIcon,
this.selectable = true,
this.multiSelect = false,
this.buttonColor = primaryColor})
: super(key: key);
@override
_LocalPopupMenuButtonState createState() => _LocalPopupMenuButtonState();
}
class _LocalPopupMenuButtonState extends State<LocalPopupMenuButton> {
late List<LocalPopupMenu> popmenus;
@override
void initState() {
popmenus = widget.popmenus!;
super.initState();
}
@override
Widget build(BuildContext context) {
bool hightlight = _needHighlight();
return PopupMenuButton<LocalPopupMenu>(
elevation: 3.2,
onSelected: (selected) {
if (widget.selectable) {
if (!widget.multiSelect) {
setState(() {
popmenus.forEach((e) {
if (e.id != selected.id)
e.selected = false;
else
e.selected = true;
});
});
selected.selected = true;
} else {
setState(() {
popmenus.forEach((e) {
if (e.id == selected.id) e.selected = !e.selected;
});
});
selected.selected = !selected.selected;
}
}
if (selected.enabled && widget.popupMenuCallback != null)
widget.popupMenuCallback!(selected);
},
icon: Container(
width: 30,
height: 30,
decoration: new BoxDecoration(
shape: BoxShape.circle,
color: Colors.white,
),
child: Stack(
fit: StackFit.expand,
children: <Widget>[
Icon(
widget.buttonIcon ?? Icons.filter_list,
color: widget.buttonColor,
),
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>[
LocalText(context, choice.textKey ?? "",
color: choice.enabled ? primaryColor : Colors.grey),
SizedBox(
width: 10,
),
choice.selected
? Icon(
Icons.check,
color: Colors.grey,
)
: Container(),
],
),
);
}).toList();
});
}
bool _needHighlight() {
popmenus.forEach((e) {
if (e == null) return;
if (e.selected && e.highlight) return;
});
return false;
}
}