Skip to content

Instantly share code, notes, and snippets.

@nextdev1111
Created July 31, 2022 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nextdev1111/551b2d07770d6ba761ec07bd353e713d to your computer and use it in GitHub Desktop.
Save nextdev1111/551b2d07770d6ba761ec07bd353e713d to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:flutter_supabase_yt_1/models/models.dart';
import 'package:flutter_supabase_yt_1/utils/utils.dart';
class EditScreen extends StatefulWidget {
const EditScreen({
Key? key,
required this.todo,
}) : super(key: key);
// this is the arguments for the edit screen.
// Whenever we route to editscreen, we need to pass the todo.
// This is done because then only we can fetch the title and id of the todo.
// You can achieve this type of quality by using state mangagement also.
final Todo todo;
@override
State<EditScreen> createState() => _EditScreenState();
}
class _EditScreenState extends State<EditScreen> {
TextEditingController todoController = TextEditingController();
@override
void initState() {
// we are changing the value of todoController so that the title which user
// has set can be showed in the controller as default.
todoController.text = widget.todo.title;
super.initState();
}
@override
void dispose() {
todoController.dispose();
super.dispose();
}
// instance
SupabaseDataManager supabaseDataManager = SupabaseDataManager();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Create')),
body: Padding(
padding: const EdgeInsets.all(20),
child: Form(
child: Column(
children: [
TextFormField(
// controller
controller: todoController,
decoration: const InputDecoration(hintText: 'Todo'),
),
ElevatedButton(onPressed: _updateData, child: const Text('Update'))
],
)),
),
);
}
Future _updateData() async {
// we are changing the todoController title value so that we don't have to write the todo again.
// we are just using the todo from the arguments 👆, but we are changing the title to todoController text value.
Todo todo = widget.todo.copyWith(title: todoController.text);
PostgrestResponse<dynamic> res = await supabaseDataManager.updateTodo(todo);
if (res.error != null) {
// ignore: use_build_context_synchronously
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(res.error!.message)));
}
// ignore: use_build_context_synchronously
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Updated')));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment