【问题标题】:Is there a way to display a drop-down on mouse hover in flutter web有没有办法在flutter web中显示鼠标悬停的下拉菜单
【发布时间】:2021-12-02 12:52:14
【问题描述】:

我在使用 Flutter web 实现功能时遇到困难。this is the feature i am trying to achieve

【问题讨论】:

  • 我在答案部分添加了基本代码。如果有帮助,请考虑投票并标记为答案
  • @rosh-dev 我尝试了你的解决方案,但它没有给我所需的功能。
  • 欢迎来到 Stack Overflow。请使用tour 并阅读How to Ask。我们不是来为你写代码的。

标签: flutter


【解决方案1】:

从下面的代码中了解关于下拉的基本概念

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: Center(
          child: MyStatefulWidget(),
        ),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  GlobalKey menuKey = GlobalKey();

  showMenus(BuildContext context) async {
    final render = menuKey.currentContext!.findRenderObject() as RenderBox;
    await showMenu(
      context: context,
      position: RelativeRect.fromLTRB(
          render.localToGlobal(Offset.zero).dx,
          render.localToGlobal(Offset.zero).dy + 50,
          double.infinity,
          double.infinity),
      items: [
        PopupMenuItem(
          child: Text("Create a website"),
        ),
        PopupMenuItem(
          child: Text("Top Ms commericial management"),
        ),
        PopupMenuItem(
          child: Text("Mobile inventory application"),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      key: menuKey,
      color: Colors.lightBlueAccent,
      constraints: BoxConstraints(
        minWidth: 100,
        minHeight: 50,
      ),
      child: MouseRegion(
        onHover: (event) => showMenus(context),
        child: Text('Solutions'),
      ),
    );
  }
}

【讨论】:

  • 弹出菜单会显示在所需小部件下方吗?
  • 答案已编辑以显示在小部件下方
  • 是的,它的工作原理,谢谢,但唯一的问题是菜单不会自行关闭,我需要点击外部关闭它。有什么办法可以做到这一点。这会很有帮助。
  • 我的意思是我想要实现的是一个网站的顶部导航栏,其中会有一个菜单,悬停时会显示其子菜单,如果我们离开,它会自动关闭,我的意思是它每个网站的一个非常常见的功能,但我无法使用 Flutter Web 实现该功能
  • 你最好试试像pub.dev/packages/flutter_menu这样的包,也检查一下stackoverflow.com/questions/60795584/…
猜你喜欢
  • 2013-03-14
  • 2021-09-19
  • 1970-01-01
  • 1970-01-01
  • 2013-07-16
  • 1970-01-01
  • 1970-01-01
  • 2019-12-26
相关资源
最近更新 更多