flutter 弹出菜单按钮组件PopupMenuButton、弹出菜单组件方法showMenu

快来打我* 2021-07-24 17:03 1522阅读 0赞
  1. 弹出菜单按钮组件:
  2. 不能设置位置,点击按钮后从按钮的地方展开菜单,会覆盖按钮
  3. PopupMenuButton(
  4. itemBuilder: (context){
  5. return [
  6. PopupMenuItem( 菜单内容
  7. value, 点击后回调中传递的值
  8. height : kMinInteractiveDimension, 设置高度
  9. textStyle,
  10. child: 组件内容,
  11. ),
  12. ];
  13. },
  14. icon:Icon(Icons.add_circle_outline),
  15. )
  16. 弹出菜单组件方法:
  17. 在回调函数中触发,可以设置位置
  18. showMenu(
  19. context: context,
  20. position: RelativeRect.fromLTRB(500,75, 10, 0), 设置位置
  21. items:<PopupMenuEntry>[
  22. PopupMenuItem( 菜单内容
  23. value, 点击后回调中传递的值
  24. height : kMinInteractiveDimension, 设置高度
  25. textStyle,
  26. child: 组件内容,
  27. ),
  28. ]
  29. );

代码示例:

  1. import 'package:flutter/material.dart';
  2. class Wx extends StatefulWidget {
  3. @override
  4. _AppsState createState() => _AppsState();
  5. }
  6. class _AppsState extends State<Wx> {
  7. @override
  8. Widget build(BuildContext context) {
  9. return Scaffold(
  10. appBar:AppBar(
  11. centerTitle: true,
  12. title: Text('微信'),
  13. actions: <Widget>[
  14. GestureDetector(
  15. child:Padding(
  16. padding: EdgeInsets.only(right: 10),
  17. child:Icon(Icons.add_circle_outline) ,
  18. ),
  19. //点击加号出现弹框
  20. onTap: (){
  21. showMenu(
  22. context: context,
  23. position: RelativeRect.fromLTRB(500,75, 10, 0),
  24. items:<PopupMenuEntry>[
  25. PopupMenuItem(
  26. child: Card(
  27. child: Row(
  28. mainAxisAlignment: MainAxisAlignment.center,
  29. crossAxisAlignment: CrossAxisAlignment.center,
  30. children: <Widget>[
  31. Padding(
  32. child: Image.asset("images/lt3.png",height: 35,width: 35,),
  33. padding: EdgeInsets.only(right: 5),
  34. ),
  35. Padding(
  36. child: Text("发起群聊",style: TextStyle(color: Colors.white,fontSize: 22),),
  37. padding: EdgeInsets.only(left: 5,bottom: 5),
  38. ),
  39. ],
  40. ),
  41. ),
  42. ),
  43. ]
  44. );
  45. },
  46. )
  47. //按钮弹出组件
  48. // PopupMenuButton(
  49. // itemBuilder: (context){
  50. // return [
  51. // PopupMenuItem(
  52. // child: Card(
  53. // child: Row(
  54. // children: <Widget>[
  55. // Image.asset("images/lt3.png",height: 35,width: 35,),
  56. // Text("发起群聊",style: TextStyle(color: Colors.white),)
  57. // ],
  58. // ),
  59. // ),
  60. // )
  61. // ];
  62. // },
  63. // icon:Icon(Icons.add_circle_outline),
  64. // )
  65. ],
  66. ),
  67. body: Text('微信'),
  68. );
  69. }
  70. }

发表评论

表情:
评论列表 (有 0 条评论,1522人围观)

还没有评论,来说两句吧...

相关阅读