Skip to content

Commit

Permalink
feat: add support for editing notes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Nov 27, 2023
1 parent 2e0ae35 commit 46ff98a
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/core/di_container.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:get_it/get_it.dart';
import 'package:noteapp/bloc/fetch_notes/fetch_notes_bloc.dart';
import 'package:noteapp/bloc/update_note/update_note_bloc.dart';

import '../bloc/save_new_note/save_new_note_bloc.dart';
import '../data/implementation/local_notes_repository.dart';
Expand All @@ -19,4 +20,8 @@ void setupLocator() {
getIt.registerFactory(
() => FetchNotesBloc(repository: getIt()),
);

getIt.registerFactory(
() => UpdateNoteBloc(repository: getIt()),
);
}
7 changes: 7 additions & 0 deletions lib/core/go_router.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:go_router/go_router.dart';
import 'package:noteapp/ui/screens/edit_note.dart';
import 'package:noteapp/ui/screens/note_view.dart';

import '../models/note.dart';
Expand All @@ -23,6 +24,12 @@ GoRouter appRouter = GoRouter(
GoRoute(
path: '/NoteView',
builder: (context, state) => NoteView(note: state.extra as Note),
routes: [
GoRoute(
path: 'EditNote',
builder: (context, state) => EditNote(state.extra as Note),
),
],
),
],
);
143 changes: 143 additions & 0 deletions lib/ui/screens/edit_note.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get_it/get_it.dart';
import 'package:go_router/go_router.dart';

import '../../bloc/update_note/update_note_bloc.dart';
import '../../models/note.dart';
import '../widgets/go_back_button.dart';
import '../widgets/square_icon_button.dart';

class EditNote extends StatefulWidget {
final Note oldNote;
const EditNote(
this.oldNote, {
super.key,
});

@override
State<EditNote> createState() => _EditNoteState();
}

class _EditNoteState extends State<EditNote> {
late final UpdateNoteBloc bloc;
late final TextEditingController titleController;
late final TextEditingController contentController;

@override
void initState() {
super.initState();
bloc = GetIt.I.get<UpdateNoteBloc>();
titleController = TextEditingController(text: widget.oldNote.title);
contentController = TextEditingController(text: widget.oldNote.content);
}

@override
Widget build(BuildContext context) {
return BlocProvider<UpdateNoteBloc>(
create: (context) => bloc,
child: BlocListener<UpdateNoteBloc, UpdateNoteState>(
listener: (context, state) {
if (state is UpdateNoteInSuccess) context.pop();
},
child: Scaffold(
appBar: AppBar(
leadingWidth: 75,
leading: const GoBackButton(),
actions: [
SquareIconButton(
icon: const Icon(Icons.visibility_outlined),
onPressed: () => context.push(
'/NoteView',
extra: Note(
title: titleController.text,
content: contentController.text,
),
),
),
const SizedBox(width: 25),
SaveButton(
titleController: titleController,
noteController: contentController,
bloc: bloc,
oldNote: widget.oldNote,
),
const SizedBox(width: 25),
],
),
body: Container(
margin: const EdgeInsets.symmetric(horizontal: 32),
child: SingleChildScrollView(
child: Column(
children: <Widget>[
const SizedBox(height: 40),
TextField(
controller: titleController,
decoration: const InputDecoration(
hintText: 'Title',
border: InputBorder.none,
),
maxLines: null,
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 40),
TextField(
controller: contentController,
decoration: const InputDecoration(
hintText: 'Note',
border: InputBorder.none,
),
style: Theme.of(context).textTheme.bodyLarge,
maxLines: null,
),
],
),
),
),
),
),
);
}
}

class SaveButton extends StatelessWidget {
const SaveButton({
super.key,
required this.titleController,
required this.noteController,
required this.bloc,
required this.oldNote,
});

final Note oldNote;
final TextEditingController titleController;
final TextEditingController noteController;
final UpdateNoteBloc bloc;

@override
Widget build(BuildContext context) {
return BlocBuilder<UpdateNoteBloc, UpdateNoteState>(
builder: (context, state) {
if (state is UpdateNoteInProgress) {
return const CircularProgressIndicator.adaptive();
}
return SquareIconButton(
icon: const Icon(Icons.save_outlined),
onPressed: () {
if (titleController.text.trim().isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('The title cannot be empty')),
);
return;
}
final Note newNote = Note(
title: titleController.text,
content: noteController.text,
);
bloc.add(UpdateNote(oldNote: oldNote, newNote: newNote));
},
);
},
);
}
}
2 changes: 1 addition & 1 deletion lib/ui/screens/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HomeScreen extends StatefulWidget {
State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
class _HomeScreenState extends State<HomeScreen> with RouteAware {
late final FetchNotesBloc bloc;
late final GlobalKey<RefreshIndicatorState> refreshIndicatorKey;

Expand Down
14 changes: 14 additions & 0 deletions lib/ui/screens/note_view.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:noteapp/ui/widgets/square_icon_button.dart';

import '../../models/note.dart';
import '../widgets/go_back_button.dart';
Expand All @@ -17,6 +19,18 @@ class NoteView extends StatelessWidget {
appBar: AppBar(
leadingWidth: 75,
leading: const GoBackButton(),
actions: [
SquareIconButton(
icon: const Icon(Icons.edit),
onPressed: () => context
.push(
'/NoteView/EditNote',
extra: note,
)
.then((value) => context.pop()),
),
const SizedBox(width: 25),
],
),
body: SingleChildScrollView(
child: Container(
Expand Down

0 comments on commit 46ff98a

Please sign in to comment.