Created
November 9, 2019 14:32
-
-
Save Norbert515/18d91cb03b232d755b1088d62333b75b to your computer and use it in GitHub Desktop.
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/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/foundation.dart' | |
show debugDefaultTargetPlatformOverride; | |
class TodoModel { | |
String title; | |
String date; | |
bool done; | |
TodoModel({ | |
@required this.title, | |
@required this.date, | |
@required this.done | |
}); | |
} | |
void main() { | |
debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia; | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: new ThemeData( | |
primarySwatch: Colors.orange, | |
), | |
home: new MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
List<TodoModel> models = [ | |
TodoModel( | |
done: false, | |
title: "Some title", | |
date: "Some date" | |
), | |
TodoModel( | |
done: false, | |
title: "Some title", | |
date: "Some date" | |
), | |
TodoModel( | |
done: true, | |
title: "other stuff", | |
date: "Some date" | |
), | |
TodoModel( | |
done: false, | |
title: "Some title", | |
date: "Some date" | |
), | |
]; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("GDG-Devfest super ToDo App"), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () async { | |
TodoModel model = await showDialog<TodoModel>(context: context, | |
builder: (context) { | |
return CreateTodoItemDialog(); | |
}, | |
); | |
if(model != null) { | |
setState(() { | |
models.add(model); | |
}); | |
} | |
}, | |
child: Icon(Icons.add,color: Colors.white,), | |
), | |
body: ListView.builder( | |
itemCount: models.length, | |
itemBuilder: (context, index) { | |
return TodoItem( | |
todoModel: models[index], | |
onChanged: (done) { | |
setState(() { | |
models[index].done = done; | |
}); | |
}, | |
); | |
}, | |
), | |
); | |
} | |
} | |
class TodoItem extends StatelessWidget { | |
final TodoModel todoModel; | |
final ValueChanged<bool> onChanged; | |
const TodoItem({ | |
Key key, | |
@required this.todoModel, | |
@required this.onChanged | |
}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return ListTile( | |
title: Text(todoModel.title, | |
style: TextStyle( | |
color: todoModel.done? Colors.red : Colors.black, | |
decoration: todoModel.done? TextDecoration.lineThrough : null | |
), | |
), | |
subtitle: Text(todoModel.date), | |
trailing: Checkbox( | |
value: todoModel.done, | |
onChanged: onChanged, | |
), | |
); | |
} | |
} | |
class CreateTodoItemDialog extends StatelessWidget { | |
final TextEditingController controller = TextEditingController(); | |
@override | |
Widget build(BuildContext context) { | |
return SimpleDialog( | |
title: Text("Create new item"), | |
contentPadding: const EdgeInsets.all(16), | |
children: <Widget>[ | |
TextField( | |
controller: controller, | |
decoration: InputDecoration( | |
hintText: "Todo task item" | |
), | |
), | |
SizedBox(height: 16,), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.end, | |
children: <Widget>[ | |
MaterialButton( | |
onPressed: () { | |
Navigator.of(context).pop(null); | |
}, | |
child: Text("Cancle"), | |
), | |
RaisedButton( | |
color: Theme.of(context).primaryColor, | |
onPressed: () { | |
Navigator.of(context).pop(TodoModel( | |
done: false, | |
date: DateTime.now().toIso8601String(), | |
title: controller.text, | |
)); | |
}, | |
child: Text("Create Task"), | |
), | |
], | |
), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment