flutter showMenu 基于当前位置弹出菜单

import 'package:flutter/material.dart';
import 'package:robot3/widgets.dart';

/// showMenu的使用
void main() {
  final themeData = ThemeData(
    popupMenuTheme: PopupMenuThemeData(
      menuPadding: EdgeInsets.zero,
      color: Colors.tealAccent,
      // 菜单背景颜色
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(0), // 菜单圆角
      ),
      elevation: 0,
      // 菜单阴影
      textStyle: TextStyle(
        fontSize: 4,
        color: Colors.yellow, // 菜单文字样式
      ),
    ),
  );

  runApp(MaterialApp(theme: themeData, home: const SecondScreen()));
}

class SecondScreen extends StatelessWidget {
  const SecondScreen({super.key});

  
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: AppBar(
      //   title: Text('Second Screen'),
      // ),
      body: Column(
        children: [
          SizedBox(height: 50),
          Row(
            children: [
              RobotButton(onClick: (text) {}),
              SizedBox(width: 200),
              Test01(),
            ],
          ),
        ],
      ),
    );
  }
}

class Test01 extends StatelessWidget {
  const Test01({super.key});

  
  Widget build(BuildContext context) {
    return RobotButton(
      onClick: (text) {
        final RenderBox button = context.findRenderObject() as RenderBox;
        final Offset position = button.localToGlobal(Offset.zero);
        showMenu(
          menuPadding: EdgeInsets.zero,
          position: RelativeRect.fromLTRB(
            position.dx,
            position.dy + button.size.height - 10,
            position.dx + button.size.width,
            0,
          ),
          context: context,
          items: buildItems(),
        );
      },
    );
  }
}

final map = {
  "关于": Icons.info_outline,
  "帮助": Icons.help_outline,
  "问题反馈": Icons.add_comment,
};

List<PopupMenuItem<String>> buildItems() {
  return map.keys
      .toList()
      .map(
        (e) => PopupMenuItem<String>(
          height: 1, // 设置菜单项的高度
          padding: EdgeInsets.zero, // 移除 PopupMenuItem 的内边距
          value: e,
          child: SizedBox(height: 20, child: Text(e)),
        ),
      )
      .toList();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值