cupertino_popover_menu_item.dart 2.4 KB

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