首页 > 建站教程 > APP开发,混合APP >  Flutter No Material widget found报错的解决办法正文

Flutter No Material widget found报错的解决办法

    我爱模板网利用showCupertinoModalPopup做了一个类似于js中的prompt效果的弹窗,里面有TextField,热更新时,提示下面的错误:
I/flutter (25517): No Material widget found.
I/flutter (25517): TextField widgets require a Material widget ancestor.
I/flutter (25517): In material design, most widgets are conceptually "printed" on a sheet of material. In Flutter's
I/flutter (25517): material library, that material is represented by the Material widget. It is the Material widget
I/flutter (25517): that renders ink splashes, for instance. Because of this, many material library widgets require that
I/flutter (25517): there be a Material widget in the tree above them.
I/flutter (25517): To introduce a Material widget, you can either directly include one, or use a widget that contains
I/flutter (25517): Material itself, such as a Card, Dialog, Drawer, or Scaffold.
I/flutter (25517): The specific widget that could not find a Material ancestor was:

    重点是:“No Material widget found.”和“TextField widgets require a Material widget ancestor.”,显而易见,TextField 必须包含在 Material 里面。而showDialog和showCupertinoModalPopup都在 Material 节点之外,除了 TextField,还有下面
AppBar
BottomAppBar
FloatingActionButton
Drawer
SnackBar
BottomSheet
BottomNavigationBar
ListTile
等等
    都使用了Material 风格的widget ,都需要Scaffold作为根布局,所以,将弹窗的widget外面套个Scaffold即可,如下:
return Scaffold(
    backgroundColor: Color.fromRGBO(0, 0, 0, 0),  //这个有必要设置下,因为Scaffold默认有不透明背景,影响弹窗显示,这里把背景设置为透明。
    body://这里放刚才的widget,就没问题了。
);