android - 如何将应用栏添加到 StatelessWidget?
问题描述
当我尝试添加此应用栏时,它给了我一个错误。错误是:断言失败:第 4345 行 pos 14:'owner._debugCurrentBuildTarget == this':不正确。
appBar: new AppBar(
title: new Text("Appbar"),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 20.0),
child: Icon(Icons.search),
),
],
backgroundColor: Colors.grey,
),
如何将上述应用栏添加到以下代码中?
class StatisticsPage extends StatelessWidget{
@override
Widget build(BuildContext context){
return MaterialApp(
debugShowCheckedModeBanner: false,
title: "Welcome to Flutter",
home: Statistics(),
);
}
}
class Statistics extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Material(
child:new Container(
padding: const EdgeInsets.all(30.0),
color: Colors.white,
...
解决方案
该appBar
属性位于Scaffold()
小部件中。您正在尝试调用appBar
不起作用的构建。
class Statistics extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Material(
child: Scaffold(
appBar: AppBar(
title: Text("Appbar"),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 20.0),
child: Icon(Icons.search),
),
],
backgroundColor: Colors.grey,
),
body: Container(
padding: const EdgeInsets.all(30.0),
color: Colors.white,
),
));
}
}
您似乎对 Flutter 很陌生,或者您只是从某个旧存储库中复制粘贴代码。
new
自 Dart 2.0 起,不再需要应用该关键字。先学 Dart,再学 Flutter,可以从 Youtube 教程开始。
推荐阅读
- three.js - 在几何之上的 Three.js 线框
- java - 使用 DBUnit 时如何防止数据被删除
- python - 将字典转换为嵌套字典列表
- neo4j - Neo4j 和代谢途径
- dns - 域所有权标准 (.eu)
- node.js - 如何使用 mongodb ref 外键?猫鼬和 NodeJS
- postgresql - 在 PostgreSQL 中使用具有数组数据类型的 JOIN 表的 SELECT 语句
- elasticsearch - Kubernetes - 不能卷曲到服务,但我可以卷曲到 Pod
- c# - C# - 如果 DataGridView 包含搜索的单词,则将这些行导出到新的 DataGridView
- r - shinyloadtest 与多个不同的用户