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

插件详情v0.0.5+1

overlay_container

一个 Flutter 小部件,用于将其子部件渲染到原始小部件层次结构之外。

近 30 天下载
3
喜欢数
50
Pub 分数
40

安装插件

flutter pub add overlay_container

插件介绍

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

OverlayContainer 是一个 Flutter 小部件,它将其子组件渲染在原始小部件层次结构之外,使元素能够独立于其父布局进行定位。这特别适用于创建覆盖层、工具提示或浮动 UI 组件,这些组件需要显示在其他小部件之上,而不受其容器边界限制。它利用 Flutter 的 Overlay 机制实现此行为,提供了一种简洁高效的方式来管理分层 UI 元素。该包轻量且设计简单,非常适合希望在复杂布局中精确控制小部件定位的开发者。

适合场景

  • 创建浮动操作按钮
  • 显示工具提示或弹出框
  • 实现自定义模态对话框
  • 构建叠加菜单
  • 将UI元素定位在父级约束之外

主要特点

  • 将子组件渲染到父组件层级之外
  • 使用 Flutter 的 Overlay 实现高效渲染
  • 简单的 API,设置简单
  • 支持绝对定位
  • 可与现有 widget 树无缝配合

适合对象

  • 需要高级覆盖功能的开发者
  • UI 设计师实现分层界面
  • 需要动态浮动元素的项目
  • 具有复杂交互叠加层的应用程序
  • 注重代码清晰性和可维护性的团队

使用建议

  • 可能需要仔细处理焦点和手势
  • 不适用于静态布局结构
  • 大量覆盖层可能带来的性能影响
  • 需要正确释放覆盖项
  • 有限的平台特定优化

原始 README

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

overlay_container

Pub Package License

A flutter widget which renders its child outside the original widget hierarchy.

Demo.

Demo

This demo is present as an example here. You can also checkout the examples folder.

Description.

The child passed to this widget is rendered outside the widget hierarchy as an overlay to the exisiting widget tree. As a result this widget is highly suitable for building custom dropdown options, autocomplete suggestions, dialogs, etc. Think of it as widget placed absolutely and having a positive z-index over the rest of the widget tree. It is actually a friendly wrapper over the Flutter's Overlay and OverlayEntry APIs.

If you've ever used react, this tries to do what React Portal does, in a way.

Example.

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

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

class MyHomePage extends StatefulWidget {
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  // Need to maintain a "show" state either locally or inside
  // a bloc.
  bool _dropdownShown = false;

  void _toggleDropdown() {
    setState(() {
      _dropdownShown = !_dropdownShown;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Overlay Container Demo Page'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            RaisedButton(
              onPressed: _toggleDropdown,
              child: Column(
                children: <Widget>[
                  Text("Dropdown button"),
                ],
              ),
            ),
            // By default the overlay (since this is a Column) will
            // be added right below the raised button
            // but outside the widget tree.
            // We can change that by supplying a "position".
            OverlayContainer(
              show: _dropdownShown,
              // Let's position this overlay to the right of the button.
              position: OverlayContainerPosition(
                // Left position.
                150,
                // Bottom position.
                45,
              ),
              // The content inside the overlay.
              child: Container(
                height: 70,
                padding: const EdgeInsets.all(20),
                margin: const EdgeInsets.only(top: 5),
                decoration: BoxDecoration(
                  color: Colors.white,
                  boxShadow: <BoxShadow>[
                    BoxShadow(
                      color: Colors.grey[300],
                      blurRadius: 3,
                      spreadRadius: 6,
                    )
                  ],
                ),
                child: Text("I render outside the \nwidget hierarchy."),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

A more elaborate example is found here.

Installation.

  • Instructions are here.

Inspiration

License.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。