我的代码中出现以下错误。
未为类“ String”定义吸气剂“ todoText”。
尝试导入定义“ todoText”的库,将名称更正为现有吸气剂的名称,或者定义一个名为“ todoText”的吸气剂或字段。dart(undefined_getter)
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
void main() => runApp(new ToDoApp());
class ToDoApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'To Do Application',
theme: new ThemeData(
primarySwatch: Colors.teal,
),
home: new ToDoList(),
);
}
}
class ListItem{
bool _todoCheck;
ListItem(this._todoCheck);
}
class _strikeThrough extends StatelessWidget{
final String todoText;
final bool todoCheck;
_strikeThrough(this.todoText, this.todoCheck) : super();
Widget _widget(){
if(todoCheck){
return Text(
todoText,
style: TextStyle(
decoration: TextDecoration.lineThrough,
fontStyle: FontStyle.italic,
fontSize: 22.0,
color: Colors.red[200],
),
);
}
else{
return Text(
todoText,
style: TextStyle(
fontSize: 22.0
),
);
}
}
@override
Widget build(BuildContext context){
return _widget();
}
}
class ToDoList extends StatefulWidget {
@override
createState() => new _TodoListState();
}
class _TodoListState extends State<ToDoList> {
var textController = TextEditingController();
var popUpTextController = TextEditingController();
List<String> _todoItems = [];
List<ListItem> WidgetList =[];
void _addToDoItem(String task){
if(task.length > 0){
setState(()=> _todoItems.add(task));
}
}
void _removeToDoItem(int index){
setState(() => _todoItems.removeAt(index));
}
void _promptRemoveToDoItem(int index){
showDialog(context: context,
builder: (BuildContext context){
return new AlertDialog(
title: new Text('Mark "${_todoItems[index]}" as Completed?'+' This will remove this Item'),
actions: <Widget>[
new FlatButton(
child: new Text('CANCEL'),
onPressed: () => Navigator.of(context).pop()
),
new FlatButton(
child: new Text('MARK AS DONE'),
onPressed: () {
_removeToDoItem(index);
Navigator.of(context).pop();
Fluttertoast.showToast(
msg: 'ToDo Task Deleted!'
);
}
),
]
);
}
);
}
Widget _buildToDoList(BuildContext context){
return Column(
children: <Widget>[
new Flexible(
child: new ListView.builder(
itemBuilder: (context,index){
return _buildToDoItem(_todoItems[index], index);
},
),
),
new Expanded(
child: ReorderableListView(
children: <Widget>[
for(final widget in _todoItems)
for(final widget1 in WidgetList)
GestureDetector(
key: Key(widget.todoText),
child: Dismissible(
key: Key(widget.todoText),
child: CheckboxListTile(
value: widget1._todoCheck,
title: _strikeThrough(widget.todoText,widget1._todoCheck),
onChanged: (checkValue){
setState(() {
if(!checkValue){
widget1._todoCheck = false;
}
else{
widget1._todoCheck = true;
}
});
}),
background: Container(
child: Icon(Icons.delete),
alignment: Alignment.centerRight,
color: Colors.orange[300],
),
confirmDismiss: (dismissDirection){
return showDialog(
context: context,
barrierDismissible: true,
builder: (BuildContext context){
return AlertDialog(
title: Text("Delete Todo?"),
actions: <Widget>[
FlatButton(
child: Text("OK"),
onPressed: (){
Navigator.of(context).pop(true);
},
),
FlatButton(
child: Text("Cancel"),
onPressed: (){
Navigator.of(context).pop(true);
},),
],
);
});
},
direction: DismissDirection.endToStart,
movementDuration: const Duration(milliseconds: 200),
onDismissed: (dismissedDirection){
WidgetList.remove(widget);
Fluttertoast.showToast(
msg: "Todo Deleted!"
);
},
),
onDoubleTap: (){
popUpTextController.text = widget.todoText;
showDialog(
context: context,
barrierDismissible: true,
builder: (BuildContext context){
return AlertDialog(
title: Text("Edit ToDo"),
content: TextFormField(
controller: popUpTextController,
),
actions: <Widget>[
FlatButton(
child: Text("OK"),
onPressed: (){
setState(() {
widget.todoText=popUpTextController.text;
});
Navigator.of(context).pop(true);
},
),
FlatButton(
child: Text("Cancel"),
onPressed: (){
Navigator.of(context).pop(false);
},
)
],
);
}
);
},
)
],
onReorder: (oldIndex, newIndex){
setState(() {
if(oldIndex < newIndex){
newIndex -= 1;
}
var replaceWiget = WidgetList.removeAt(oldIndex);
WidgetList.insert(newIndex, replaceWiget);
});
},)
)
],
);
}
Widget _buildToDoItem(String todoText, int index){
return new ListTile(
title: new Text(todoText),
onLongPress: () => _promptRemoveToDoItem(index),
);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('To Do List')
),
body:_buildToDoList(context),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: new FloatingActionButton(
focusElevation: 7,
onPressed:_pushAddToDoScreen,
tooltip: 'Add Task',
child: new Icon(Icons.add)
),
drawer: Drawer(
child: ListView(
padding: const EdgeInsets.all(0),
children: <Widget>[
DrawerHeader(
child: Text('To Do App Drawer Header'),
decoration: BoxDecoration(
color: Colors.teal,
)
),
ListTile(
title: Text('Add Items'),
onTap: _pushAddToDoScreen,
),
ListTile(
title: Text('Remove Items'),
subtitle: Text('Long Press on any item to remove it'),
onTap: (){
Navigator.pop(context);
},
),
ListTile(
title: Text('Check Status of Items'),
subtitle: Text('If the item is in List, then it is Pending, else long press to mark it done'),
onTap: (){
Navigator.pop(context);
},
),
],
)
),
bottomNavigationBar: BottomAppBar(
shape: CircularNotchedRectangle(),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: Icon(Icons.settings),
onPressed: () {},
),
IconButton(
icon: Icon(Icons.more_vert),
onPressed: () {},
)
]
)
)
);
}
void _pushAddToDoScreen(){
Navigator.of(context).push(
new MaterialPageRoute(
builder: (context){
return new Scaffold(
appBar: new AppBar(
title: new Text('Add a new task')
),
body: new TextField(
autofocus: true,
onSubmitted: (val){
_addToDoItem(val);
Navigator.pop(context);
},
decoration: new InputDecoration(
hintText: 'Enter Tasks....',
contentPadding: const EdgeInsets.all(16.0)
),
)
);
}
)
);
}
}