Skip to content

Commit

Permalink
Merge pull request #5 from achintya-7/feat/markdown-support
Browse files Browse the repository at this point in the history
[feat] added basic markdown support
  • Loading branch information
achintya-7 authored Dec 23, 2023
2 parents b450dce + 0d27bdc commit 4602901
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 149 deletions.
2 changes: 0 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ void main() {
class MyApp extends StatelessWidget {
const MyApp({super.key});

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,

home: const NotePageState(),
theme: Mytheme.darkTheme(context),
);
Expand Down
38 changes: 17 additions & 21 deletions lib/pages/add_edit_note_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: prefer_const_constructors_in_immutables, prefer_const_constructors
// ignore_for_file: prefer_const_constructors_in_immutables, prefer_const_constructors, use_build_context_synchronously

import 'package:flutter/material.dart';
import 'package:notesapp/db/notes_database.dart';
Expand All @@ -20,6 +20,7 @@ class _AddEditNotePageState extends State<AddEditNotePage> {
late bool isImportant;
late String title;
late String description;
bool isPreview = false;

@override
void initState() {
Expand All @@ -43,11 +44,11 @@ class _AddEditNotePageState extends State<AddEditNotePage> {
isImportant: isImportant,
title: title,
description: description,
onChangedImportant: (isImportant) =>
setState(() => this.isImportant = isImportant),
isPreview: isPreview,
onChangedImportant: (isImportant) => setState(() => this.isImportant = isImportant),
onChangedTitle: (title) => setState(() => this.title = title),
onChangedDescription: (description) =>
setState(() => this.description = description),
onChangedDescription: (description) => setState(() => this.description = description),
onChangedIsText: (isPreview) => setState(() => this.isPreview = isPreview),
),
),
);
Expand All @@ -57,15 +58,16 @@ class _AddEditNotePageState extends State<AddEditNotePage> {
final isFormValid = title.isNotEmpty;

return Padding(
padding: EdgeInsets.symmetric(vertical: 8, horizontal: 12),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
foregroundColor: Colors.white,
backgroundColor: isFormValid ? Colors.green.shade500 : Colors.grey.shade700,
),
onPressed: addorUpdateNote,
child: Text('Save'),
));
padding: EdgeInsets.symmetric(vertical: 8, horizontal: 12),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
foregroundColor: Colors.white,
backgroundColor: isFormValid ? Colors.green.shade500 : Colors.grey.shade700,
),
onPressed: addorUpdateNote,
child: Text('Save'),
),
);
}

void addorUpdateNote() async {
Expand All @@ -92,17 +94,11 @@ class _AddEditNotePageState extends State<AddEditNotePage> {
title: title,
description: description,
);

await NoteDatabase.instance.update(note);
}

Future addNote() async {
final note = Note(
isImportant: isImportant,
title: title,
description: description,
createdTime: DateTime.now());

final note = Note(isImportant: isImportant, title: title, description: description, createdTime: DateTime.now());
await NoteDatabase.instance.create(note);
}
}
27 changes: 14 additions & 13 deletions lib/pages/note_detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:intl/intl.dart';
import 'package:notesapp/db/notes_database.dart';
import 'package:notesapp/model/note.dart';
import 'package:notesapp/pages/add_edit_note_page.dart';
import 'package:notesapp/widgets/markdown_rendered.dart';

class NoteDetailPage extends StatefulWidget {
NoteDetailPage({super.key, required this.noteId});
Expand Down Expand Up @@ -42,25 +43,25 @@ class _NoteDetailPageState extends State<NoteDetailPage> {
? Center(child: CircularProgressIndicator())
: Padding(
padding: EdgeInsets.all(12),
child: ListView(
padding: EdgeInsets.symmetric(vertical: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: [
Text(note.title,
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontWeight: FontWeight.bold,
)),
Text(
note.title,
style: TextStyle(
color: Colors.white,
fontSize: 30,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 8),
Text(
DateFormat.yMMMd().format(note.createdTime),
style: TextStyle(color: Colors.white38),
),
SizedBox(height: 8),
Text(
note.description,
style: TextStyle(color: Colors.white70, fontSize: 18),
)
SizedBox(height: 20),
MarkDownRenderer(data: note.description),
],
),
),
Expand Down
39 changes: 10 additions & 29 deletions lib/pages/note_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import 'package:notesapp/model/note.dart';
import 'package:notesapp/pages/add_edit_note_page.dart';
import 'package:notesapp/pages/note_detail_page.dart';
import 'package:notesapp/widgets/note_cart_widget.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:velocity_x/velocity_x.dart';

import '../widgets/custom_drawer.dart';

class NotePageState extends StatefulWidget {
const NotePageState({super.key});

Expand All @@ -31,7 +32,6 @@ class _NotePageState extends State<NotePageState> {
@override
void dispose() {
NoteDatabase.instance.close();

super.dispose();
}

Expand All @@ -44,37 +44,16 @@ class _NotePageState extends State<NotePageState> {
@override
Widget build(BuildContext context) {
return Scaffold(
// App Bar
appBar: AppBar(
title: "Notes".text.xl3.white.make(),
backgroundColor: Colors.transparent,
),
drawer: Drawer(
child: Container(
color: Colors.grey.shade900,
child: Column(children: [
80.heightBox,
"Made By".text.white.xl3.semiBold.make().p(16),
"Achintya".text.white.bold.xl4.make().shimmer(primaryColor: Vx.pink500, secondaryColor: Vx.blue500),
20.heightBox,
ElevatedButton(
onPressed: () {
launch("https://linktr.ee/achintya_only");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.greenAccent,
padding: EdgeInsets.symmetric(horizontal: 35, vertical: 10),
textStyle: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.white70)),
child: "Link Tree".text.white.make(),
),
IconButton(
onPressed: () {
launch("https://github.com/achintya-7/notesApp_flutter");
},
icon: Image.asset('assets/images/icons8-github-96.png'),
iconSize: 80)
]),
),
),

// Drawer
drawer: CustomDrawer(),

// Body
body: _isLoading
? CircularProgressIndicator()
: notes.isEmpty
Expand All @@ -85,6 +64,8 @@ class _NotePageState extends State<NotePageState> {
),
)
: buildNotes(),

// Floating Action Button
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.white,
child: Icon(Icons.add, color: Colors.black),
Expand Down
38 changes: 38 additions & 0 deletions lib/widgets/custom_drawer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:velocity_x/velocity_x.dart';

class CustomDrawer extends StatelessWidget {
const CustomDrawer({super.key});

@override
Widget build(BuildContext context) {
return Drawer(
child: Container(
color: Colors.grey.shade900,
child: Column(
children: [
80.heightBox,
"Made By".text.white.xl3.semiBold.make().p(16),
"Achintya".text.white.bold.xl4.make().shimmer(primaryColor: Vx.pink500, secondaryColor: Vx.blue500),
20.heightBox,
ElevatedButton(
onPressed: () => launchUrlString("https://linktr.ee/achintya_only"),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.greenAccent,
padding: const EdgeInsets.symmetric(horizontal: 35, vertical: 10),
textStyle: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.white70),
),
child: "Link Tree".text.white.make(),
),
IconButton(
onPressed: () => launchUrlString("https://github.com/achintya-7/notesApp_flutter"),
icon: Image.asset('assets/images/icons8-github-96.png'),
iconSize: 80,
)
],
),
),
);
}
}
20 changes: 20 additions & 0 deletions lib/widgets/markdown_rendered.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_markdown/flutter_markdown.dart';

class MarkDownRenderer extends StatelessWidget {
const MarkDownRenderer({super.key, required this.data});
final String data;

@override
Widget build(BuildContext context) {
return Expanded(
child: SingleChildScrollView(
child: MarkdownBody(
data: data,
selectable: true,
fitContent: false,
),
),
);
}
}
61 changes: 30 additions & 31 deletions lib/widgets/note_cart_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ class NoteCardWidget extends StatelessWidget {

@override
Widget build(BuildContext context) {

/// Pick colors from the accent colors based on index
final color = _lightColors[index % _lightColors.length];
final time = DateFormat.yMMMd().format(note.createdTime);
final minHeight = getMinHeight(index);

return Card(
color: color,
child: Container(
child: Container(
constraints: BoxConstraints(minHeight: minHeight, maxHeight: 250),
padding: EdgeInsets.all(8),
child: Column(
Expand All @@ -45,44 +44,36 @@ class NoteCardWidget extends StatelessWidget {
style: TextStyle(color: Colors.grey.shade700),
),
SizedBox(height: 4),

Text(
note.title,
style: TextStyle(
color: Colors.black,
fontSize: 20,
fontWeight: FontWeight.bold
),
style: TextStyle(color: Colors.black, fontSize: 20, fontWeight: FontWeight.bold),
),


note.isImportant
? Text(
" ... ",
maxLines: 4,
overflow: TextOverflow.fade,
style: TextStyle(
color: Colors.grey.shade800,
fontSize: 16,
),
)
: Text(
note.description,
maxLines: 4,
overflow: TextOverflow.fade,
style: TextStyle(
color: Colors.grey.shade800,
fontSize: 16,
),
),
? Text(
" ... ",
maxLines: 4,
overflow: TextOverflow.fade,
style: TextStyle(
color: Colors.grey.shade800,
fontSize: 16,
),
)
: Text(
extractHeadersAndText(note.description),
maxLines: 4,
overflow: TextOverflow.fade,
style: TextStyle(
color: Colors.grey.shade800,
fontSize: 16,
),
),
],
),
),
);


}
double getMinHeight(int index) {

double getMinHeight(int index) {
switch (index % 4) {
case 0:
return 100;
Expand All @@ -97,3 +88,11 @@ class NoteCardWidget extends StatelessWidget {
}
}
}

String extractHeadersAndText(String markdown) {
String extractedContent = markdown.
replaceAll(RegExp(r"!\[.*?\]\(.*?\)|\[.*?\]\(.*?\)"), '')
.replaceAll("#", "");

return extractedContent;
}
Loading

0 comments on commit 4602901

Please sign in to comment.