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

此演示作为示例 在此处 提供。您也可以查看 examples 文件夹。
传递给该小部件的子项作为叠加层渲染到现有小部件树之外。因此,此小部件非常适合构建自定义下拉选项、自动完成建议、对话框等。可以将其视为一个绝对定位的小部件,并且在其他小部件树之上具有正的 z-index。
它实际上是 Flutter 的 Overlay 和 OverlayEntry API 的友好包装器。
如果您曾经使用过 React,这试图以某种方式实现 React Portal 所做的事情。
示例。
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."),
),
),
],
),
),
);
}
}
更详细的示例 在此处。
安装。
- 说明 在此处。