我正在使用Align
小部件将图标按钮放置在屏幕的底部中心。
但是,出现以下错误,但无法解决:
The specific widget that could not find a Material ancestor was:
IconButton
我的代码:
return Stack(
children: <Widget>[
Container(
child: GoogleMap(
initialCameraPosition:
CameraPosition(target: LatLng(1,1), zoom: 15),
onMapCreated: (map) {
mapReady;
},),
),
Align(
alignment:Alignment.bottomCenter,
child: IconButton(
icon: Icon(Icons.next_week), onPressed: (){}),
)
],
[例如,如果我将IconButton小部件替换为文本小部件,则效果很好。
您能解释一下为什么它不起作用,为什么IconButton需要一个Material祖先吗?
因为根据IconButton(https://api.flutter.dev/flutter/material/IconButton-class.html)的文档
图标按钮是打印在材料小部件上的图片,该图片会做出反应通过填充颜色(墨水)来触摸。
[..]
需要其祖先之一为材质小部件。
IconButton使用最有可能的ThemeData以及MaterialApp通常提供的其他内容。
您是否有理由不使用MaterialApp作为祖先?
如果使用Scaffold将堆栈(或通常是父级)包装起来,将不会出现此错误。
在这种情况下,如果您将IconButton与Material Widget封装在一起,我相信它将解决此问题:
Align(
alignment: Alignment.bottomCenter,
child: Material(
child: IconButton(icon: Icon(Icons.next_week), onPressed: () {})),
)