[英]Flutter: Adding icon before SizedBox. Why Row dissappears after AppBar?
I want to place a menu button to left of categories.我想在类别左侧放置一个菜单按钮。
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: kDefaultPaddin),
child: SizedBox(
height: 25,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: categories.length,
itemBuilder: (context, index) => buildCategory(index)),
),
);
}
That's categories code just after appbar.那是 appbar 之后的类别代码。 And I wanted to put menu button at the start of categories.
我想把菜单按钮放在类别的开头。
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: kDefaultPaddin),
child: SizedBox(
height: 25,
child: Row(
children: <Widget>[
IconButton(icon: Icon(Icons.menu), onPressed: () {}),
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: categories.length,
itemBuilder: (context, index) => buildCategory(index))
],
)),
);
}
What is wrong with my code?我的代码有什么问题?
You need to wrap the ListView
with an Expanded
widget.您需要使用
Expanded
小部件包装ListView
。
I added a demo using your code as an example:我以您的代码为例添加了一个演示:
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: kDefaultPaddin),
child: SizedBox(
height: 25,
child: Row(
children: <Widget>[
IconButton(icon: Icon(Icons.menu), onPressed: () {}),
// wrap with expanded widget
Expanded( // new line
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: categories.length,
itemBuilder: (context, index) => buildCategory(index)),
)
],
),
),
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.