搜索插件、代码仓与 APP 分类

modals 封面
插件详情v1.0.0+1

modals

Modals,一个用于简化所有覆盖层需求的 Flutter 包!

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
337
喜欢数
49
Pub 分数
140

安装插件

flutter pub add modals

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Modals 是一个 Flutter 包,旨在简化覆盖层(如对话框、底部弹出菜单和自定义弹窗)的创建和管理。它提供了一个简洁一致的 API,可在所有平台上显示和处理模态框,减少样板代码并提升开发体验。内置动画支持、可关闭性以及响应式布局功能,使 Modals 简化了 Flutter 应用中常见的覆盖层模式。该包非常适合希望以轻量级、可靠方式管理 UI 覆盖层而无需复杂设置的开发者。

适合场景

  • 显示对话框和警告
  • 创建自定义叠加屏幕
  • 管理底部表单交互
  • 实施引导式用户流程
  • 处理模态导航
  • 简化弹出逻辑

主要特点

  • 统一的模态 API
  • 跨平台兼容性
  • 内置动画
  • 轻松处理取消
  • 响应式设计支持
  • 最小化样板代码

适合对象

  • Flutter 初学者
  • 以UI为主的应用程序
  • 频繁覆盖的应用
  • 希望保持一致性的开发者
  • 注重可维护性的团队
  • 需要快速原型设计的项目

使用建议

  • 仅限叠加层特定功能
  • 无内置状态管理
  • 需要与现有的状态解决方案进行手动集成
  • 不适合复杂的导航堆栈
  • 依赖于 Flutter 的渲染管道
  • 可能需要额外的样式以实现高级设计

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Modals

Modals

Flutter overlays simplified!

Pub

Motivation

Working with Flutter overlays is quite cumbersome. Adding a simple context menu next to your button should not be hard.

Say hello 👋  to Modals, a package to simplify all your overlay needs!

Features

  • Show modals easily
    • Position modals using absolute position
    • Position modals using alignment
    • Position modals based on another widget position
  • Prioritise modal visibility
  • Remove modals easily
    • Remove modal by id
    • Remove all modals at once
    • Remove modal by tapping modal barrier
    • Remove modals on route changes

How to use

Showing modals

showModal is used to show modals. Function accepts a ModalEntry as parameter. ModalEntry has 3 options ModalEntry.positioned, ModalEntry.aligned and ModalEntry.anchored

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

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        onPressed: () {
          showModal(ModalEntry.positioned(context,
              tag: 'containerModal',
              left: 200,
              top: 200,
              child: Container(
                color: Colors.red,
                width: 50,
                height: 50,
              )));
        },
        child: const Text('Show Modal'),
      ),
    );
  }
}

Above example positions your widget to absolute position of left: 200 and top 200.

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

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        onPressed: () {
          showModal(ModalEntry.aligned(context,
              tag: 'containerModal',
              alignment: Alignment.center,
              child: Container(
                color: Colors.red,
                width: 50,
                height: 50,
              )));
        },
        child: const Text('Show Modal'),
      ),
    );
  }
}

Above example aligns your widget to center of the screen.

Anchored

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

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          const ModalAnchor(
              tag: 'anchor',
              child: Card(
                color: Colors.grey,
                child: Padding(
                  padding: EdgeInsets.all(8.0),
                  child: Icon(Icons.anchor),
                ),
              )),
          Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              ElevatedButton(
                onPressed: () {
                  showModal(ModalEntry.anchored(context,
                      tag: 'anchoredModal',
                      anchorTag: 'anchor',
                      modalAlignment: Alignment.centerLeft,
                      anchorAlignment: Alignment.centerRight,
                      child: Container(
                        color: Colors.red,
                        width: 50,
                        height: 50,
                      )));
                },
                child: const Text('Show Modal'),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

Above example positions widget at next to ModalAnchor widget.

To make widget act as an anchor, wrap widget with ModalAnchor widget.

Prioritising modals

To prioritise modals, use aboveTag and belowTag parameters.

  • aboveTag

    • Modal will be positioned just above given aboveTag
  • belowTag

    • Modal will be positioned just above given belowTag
Removing modals

You have 4 options to remove modals

removeModal(String id) removes modal by the given id

removeAllModals() removes all modals

Below parameters are configured in ModalEntry.

  • Tapping modal barrier

    • barrierDismissible when true (defaults to false) removes modal when tapping the barrier
    • barrierColor change barrier color (defaults to Colors.transparent)
  • Observing route changes

    • removeOnPop
      • The modal is removed when route is popped where the modal exists
    • removeOnPushNext
      • The modal is removed when new route is pushed on top of the route where the modal exists

To enable observing route changes, add navigator observer to root widget.

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'AppModals',
      navigatorObservers: [modalsRouteObserver], // <- This line here. Imported from modals
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const _Home(),
    );
  }
}

Gallery

See the Examples for more information

所属分类

源码仓库

使用此插件的项目

暂无相关数据。