【发布时间】:2020-10-24 20:56:18
【问题描述】:
我真的不知道是什么导致了这个错误。我尝试删除函数 sunday() 因为我认为它可能会导致问题,但问题仍然存在。我也尝试删除 selectableDayPredicate 但问题仍然存在。我的小部件树有点长,抱歉读了很久。任何帮助表示赞赏!这是我的代码:
class TaskScreen extends StatefulWidget {
@override
_TaskScreenState createState() => _TaskScreenState();
}
class _TaskScreenState extends State<TaskScreen> {
DateTime sunday() {
_date = DateTime.now();
if (_date.weekday == 1) {
_date = DateTime.utc(_date.year, _date.month, _date.weekday + 6);
}
if (_date.weekday == 2) {
_date = DateTime.utc(_date.year, _date.month, _date.weekday + 5);
}
if (_date.weekday == 3) {
_date = DateTime.utc(_date.year, _date.month, _date.weekday + 4);
}
if (_date.weekday == 4) {
_date = DateTime.utc(_date.year, _date.month, _date.weekday + 3);
}
if (_date.weekday == 5) {
_date = DateTime.utc(_date.year, _date.month, _date.weekday + 2);
}
if (_date.weekday == 6) {
_date = DateTime.utc(_date.year, _date.month, _date.weekday + 1);
}
return _date;
}
final searchFocusNode = FocusNode();
DateTime _date = DateTime.now();
Future<Null> _selectDate(BuildContext context) async {
DateTime _datePicker = await showDatePicker(
context: context,
builder: (BuildContext context, Widget child) {
return Theme(
data: ThemeData.light().copyWith(
primaryColor: Color.fromRGBO(66, 87, 184, 1),
accentColor: Color.fromRGBO(56, 85, 144, 1),
colorScheme: ColorScheme.light(
primary: const Color.fromRGBO(66, 87, 184, 1)),
buttonTheme: ButtonThemeData(textTheme: ButtonTextTheme.primary),
),
child: child,
);
},
initialDate: sunday(),
firstDate: DateTime(1947),
lastDate: DateTime(2030),
selectableDayPredicate: (day) => day.weekday == 7 ? true : false,
);
if (_datePicker != null && _datePicker != _date) {
setState(() {
_date = _datePicker;
});
}
}
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.symmetric(vertical: SizeConfig.gridSizeHeight * 2),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
IconButton(
icon: Icon(Icons.search),
onPressed: () {},
iconSize: 40,
focusNode: searchFocusNode,
color: Color.fromRGBO(66, 87, 184, 1),
),
SizedBox(
width: SizeConfig.gridSizeWidth * 80,
child: TextFormField(
focusNode: searchFocusNode,
decoration: InputDecoration(
hintText: "Search",
),
onFieldSubmitted: (_) {},
),
),
],
),
SizedBox(
height: SizeConfig.gridSizeHeight * 0.1,
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
IconButton(
icon: Icon(
Icons.date_range,
color: Color.fromRGBO(66, 87, 184, 1),
),
iconSize: 40,
onPressed: () {
setState(() {
_selectDate(context);
});
}),
Column(
children: [
Container(
width: SizeConfig.gridSizeWidth * 80,
child: TextFormField(
initialValue: "${_date.year}-${_date.month}-${_date.day}",
cursorColor: Color.fromRGBO(66, 87, 184, 1),
readOnly: true,
onTap: () {
setState(() {
_selectDate(context);
});
},
decoration: InputDecoration(
labelText: "Date",
hintText: "${_date.year}-${_date.month}-${_date.day}",
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Color.fromRGBO(66, 87, 184, 1),
width: 2,
)),
),
),
),
],
),
],
),
SizedBox(
height: SizeConfig.gridSizeHeight * 2,
),
Padding(padding: const EdgeInsets.all(5)),
Expanded(
child: ListView.builder(
itemBuilder: (ctx, index) {
return Column(
children: [
Container(
padding: EdgeInsets.symmetric(
horizontal: SizeConfig.gridSizeWidth * 3,
vertical: SizeConfig.gridSizeHeight,
),
margin: EdgeInsets.symmetric(
horizontal: SizeConfig.gridSizeWidth * 7.5,
vertical: SizeConfig.gridSizeHeight * 2,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
children: [
Text(
"Project Name: ",
style: TextStyle(
color: Color.fromRGBO(66, 87, 184, 1),
),
),
Padding(
padding: const EdgeInsets.all(0.5),
),
Text("Telecom Egypt"),
],
),
Padding(
padding:
EdgeInsets.all(SizeConfig.gridSizeHeight * 0.5),
),
Row(
children: [
Text(
"Task Code: ",
style: TextStyle(
color: Color.fromRGBO(66, 87, 184, 1),
),
),
Text("#1235"),
],
),
Padding(
padding:
EdgeInsets.all(SizeConfig.gridSizeHeight * 0.5),
),
Row(
children: [
Text(
"Project Manager: ",
style: TextStyle(
color: Color.fromRGBO(66, 87, 184, 1),
),
),
Text("Khaled Khalifa"),
],
),
Padding(
padding:
EdgeInsets.all(SizeConfig.gridSizeHeight * 0.5),
),
Row(
children: [
Text(
"Time: ",
style: TextStyle(
color: Color.fromRGBO(66, 87, 184, 1),
),
),
Text("8 Hours"),
],
),
Padding(
padding:
EdgeInsets.all(SizeConfig.gridSizeHeight * 0.5),
),
Row(
children: [
Text(
"Priority: ",
style: TextStyle(
color: Color.fromRGBO(66, 87, 184, 1),
),
),
Text("1"),
],
),
SizedBox(
height: SizeConfig.gridSizeHeight,
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Icon(
Icons.remove_red_eye,
color: Color.fromRGBO(66, 87, 184, 1),
),
Padding(
padding:
EdgeInsets.all(SizeConfig.gridSizeWidth),
),
Icon(
Icons.edit,
color: Color.fromRGBO(66, 87, 184, 1),
),
Padding(
padding:
EdgeInsets.all(SizeConfig.gridSizeWidth),
),
Icon(
Icons.comment,
color: Color.fromRGBO(66, 87, 184, 1),
),
Padding(
padding:
EdgeInsets.all(SizeConfig.gridSizeWidth),
),
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Icon(
Icons.clear,
color: Colors.red,
),
Padding(
padding: EdgeInsets.all(
SizeConfig.gridSizeWidth),
),
Icon(
Icons.check,
color: Color.fromRGBO(56, 85, 144, 1),
),
],
))
],
),
],
),
decoration: BoxDecoration(
border: Border.all(
color: Color.fromRGBO(66, 87, 184, 1),
width: 1,
),
),
),
index == 2
? SizedBox(
height: SizeConfig.gridSizeHeight * 7,
)
: SizedBox()
],
);
},
itemCount: 3,
),
),
],
),
);
}
}
【问题讨论】:
-
如果您只能发布导致问题的代码部分,那么提供帮助会容易得多。旁注我注意到在
sunday()函数的代码顶部,您只处理第 1-6 天,而有 7 天,您是否错过了第 0 天或第 7 天,或者这可能是故意的?请设置调试器以捕获所有未处理的异常,运行代码并找出错误发生的确切位置以清理此问题。 -
@augustKimo 事情是我不知道哪个部分导致了错误。是的,第 7 天是故意留下的。当我运行代码时,它只会显示一个红屏。连调试的机会都没有。感谢您的回复!
-
您使用的是什么代码编辑器?您可以随时进行调试,使用调试器它会查明发生错误的代码行。