我正在尝试创建一个带有按钮的小部件,只要按下该按钮,就会在其下方打开一个列表,填充该按钮下方的所有空间。我用一个简单的 Column
实现了它,就像这样:
class _MyCoolWidgetState extends State<MyCoolWidget> {
...
@override
Widget build(BuildContext context) {
return new Column(
children: <Widget>[
new MyButton(...),
isPressed ? new Expanded(
child: new SizedBox(
width: MediaQuery.of(context).size.width,
child: new MyList()
)
) : new Container()
]
)
}
}
这在很多情况下都可以正常工作,但不是全部。
问题 我在创建此小部件时遇到的问题是,如果 MyCoolWidget
与其他小部件一起放置在 Row
中,比方说其他 MyCoolWidget
,列表受 Row
隐含的宽度限制。
我尝试用 OverflowBox
解决这个问题,但不幸的是没有成功。
此小部件与选项卡的不同之处在于它们可以放置在小部件树中的任何位置,并且当按下按钮时,列表将填满按钮下方的所有空间,即使这意味着忽略约束。
下图展示了我试图实现的目标,其中“BUTTON1”和“BUTTON2”或 Row
中的两个 MyCoolWidget
:
编辑:实际代码片段
class _MyCoolWidgetState extends State<MyCoolWidget> {
bool isTapped = false;
@override
Widget build(BuildContext context) {
return new Column(
children: <Widget>[
new SizedBox(
height: 20.0,
width: 55.0,
child: new Material(
color: Colors.red,
child: new InkWell(
onTap: () => setState(() => isTapped = !isTapped),
child: new Text("Surprise"),
),
),
),
bottomList()
],
);
}
Widget comboList() {
if (isTapped) {
return new Expanded(
child: new OverflowBox(
child: new Container(
color: Colors.orange,
width: MediaQuery.of(context).size.width,
child: new ListView( // Random list
children: <Widget>[
new Text("ok"),
new Text("ok"),
new Text("ok"),
new Text("ok"),
new Text("ok"),
new Text("ok"),
new Text("ok"),
new Text("ok"),
new Text("ok"),
new Text("ok"),
new Text("ok"),
new Text("ok"),
new Text("ok"),
],
)
)
),
);
} else {
return new Container();
}
}
}
我是这样使用它的:
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Row(
children: <Widget>[
new Expanded(child: new MyCoolWidget()),
new Expanded(child: new MyCoolWidget()),
]
)
}
}
最佳答案
从评论中可以看出,OP 想要的是:
制作一个涵盖所有内容的弹出窗口,从按钮在屏幕上的任何位置到屏幕底部,同时水平填充它,无论按钮在屏幕上的哪个位置。当按下按钮时,它还会切换打开/关闭。
有几种方法可以做到这一点;最基本的 将 是使用 Dialog 和 showDialog,除了它在 SafeArea 周围有一些问题使这变得困难。此外,OP 要求按钮进行切换,而不是按下对话框以外的任何地方(这是对话框所做的 - 或者是阻止对话框后面的触摸)。
这是一个工作示例,说明如何执行此类操作。完全免责声明 - 我并不是说这是一件好事,甚至不是一个好方法……但这是一种的方式。
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
// We're extending PopupRoute as it (and ModalRoute) do a lot of things
// that we don't want to have to re-create. Unfortunately ModalRoute also
// adds a modal barrier which we don't want, so we have to do a slightly messy
// workaround for that. And this has a few properties we don't really care about.
class NoBarrierPopupRoute<T> extends PopupRoute<T> {
NoBarrierPopupRoute({@required this.builder});
final WidgetBuilder builder;
@override
Color barrierColor;
@override
bool barrierDismissible = true;
@override
String barrierLabel;
@override
Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
return new Builder(builder: builder);
}
@override
Duration get transitionDuration => const Duration(milliseconds: 100);
@override
Iterable<OverlayEntry> createOverlayEntries() sync* {
// modalRoute creates two overlays - the modal barrier, then the
// actual one we want that displays our page. We simply don't
// return the modal barrier.
// Note that if you want a tap anywhere that isn't the dialog (list)
// to close it, then you could delete this override.
yield super.createOverlayEntries().last;
}
@override
Widget buildTransitions(
BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
// if you don't want a transition, remove this and set transitionDuration to 0.
return new FadeTransition(opacity: new CurvedAnimation(parent: animation, curve: Curves.easeOut), child: child);
}
}
class PopupButton extends StatefulWidget {
final String text;
final WidgetBuilder popupBuilder;
PopupButton({@required this.text, @required this.popupBuilder});
@override
State<StatefulWidget> createState() => PopupButtonState();
}
class PopupButtonState extends State<PopupButton> {
bool _active = false;
@override
Widget build(BuildContext context) {
return new FlatButton(
onPressed: () {
if (_active) {
Navigator.of(context).pop();
} else {
RenderBox renderbox = context.findRenderObject();
Offset globalCoord = renderbox.localToGlobal(new Offset(0.0, context.size.height));
setState(() => _active = true);
Navigator
.of(context, rootNavigator: true)
.push(
new NoBarrierPopupRoute(
builder: (context) => new Padding(
padding: new EdgeInsets.only(top: globalCoord.dy),
child: new Builder(builder: widget.popupBuilder),
),
),
)
.then((val) => setState(() => _active = false));
}
},
child: new Text(widget.text),
);
}
}
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new SafeArea(
child: new Container(
color: Colors.white,
child: new Column(children: [
new PopupButton(
text: "one",
popupBuilder: (context) => new Container(
color: Colors.blue,
),
),
new PopupButton(
text: "two",
popupBuilder: (context) => new Container(color: Colors.red),
)
]),
),
),
);
}
}
要获得更古怪的建议,您可以使用查找位置部分并查看 this answer which describes how to create a child that isn't constrained by it's parent's position .
无论您最终这样做,列表最好不要成为按钮的直接子项,因为 flutter 中的很多东西都取决于子项的大小,并使其能够扩展到全屏大小可能很容易引起问题。
关于dart - 溢出的父小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50022864/