因此,在几个小时没有工作后,我加载了我的 flutter 项目,当我尝试单击一个应该让我进入特定页面的按钮时,整个应用程序冻结了。上次我运行它时,它工作正常,我没有改变任何东西。我已经重新启动了该程序,但我的电脑仍然死机。所有其他根植于页面的按钮都可以工作,所以我认为这是该页面代码的问题。这是该页面的完整代码:
import 'package:flutter/material.dart';
import 'package:workwise2/classes/taskClass.dart';
import 'package:workwise2/config.dart';
class TodoTasks extends StatefulWidget {
@override
_TodoTasksState createState() => _TodoTasksState();
}
class _TodoTasksState extends State<TodoTasks> {
Settings settings = Settings();
BottomNavBarConfig bottomNavBarConfig = BottomNavBarConfig();
List <Task> tasks = [
Task(title: "Maths HW", description: "Complete Q2, Q6 and Q7 with GPCs on Q8", subject: "maths", day: 12, month: 4, year: 2021),
Task(title: "English test", description: "Revise quotes for anger", subject: "english", day: 13, month: 4, year: 2021),
];
@override
Widget build(BuildContext context) {
int _currentIndex = 0;
String _requestedPage;
return Scaffold(
appBar: AppBar(
title: Text(
"Tasks",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30.0
),
),
backgroundColor: Colors.indigoAccent[700],
leading: Icon(Icons.check_box_rounded, size: 40.0,),
),
body: AnimatedContainer(
duration: Duration(milliseconds: 1700),
child: ListView.builder(
itemBuilder: (context, index) {
Color _checkButtonColour;
double _checkButtonElevation;
IconData _bookmark = Icons.bookmark_border;
if (tasks[index].state == "todo") {
_checkButtonColour = Colors.red[600];
_checkButtonElevation = 0.0;
}
else if (tasks[index].state == "doing") {
_checkButtonColour = Colors.amber;
_checkButtonElevation = 5.0;
}
else if (tasks[index].state == "done") {
_checkButtonColour = Colors.green;
_checkButtonElevation = 10.0;
}
while (tasks[index].favourited == true) {
_bookmark = Icons.bookmark;
}
while (tasks[index].favourited == false) {
_bookmark = Icons.bookmark_border;
}
return Padding(
padding: EdgeInsets.symmetric(horizontal: 8.0, vertical: 3.0),
child: Card(
elevation: 10.0,
child: ListTile(
contentPadding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 5.0),
onTap: () {},
leading: IconButton(
onPressed: () {
tasks[index].favourited = !tasks[index].favourited;
print(tasks[index].favourited);
},
icon: Icon(_bookmark),
iconSize: 35.0,
color: settings.subjectColours[tasks[index].subject]
),
title: Text(tasks[index].title, maxLines: 1, overflow: TextOverflow.fade,),
subtitle: Text(tasks[index].description, maxLines: 1, overflow: TextOverflow.ellipsis,),
trailing: AnimatedContainer(
height: 80.0,
width: 100.0,
duration: Duration(milliseconds: 1700),
child: Row(
children: <Widget> [
SizedBox(
width: 40.0,
height: 40.0,
child: MaterialButton(
elevation: _checkButtonElevation,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(6.0)),
onPressed: () {
if (tasks[index].state == "todo") {
setState(() {
tasks[index].state = "doing";
});
} else if (tasks[index].state == "doing") {
setState(() {
tasks[index].state = "done";
});
} else if (tasks[index].state == "done") {
setState(() {
tasks[index].state = "todo";
});
}
},
padding: EdgeInsets.all(2.0),
color: _checkButtonColour,
),
),
IconButton(
onPressed: () {},
icon: Icon(Icons.more_vert),
)
],
),
),
),
),
);
},
itemCount: tasks.length,
)
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
backgroundColor: Colors.indigoAccent[700],
fixedColor: Colors.white,
unselectedFontSize: bottomNavBarConfig.unactiveTextSize,
unselectedIconTheme: IconThemeData(
size: bottomNavBarConfig.unactiveIconSize
),
selectedFontSize: bottomNavBarConfig.activeTextSize,
selectedIconTheme: IconThemeData(
size: bottomNavBarConfig.activeIconSize
),
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.check_box_rounded),
label: "Tasks"
),
BottomNavigationBarItem(
icon: Icon(Icons.lightbulb),
label: "Learn"
),
BottomNavigationBarItem(
icon: Icon(Icons.home_rounded),
label: "Home"
),
BottomNavigationBarItem(
icon: Icon(Icons.library_books_rounded),
label: "Notes"
),
],
onTap: (index) {
if (index == 0) {
_requestedPage = "/todotasks";
}
else if (index == 2) {
_requestedPage = "/home";
}
else if (index == 3) {
_requestedPage = "/notes";
}
else if (index == 1) {
_requestedPage = "/sets";
}
setState(() {
_currentIndex = index;
});
Navigator.popAndPushNamed(context, _requestedPage);
},
)
);
}
}
我不知道出了什么问题。请帮助我,我不知道该怎么办。评论了解更多详情。
这是由于我的代码中的两个 while 语句引起的。他们基本上互相打电话并冻结了应用程序。
使用
Navigator.of(context).pushNamedUntil(_requestedPage);
如果您使用
ListView.builder
小部件且 itemCount
是可选值。
看看这个:
ListView.builder(
itemCount: items?.length ?? 0,
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (BuildContext context, int index) {
return Text("Hello");
}
)