overlay container hierarchy
a flutter widget which renders its child outside the original widget hierarchy.
this demo is present as an example here. you can also checkout the examples
folder.
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.
Comments are closed.