Last active
October 27, 2020 13:09
-
-
Save tieorange/ffdad0c7697b9d0a4ae02d15cb146383 to your computer and use it in GitHub Desktop.
TodoListApp flutter
This file contains hidden or 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 'dart:math'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
// DTO | |
class TodoTask { | |
final String name; | |
final DateTime dateCreatedAt = DateTime.now(); | |
final String description = | |
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen"; | |
static int randomWidth() => 500 + Random().nextInt(600 - 500); | |
static String imageUrl() { | |
var random = randomWidth(); | |
print("Width = $random"); | |
return "https://source.unsplash.com/random/${random}x200"; | |
} | |
TodoTask(this.name); | |
} | |
// Random image url: https://source.unsplash.com/random/600x400 | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'TODO', | |
home: TasksListPage(), | |
); | |
} | |
} | |
class TasksListPage extends StatefulWidget { | |
@override | |
_TasksListPageState createState() => _TasksListPageState(); | |
} | |
class _TasksListPageState extends State<TasksListPage> { | |
List<TodoTask> todoList = [ | |
TodoTask("Buy milk"), | |
]; | |
final taskNameController = TextEditingController(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("Todo List"), | |
), | |
body: Column( | |
children: [ | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: TextField( | |
decoration: | |
InputDecoration.collapsed(hintText: "Enter task name"), | |
controller: taskNameController, | |
onSubmitted: (value) => onAddNewTaskClicked(), | |
), | |
), | |
Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: buildListView(), | |
), | |
], | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: onAddNewTaskClicked, | |
child: Text("Add"), | |
), | |
); | |
} | |
void onAddNewTaskClicked() { | |
setState(() => todoList.add(TodoTask(taskNameController.text))); | |
taskNameController.clear(); | |
} | |
// Tasks list | |
Widget buildListView() { | |
final List<Widget> listOfWidgetConvertedFromTodoTasks = | |
todoList.map((TodoTask todoTask) { | |
return buildListItem(todoTask); | |
}).toList(); | |
return Container( | |
height: 300, | |
child: ListView(children: listOfWidgetConvertedFromTodoTasks), | |
); | |
} | |
// Todo list item | |
Widget buildListItem(TodoTask todoTask) => Card( | |
child: Column(children: [ | |
Image.network(TodoTask.imageUrl()), | |
Padding( | |
padding: const EdgeInsets.all(16), | |
child: Text("Task: ${todoTask.name}"), | |
) | |
]), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment