Created
March 21, 2022 18:45
-
-
Save romanejaquez/6e99fe72a180f67d93317e78b07750b4 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/material.dart'; | |
import 'package:provider/provider.dart'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp( | |
ChangeNotifierProvider( | |
create: (_) => Tasks(), | |
child: MyApp() | |
) | |
); | |
} | |
class Task { | |
String title; | |
String description; | |
bool done; | |
Task({ this.title = '', this.description = '', this.done = false }); | |
} | |
class Tasks extends ChangeNotifier { | |
List<Task> tasks = [ | |
Task(title: 'First one', description: 'First Description'), | |
Task(title: 'Second one', description: 'Second Description'), | |
Task(title: 'Third one', description: 'Third Description'), | |
]; | |
void deleteTask(Function callback, {required int index}) { | |
Future.delayed(const Duration(seconds: 2), () { | |
tasks.removeAt(index); | |
callback(); | |
notifyListeners(); | |
}); | |
} | |
void save({ index: int, newTitle: String, newDescription: String }) { | |
var newTask = Task(title: newTitle, description: newDescription); | |
tasks.insert(index, newTask); | |
notifyListeners(); | |
} | |
void changeDone({ index: int, isDone: bool }) { | |
tasks[index].done = isDone; | |
} | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: Rivers(), | |
), | |
), | |
); | |
} | |
} | |
class Rivers extends StatelessWidget { | |
const Rivers({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
key: Utils.scaffoldKey, | |
appBar: AppBar( | |
title: const Text('Wave a Task'), | |
), | |
body: Column( | |
children: const [ | |
CircleAvatar( | |
radius: 40, | |
backgroundImage: NetworkImage('https://media.gettyimages.com/vectors/blue-waves-set-vector-id1223223152?s=612x612'), | |
), | |
RiverBody(), | |
], | |
), | |
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
Navigator.of(context).push( | |
MaterialPageRoute( | |
builder: (context) => TaskPage(), | |
), | |
); | |
}, | |
child: const Icon(Icons.add), | |
), | |
); | |
} | |
} | |
class TaskPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold(); // todo | |
} | |
} | |
class RiverBody extends StatelessWidget { | |
const RiverBody({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Expanded( | |
child: ListView( | |
padding: const EdgeInsets.only(top: 10.0), | |
children: List.generate( | |
Provider.of<Tasks>(context).tasks.length, | |
(index) => ListTileCustom( | |
index: index, | |
), | |
), | |
), | |
); | |
} | |
} | |
class Utils { | |
static GlobalKey scaffoldKey = GlobalKey(); | |
static void showDeletingAlert() { | |
showDialog( | |
useRootNavigator: true, | |
context: Utils.scaffoldKey.currentContext!, | |
builder: (ctx) { | |
return AlertDialog( | |
content: Container( | |
padding: const EdgeInsets.all(20), | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: const [ | |
CircularProgressIndicator(), | |
SizedBox(height: 20), | |
Text('Deleting Task...') | |
] | |
) | |
) | |
); | |
}); | |
} | |
} | |
class ListTileCustom extends StatelessWidget { | |
const ListTileCustom({Key? key, required this.index}) : super(key: key); | |
final int index; | |
@override | |
Widget build(BuildContext context) { | |
Task currentTask = Provider.of<Tasks>(context).tasks[index]; | |
return CheckboxListTile( | |
subtitle: Align( | |
alignment: Alignment.centerLeft, | |
child: TextButton( | |
onPressed: () { | |
showDialog( | |
context: context, | |
builder: (context) { | |
String title = Provider.of<Tasks>(context).tasks[index].title; | |
String description = | |
Provider.of<Tasks>(context).tasks[index].description; | |
TextEditingController titleController = | |
TextEditingController(text: title); | |
TextEditingController descriptionController = | |
TextEditingController(text: description); | |
return AlertDialog( | |
title: TextField( | |
controller: titleController, | |
), | |
content: TextField( | |
controller: descriptionController, | |
), | |
actions: [ | |
TextButton( | |
style: TextButton.styleFrom( | |
primary: Color.fromARGB(255, 175, 158, 158), | |
backgroundColor: Colors.red, | |
), | |
onPressed: () { | |
Provider.of<Tasks>(context, listen: false) | |
.deleteTask(() { | |
Navigator.of(Utils.scaffoldKey.currentContext!, rootNavigator: true).pop('dialog'); | |
}, index: index); | |
Navigator.of(context).pop(); | |
Utils.showDeletingAlert(); | |
}, | |
child: const Text('Delete'), | |
), | |
TextButton( | |
onPressed: () { | |
Provider.of<Tasks>(context, listen: false).save( | |
index: index, | |
newTitle: titleController.text, | |
newDescription: descriptionController.text); | |
Navigator.of(context).pop(); | |
}, | |
child: const Text('Save'), | |
), | |
], | |
); | |
}); | |
}, | |
child: const Text('View'), | |
), | |
), | |
onChanged: (newbool) { | |
Provider.of<Tasks>(context, listen: false) | |
.changeDone(index: index, isDone: newbool!); | |
}, | |
value: currentTask.done, | |
title: Text(currentTask.title), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment