Created
August 28, 2019 07:03
-
-
Save bjornbjorn/8e9e38a5a719335e1a46d01164be1eb4 to your computer and use it in GitHub Desktop.
Todo app main.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Todo App', | |
theme: ThemeData( | |
primarySwatch: Colors.red, | |
), | |
home: TodoWidget(), | |
); | |
} | |
} | |
// My todo model | |
class TodoItem { | |
TodoItem(this.done, this.text); | |
final bool done; | |
final String text; | |
} | |
class TodoWidget extends StatefulWidget { | |
@override | |
_TodoWidgetState createState() => _TodoWidgetState(); | |
} | |
class _TodoWidgetState extends State<TodoWidget> { | |
List<TodoItem> todos = []; | |
int currentCount = 0; | |
Future<String> _showTodoDialog() async { | |
String todoText; | |
return showDialog<String>( | |
context: context, | |
barrierDismissible: false, // user must tap button! | |
builder: (BuildContext context) { | |
return AlertDialog( | |
title: Text('Add todo'), | |
content: SingleChildScrollView( | |
child: ListBody( | |
children: <Widget>[ | |
TextField(onChanged: (value) { | |
todoText = value; | |
print(value); | |
}), | |
], | |
), | |
), | |
actions: <Widget>[ | |
FlatButton( | |
child: Text('Add'), | |
onPressed: () { | |
Navigator.of(context).pop(todoText); | |
}, | |
), | |
], | |
); | |
}, | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text('Todo App')), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () async { | |
String todoText = await _showTodoDialog(); | |
setState(() { | |
todos.add(new TodoItem(false, todoText)); | |
}); | |
}, | |
child: Icon(Icons.add), | |
), | |
body: Container( | |
child: ListView.builder( | |
padding: const EdgeInsets.all(8.0), | |
itemCount: todos.length, | |
itemBuilder: (BuildContext context, int index) { | |
return TodoItemWidget( | |
item: todos[index], | |
onChangedCallback: (value) { | |
setState(() { | |
todos[index] = new TodoItem(value, todos[index].text); | |
}); | |
}); | |
}), | |
), | |
); | |
} | |
} | |
class TodoItemWidget extends StatelessWidget { | |
final TodoItem item; | |
final Function(bool) onChangedCallback; | |
const TodoItemWidget({Key key, this.item, this.onChangedCallback}) | |
: super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return CheckboxListTile( | |
key: Key(item.text), | |
title: Text(item.text, | |
style: | |
TextStyle(color: item.done ? Colors.lightGreen : Colors.black)), | |
value: item.done, | |
onChanged: onChangedCallback); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment