我收到 3 种错误 1) RenderBox 未布局 2) 断言失败:第 1930 行 pos 12:'hasSize' 和 Null 检查运算符用于空值
I m getting 3types of errors1) RenderBox was not laid out2) Failed assertion: line 1930 pos 12: 'hasSize' and Null check operator used on a null value
我正在创建一个包含 TaskList 和 Task Tiles 的待办事项应用程序,通过创建任务列表的复选框和使用回调函数,我遇到了 3 种类型的错误。
- RenderBox 未布局 2) 断言失败:第 1930 行 pos 12:'hasSize' 3)空值检查运算符使用空值
任务列表class代码:
import 'package:flutter/material.dart';
import 'package:todoey_flutter/widgets/TaskTile.dart';
class TaskList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ListView(
children: <Widget>[
TaskTile(),
TaskTile(),
],
);
}
}
包含复选框和回调的 TaskTile 代码:
import 'package:flutter/material.dart';
class TaskTile extends StatefulWidget {
@override
_TaskTileState createState() => _TaskTileState();
}
class _TaskTileState extends State<TaskTile> {
bool isChecked= false;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
'This is my task.',
style: TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null,
),),
trailing: TaskCheckBox(isChecked, (bool checkboxState) {
setState(() {
isChecked = checkboxState;
});
},
),
);
}
}
class TaskCheckBox extends StatelessWidget {
TaskCheckBox (this.checkboxState, this.toggleCheckBoxState);
final bool checkboxState;
final Function toggleCheckBoxState;
@override
Widget build(BuildContext context) {
return Checkbox(
activeColor: Colors.blueAccent,
value: checkboxState,
onChanged: toggleCheckBoxState(),
);
}
}
我认为它已经解决了你的问题
ListView(
shrinkWrap: true,
physics: AlwaysScrollableScrollPhysics(),
children: <Widget>[
TaskTile(),
TaskTile(),
],
)
TaskTile class:
import 'package:flutter/material.dart';
class TaskTile extends StatefulWidget {
@override
_TaskTileState createState() => _TaskTileState();
}
class _TaskTileState extends State<TaskTile> {
bool isChecked= false;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
'This is my task.',
style: TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null,
),),
trailing: Checkbox(
activeColor: Colors.blueAccent,
value: isChecked,
onChanged: (value){
setState(() {
isChecked = value;
});
},
)
);
}
}
修复 1-> onChanged: ()=>toggleCheckBoxState()
第二个可能是装饰不能为空,所以通过 TextDecoration(),空构造函数。
和 3rds 如果 listview 具有约束父项,则将 listview 包装为 Expanded
或 shrinkWrap
true.
我正在创建一个包含 TaskList 和 Task Tiles 的待办事项应用程序,通过创建任务列表的复选框和使用回调函数,我遇到了 3 种类型的错误。
- RenderBox 未布局 2) 断言失败:第 1930 行 pos 12:'hasSize' 3)空值检查运算符使用空值
任务列表class代码:
import 'package:flutter/material.dart';
import 'package:todoey_flutter/widgets/TaskTile.dart';
class TaskList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ListView(
children: <Widget>[
TaskTile(),
TaskTile(),
],
);
}
}
包含复选框和回调的 TaskTile 代码:
import 'package:flutter/material.dart';
class TaskTile extends StatefulWidget {
@override
_TaskTileState createState() => _TaskTileState();
}
class _TaskTileState extends State<TaskTile> {
bool isChecked= false;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
'This is my task.',
style: TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null,
),),
trailing: TaskCheckBox(isChecked, (bool checkboxState) {
setState(() {
isChecked = checkboxState;
});
},
),
);
}
}
class TaskCheckBox extends StatelessWidget {
TaskCheckBox (this.checkboxState, this.toggleCheckBoxState);
final bool checkboxState;
final Function toggleCheckBoxState;
@override
Widget build(BuildContext context) {
return Checkbox(
activeColor: Colors.blueAccent,
value: checkboxState,
onChanged: toggleCheckBoxState(),
);
}
}
我认为它已经解决了你的问题
ListView(
shrinkWrap: true,
physics: AlwaysScrollableScrollPhysics(),
children: <Widget>[
TaskTile(),
TaskTile(),
],
)
TaskTile class:
import 'package:flutter/material.dart';
class TaskTile extends StatefulWidget {
@override
_TaskTileState createState() => _TaskTileState();
}
class _TaskTileState extends State<TaskTile> {
bool isChecked= false;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
'This is my task.',
style: TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null,
),),
trailing: Checkbox(
activeColor: Colors.blueAccent,
value: isChecked,
onChanged: (value){
setState(() {
isChecked = value;
});
},
)
);
}
}
修复 1-> onChanged: ()=>toggleCheckBoxState()
第二个可能是装饰不能为空,所以通过 TextDecoration(),空构造函数。
和 3rds 如果 listview 具有约束父项,则将 listview 包装为 Expanded
或 shrinkWrap
true.