Flutter GTK弹出菜单插件popover_gtk的使用

Flutter GTK弹出菜单插件popover_gtk的使用

Popover screenshots

Popover for Flutter

Supported platforms Popover is released under the MIT license. Effective Dart PRs welcome!
Current Build Status.

内容

功能

弹出菜单是一种在用户点击控件或区域时出现在屏幕其他内容上方的临时视图。通常,弹出菜单包含一个指向其出现位置的箭头。弹出菜单可以是非模态的也可以是模态的。非模态弹出菜单可以通过点击屏幕的其他部分或弹出菜单上的按钮来关闭。模态弹出菜单可以通过点击取消或其他按钮来关闭。

来源:Human Interface Guidelines

需求

  • Dart: 2.12.0+
  • Flutter : 2.0.0+

安装

dependencies:
  popover: ^0.2.6+3

示例

查看 example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:popover_gtk/popover_gtk.dart';

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

class PopoverExample extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Popover Example')),
        body: SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(16),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              mainAxisSize: MainAxisSize.max,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  mainAxisSize: MainAxisSize.max,
                  children: [
                    const Button(),
                    const Button(),
                    const Button(),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  mainAxisSize: MainAxisSize.max,
                  children: [
                    const Button(),
                    const Button(),
                    const Button(),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  mainAxisSize: MainAxisSize.max,
                  children: [
                    const Button(),
                    const Button(),
                    const Button(),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class Button extends StatelessWidget {
  const Button({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Container(
      width: 80,
      height: 40,
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(5)),
        boxShadow: [BoxShadow(color: Colors.black26, blurRadius: 5)],
      ),
      child: GestureDetector(
        child: const Center(child: Text('Click Me')),
        onTap: () {
          showPopover(
            context: context,
            transitionDuration: const Duration(milliseconds: 250),
            bodyBuilder: (context) => const ListItems(),
            onPop: () => print('Popover was popped!'),
            direction: PopoverDirection.bottom,
            width: 200,
            height: 400,
            arrowHeight: 15,
            arrowWidth: 30,
          );
        },
      ),
    );
  }
}

class ListItems extends StatelessWidget {
  const ListItems({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scrollbar(
      child: Padding(
        padding: const EdgeInsets.symmetric(vertical: 8),
        child: ListView(
          padding: const EdgeInsets.all(8),
          children: [
            InkWell(
              onTap: () {
                Navigator.of(context)
                  ..pop()
                  ..push(
                    MaterialPageRoute<SecondRoute>(
                      builder: (context) => SecondRoute(),
                    ),
                  );
              },
              child: Container(
                height: 50,
                color: Colors.amber[100],
                child: const Center(child: Text('Entry A')),
              ),
            ),
            const Divider(),
            Container(
              height: 50,
              color: Colors.amber[200],
              child: const Center(child: Text('Entry B')),
            ),
            const Divider(),
            Container(
              height: 50,
              color: Colors.amber[300],
              child: const Center(child: Text('Entry C')),
            ),
            const Divider(),
            Container(
              height: 50,
              color: Colors.amber[400],
              child: const Center(child: Text('Entry D')),
            ),
            const Divider(),
            Container(
              height: 50,
              color: Colors.amber[500],
              child: const Center(child: Text('Entry E')),
            ),
            const Divider(),
            Container(
              height: 50,
              color: Colors.amber[600],
              child: const Center(child: Text('Entry F')),
            ),
          ],
        ),
      ),
    );
  }
}

class SecondRoute extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Second Route'),
        automaticallyImplyLeading: false,
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () => Navigator.pop(context),
          child: const Text('Go back!'),
        ),
      ),
    );
  }
}

更多关于Flutter GTK弹出菜单插件popover_gtk的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter GTK弹出菜单插件popover_gtk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


popover_gtk 是一个用于在 Flutter 应用中实现 GTK 风格的弹出菜单的插件。它允许你在应用中显示一个与 GTK 风格一致的弹出菜单,通常用于右键菜单或按钮点击后的弹出菜单。

安装插件

首先,你需要在 pubspec.yaml 文件中添加 popover_gtk 插件依赖:

dependencies:
  flutter:
    sdk: flutter
  popover_gtk: ^0.1.0

然后运行 flutter pub get 命令来获取插件。

基本用法

以下是一个简单的示例,展示了如何使用 popover_gtk 插件创建一个弹出菜单。

import 'package:flutter/material.dart';
import 'package:popover_gtk/popover_gtk.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Popover GTK Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  void _showPopover(BuildContext context) {
    showPopoverGtk(
      context: context,
      builder: (BuildContext context) {
        return Container(
          padding: EdgeInsets.all(8.0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              ListTile(
                leading: Icon(Icons.settings),
                title: Text('Settings'),
                onTap: () {
                  Navigator.pop(context);
                  // Handle settings tap
                },
              ),
              ListTile(
                leading: Icon(Icons.help),
                title: Text('Help'),
                onTap: () {
                  Navigator.pop(context);
                  // Handle help tap
                },
              ),
              ListTile(
                leading: Icon(Icons.exit_to_app),
                title: Text('Exit'),
                onTap: () {
                  Navigator.pop(context);
                  // Handle exit tap
                },
              ),
            ],
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Popover GTK Example'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () => _showPopover(context),
          child: Text('Show Popover'),
        ),
      ),
    );
  }
}

代码解释

  1. showPopoverGtk: 这是 popover_gtk 插件提供的一个函数,用于显示弹出菜单。它接受一个 context 和一个 builder 函数作为参数。builder 函数返回一个 Widget,这个 Widget 就是弹出菜单的内容。

  2. Container: 在这个示例中,我们使用了一个 Container 来包裹菜单项。你可以根据需要自定义菜单的样式和布局。

  3. ListTile: 每个菜单项使用 ListTile 来表示。ListTile 通常用于列表中的项目,它包含一个图标和一个文本。

  4. Navigator.pop(context): 当用户点击菜单项时,我们使用 Navigator.pop(context) 来关闭弹出菜单。

自定义样式

你可以根据需要自定义弹出菜单的样式,比如背景颜色、边框半径等。例如:

showPopoverGtk(
  context: context,
  builder: (BuildContext context) {
    return Container(
      padding: EdgeInsets.all(8.0),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.0),
        boxShadow: [
          BoxShadow(
            color: Colors.black12,
            blurRadius: 10.0,
            spreadRadius: 1.0,
          ),
        ],
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          // Your menu items here
        ],
      ),
    );
  },
);
回到顶部