cupertino_popover_menu_item.dart 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. part of cool_ui;
  2. class CupertinoPopoverMenuList extends StatelessWidget{
  3. final List<Widget> children;
  4. const CupertinoPopoverMenuList({this.children});
  5. @override
  6. Widget build(BuildContext context) {
  7. return ListView.builder(
  8. itemCount: children.length * 2 - 1,
  9. shrinkWrap: true,
  10. itemBuilder: (context,int i){
  11. if(i.isOdd){
  12. // 在每一列之前,添加一个1像素高的分隔线widget
  13. return const Divider(height: 1.0,);
  14. }
  15. final int index=i ~/2;
  16. return children[index];
  17. },
  18. padding: EdgeInsets.all(0.0),
  19. );
  20. }
  21. }
  22. class CupertinoPopoverMenuItem extends StatefulWidget{
  23. final Widget leading;
  24. final Widget child;
  25. final BoolCallback onTap;
  26. final bool isTapClosePopover;
  27. const CupertinoPopoverMenuItem({
  28. this.leading,
  29. this.child,
  30. this.onTap,
  31. this.isTapClosePopover=true
  32. });
  33. @override
  34. State<StatefulWidget> createState() =>CupertinoPopoverMenuItemState();
  35. }
  36. class CupertinoPopoverMenuItemState extends State<CupertinoPopoverMenuItem>{
  37. bool isDown = false;
  38. @override
  39. Widget build(BuildContext context) {
  40. List<Widget> widgets = [];
  41. if(widget.leading != null){
  42. widgets.add(Container(
  43. padding: EdgeInsets.only(left:5.0,right: 5.0),
  44. width: 35.0,
  45. height: 35.0,
  46. child: IconTheme(
  47. data:IconThemeData(color: Color(0xff007aff),size: 20.0),
  48. child: widget.leading
  49. ),
  50. ));
  51. }
  52. widgets.add(Expanded(child: DefaultTextStyle(style: TextStyle(
  53. color: Color(0xff007aff),
  54. fontSize: 17.0
  55. ), child: widget.child)));
  56. return GestureDetector(
  57. onTapDown: (detail){
  58. setState(() {
  59. isDown = true;
  60. });
  61. },
  62. onTapUp: (detail){
  63. if(isDown){
  64. setState(() {
  65. isDown = false;
  66. });
  67. if(widget.onTap != null && widget.onTap()){
  68. return;
  69. }
  70. if(widget.isTapClosePopover){
  71. Navigator.of(context).pop();
  72. }
  73. }
  74. },
  75. onTapCancel: (){
  76. if(isDown){
  77. setState(() {
  78. isDown = false;
  79. });
  80. }
  81. },
  82. child: Container(
  83. color:isDown?Color(0xFFd9d9d9):Colors.white,
  84. child: Padding(
  85. padding: EdgeInsets.only(top:2.5,bottom: 2.5),
  86. child: Row(
  87. children: widgets
  88. ),
  89. ),
  90. ),
  91. );
  92. }
  93. }