From 26ce87c1019bf45ce5cb821e0a0d295495c19a5a Mon Sep 17 00:00:00 2001 From: KaushalVasava Date: Mon, 24 Jul 2023 20:26:12 +0530 Subject: [PATCH 1/2] Add note app - Note list - Add Search feature - Add Note feature - Delete feature --- lib/main.dart | 131 ++----------------------- lib/model/Note.dart | 22 +++++ lib/screens/home.dart | 191 +++++++++++++++++++++++++++++++++++++ lib/widgets/note_item.dart | 47 +++++++++ pubspec.yaml | 2 + 5 files changed, 270 insertions(+), 123 deletions(-) create mode 100644 lib/model/Note.dart create mode 100644 lib/screens/home.dart create mode 100644 lib/widgets/note_item.dart diff --git a/lib/main.dart b/lib/main.dart index 52aa154..f600b7b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,6 @@ import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import './screens/home.dart'; void main() { runApp(const MyApp()); @@ -7,132 +9,15 @@ 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( - title: 'Flutter Demo', - theme: ThemeData( - // This is the theme of your application. - // - // TRY THIS: Try running your application with "flutter run". You'll see - // the application has a blue toolbar. Then, without quitting the app, - // try changing the seedColor in the colorScheme below to Colors.green - // and then invoke "hot reload" (save your changes or press the "hot - // reload" button in a Flutter-supported IDE, or press "r" if you used - // the command line to start the app). - // - // Notice that the counter didn't reset back to zero; the application - // state is not lost during the reload. To reset the state, use hot - // restart instead. - // - // This works for code too, not just values: Most code changes can be - // tested with just a hot reload. - colorScheme: ColorScheme.fromSeed(seedColor: Colors.green), - useMaterial3: true, - ), - home: const MyHomePage(title: 'Flutter Demo'), + SystemChrome.setSystemUIOverlayStyle( + SystemUiOverlayStyle(statusBarColor: Colors.transparent) ); - } -} - -class MyHomePage extends StatefulWidget { - const MyHomePage({super.key, required this.title}); - - // This widget is the home page of your application. It is stateful, meaning - // that it has a State object (defined below) that contains fields that affect - // how it looks. - - // This class is the configuration for the state. It holds the values (in this - // case the title) provided by the parent (in this case the App widget) and - // used by the build method of the State. Fields in a Widget subclass are - // always marked "final". - - final String title; - @override - State createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - int _counter = 0; - - void _incrementCounter() { - setState(() { - // This call to setState tells the Flutter framework that something has - // changed in this State, which causes it to rerun the build method below - // so that the display can reflect the updated values. If we changed - // _counter without calling setState(), then the build method would not be - // called again, and so nothing would appear to happen. - _counter++; - }); - } - void _decrementCounter() { - setState(() { - // This call to setState tells the Flutter framework that something has - // changed in this State, which causes it to rerun the build method below - // so that the display can reflect the updated values. If we changed - // _counter without calling setState(), then the build method would not be - // called again, and so nothing would appear to happen. - _counter--; - }); - } - - @override - Widget build(BuildContext context) { - // This method is rerun every time setState is called, for instance as done - // by the _incrementCounter method above. - // - // The Flutter framework has been optimized to make rerunning build methods - // fast, so that you can just rebuild anything that needs updating rather - // than having to individually change instances of widgets. - return Scaffold( - appBar: AppBar( - // TRY THIS: Try changing the color here to a specific color (to - // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar - // change color while the other colors stay the same. - backgroundColor: Theme.of(context).colorScheme.inversePrimary, - // Here we take the value from the MyHomePage object that was created by - // the App.build method, and use it to set our appbar title. - title: Text(widget.title), - ), - body: Center( - // Center is a layout widget. It takes a single child and positions it - // in the middle of the parent. - child: Column( - // Column is also a layout widget. It takes a list of children and - // arranges them vertically. By default, it sizes itself to fit its - // children horizontally, and tries to be as tall as its parent. - // - // Column has various properties to control how it sizes itself and - // how it positions its children. Here we use mainAxisAlignment to - // center the children vertically; the main axis here is the vertical - // axis because Columns are vertical (the cross axis would be - // horizontal). - // - // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint" - // action in the IDE, or press "p" in the console), to see the - // wireframe for each widget. - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text( - 'You have pushed the button this many times:', - ), - Text( - '$_counter', - style: Theme.of(context).textTheme.headlineMedium, - ), - FilledButton( - onPressed: _decrementCounter, - child: const Text('Press me!'), - ), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: const Icon(Icons.add), - ), // This trailing comma makes auto-formatting nicer for build methods. + return const MaterialApp( + debugShowCheckedModeBanner: false, + title: 'Note App', + home: Home(), ); } } diff --git a/lib/model/Note.dart b/lib/model/Note.dart new file mode 100644 index 0000000..807d250 --- /dev/null +++ b/lib/model/Note.dart @@ -0,0 +1,22 @@ +class Note { + int id; + String title; + bool isDone; + + Note({required this.id, required this.title, this.isDone = false}); + + static List noteList() { + return [ + Note(id: 0, title: "Morning Exercise", isDone: true), + Note(id: 1, title: "Evening Exercise"), + Note(id: 2, title: "Android App Development"), + Note(id: 3, title: "Flutter simple app"), + Note(id: 4, title: "Build Rest Api Using Ktor", isDone: true), + Note(id: 5, title: "Mail for Job"), + Note(id: 6, title: "Mail to mobile distributors"), + Note(id: 7, title: "Apply for Jobs"), + Note(id: 8, title: "Movie watch"), + Note(id: 9, title: "Sleep 8 hours", isDone: true), + ]; + } +} diff --git a/lib/screens/home.dart b/lib/screens/home.dart new file mode 100644 index 0000000..fe79fcf --- /dev/null +++ b/lib/screens/home.dart @@ -0,0 +1,191 @@ +import 'package:basic_app/widgets/note_item.dart'; +import 'package:flutter/material.dart'; + +import '../model/Note.dart'; + +class Home extends StatefulWidget { + const Home({super.key}); + + @override + State createState() => _HomeState(); +} + +class _HomeState extends State { + final noteList = Note.noteList(); + List foundList = []; + final _todoController = TextEditingController(); + + @override + void initState() { + foundList = noteList; + super.initState(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + backgroundColor: Colors.white70, + appBar: buildAppBar(), + body: Stack( + children: [ + Container( + padding: EdgeInsets.symmetric(horizontal: 16), + child: Column( + children: [searchBox(), listView()], + ), + ), + Align( + alignment: Alignment.bottomCenter, + child: Row( + children: [ + Expanded( + child: Container( + margin: + const EdgeInsets.only(bottom: 16, right: 16, left: 16), + padding: EdgeInsets.symmetric(horizontal: 16, vertical: 5), + decoration: BoxDecoration( + color: Colors.white, + boxShadow: const [ + BoxShadow( + color: Colors.grey, + offset: Offset(0.0, 0.0), + blurRadius: 10.0, + spreadRadius: 0.0) + ], + borderRadius: BorderRadius.circular(10), + ), + child: TextField( + controller: _todoController, + decoration: InputDecoration( + hintText: "Add new note", + border: InputBorder.none, + ), + ), + ), + ), + Container( + margin: EdgeInsets.only(bottom: 16, right: 16), + child: ElevatedButton( + child: Text( + '+', + style: TextStyle( + fontSize: 40, + ), + ), + onPressed: () { + _addToDoItem(_todoController.text); + }, + style: ElevatedButton.styleFrom( + disabledBackgroundColor: Colors.green, + minimumSize: Size(60, 60), + elevation: 10, + ), + ), + ), + ], + ), + ) + ], + ), + ); + } + + void _handleToDoChange(Note note) { + setState(() { + note.isDone = !note.isDone; + }); + } + + void _deleteToDoItem(int id) { + setState(() { + noteList.removeWhere((item) => item.id == id); + }); + } + + void _addToDoItem(String title) { + setState(() { + noteList.add(Note( + id: DateTime.now().millisecondsSinceEpoch, + title: title, + )); + }); + // for clear text field + _todoController.clear(); + } + + void _runFilter(String query) { + List results = []; + if (query.isEmpty) { + results = noteList; + } else { + results = noteList + .where( + (item) => item.title.toLowerCase().contains(query.toLowerCase())) + .toList(); + } + setState(() { + foundList = results; + }); + } + + AppBar buildAppBar() { + return AppBar( + backgroundColor: Colors.green, + title: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Icon( + Icons.menu, + color: Colors.black, + size: 30, + ), + Icon(Icons.settings, color: Colors.black, size: 30) + ], + ), + ); + } + + Widget listView() { + return Expanded( + child: ListView( + children: [ + Container( + margin: EdgeInsets.only(top: 24, bottom: 16), + child: Text( + "All notes", + style: TextStyle(fontSize: 30, fontWeight: FontWeight.w500), + ), + ), + for (Note note in foundList) + NoteItem( + note: note, + onToDoChanged: _handleToDoChange, + onDeleteItem: _deleteToDoItem, + ), + ], + )); + } + + Widget searchBox() { + return Container( + margin: EdgeInsets.only(top: 16), + padding: EdgeInsets.symmetric(horizontal: 16), + decoration: BoxDecoration( + color: Colors.white54, borderRadius: BorderRadius.circular(20)), + child: TextField( + onChanged: (value) => _runFilter(value), + decoration: InputDecoration( + contentPadding: EdgeInsets.all(0), + prefixIcon: Icon( + Icons.search, + color: Colors.black, + size: 20, + ), + prefixIconConstraints: BoxConstraints(maxHeight: 20, minWidth: 25), + border: InputBorder.none, + hintText: "Search notes", + hintStyle: TextStyle(color: Colors.grey)), + ), + ); + } +} diff --git a/lib/widgets/note_item.dart b/lib/widgets/note_item.dart new file mode 100644 index 0000000..474363b --- /dev/null +++ b/lib/widgets/note_item.dart @@ -0,0 +1,47 @@ +import 'package:flutter/material.dart'; + +import '../model/Note.dart'; + +class NoteItem extends StatelessWidget { + const NoteItem( + {super.key, required this.note, this.onToDoChanged, this.onDeleteItem}); + + final Note note; + + final onToDoChanged; + final onDeleteItem; + + @override + Widget build(BuildContext context) { + return Container( + margin: EdgeInsets.only(bottom: 16), + child: ListTile( + onTap: () { + print("GO to new page"); + }, + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)), + tileColor: Colors.white54, + leading: IconButton( + icon: Icon( + note.isDone ? Icons.check_box : Icons.check_box_outline_blank, + color: Colors.green), + onPressed: () { + onToDoChanged(note); + }, + ), + title: Text( + note.title, + style: TextStyle( + fontSize: 16, + decoration: note.isDone ? TextDecoration.lineThrough : null), + ), + trailing: IconButton( + icon: Icon(Icons.delete), + onPressed: () { + onDeleteItem(note.id); + }, + ), + ), + ); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index b632002..a581ef6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -74,6 +74,8 @@ flutter: # "family" key with the font family name, and a "fonts" key with a # list giving the asset and other descriptors for the font. For # example: + assets: + - assets/images/ # fonts: # - family: Schyler # fonts: From 2e63800f6119a09635ae6c8c28b7349e0295aa11 Mon Sep 17 00:00:00 2001 From: KaushalVasava Date: Tue, 25 Jul 2023 14:20:13 +0530 Subject: [PATCH 2/2] Add Navigation - Add home to addnote screen navigation --- lib/main.dart | 12 ++- lib/screens/add_note.dart | 63 ++++++++++++++ lib/screens/home.dart | 169 ++++++++++++++++++++----------------- lib/widgets/note_item.dart | 13 ++- 4 files changed, 172 insertions(+), 85 deletions(-) create mode 100644 lib/screens/add_note.dart diff --git a/lib/main.dart b/lib/main.dart index f600b7b..f784d3b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,3 +1,4 @@ +import 'package:basic_app/screens/add_note.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import './screens/home.dart'; @@ -12,12 +13,17 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { SystemChrome.setSystemUIOverlayStyle( - SystemUiOverlayStyle(statusBarColor: Colors.transparent) + const SystemUiOverlayStyle(statusBarColor: Colors.transparent) ); - return const MaterialApp( + return MaterialApp( + initialRoute: '/home', + routes: { + '/home': (context) => const Home(), + '/addnote': (context) => const AddNote(noteTitle: ""), + }, debugShowCheckedModeBanner: false, title: 'Note App', - home: Home(), + home: const Home(), ); } } diff --git a/lib/screens/add_note.dart b/lib/screens/add_note.dart new file mode 100644 index 0000000..ab9c9b8 --- /dev/null +++ b/lib/screens/add_note.dart @@ -0,0 +1,63 @@ +import 'package:flutter/material.dart'; + +class AddNote extends StatefulWidget { + const AddNote({super.key, required this.noteTitle}); + + final String noteTitle; + + @override + State createState() => _AddNoteState(noteTitle: noteTitle); +} + +class _AddNoteState extends State { + final _noteController = TextEditingController(); + + final String noteTitle; + + _AddNoteState({required this.noteTitle}); + + @override + void initState() { + _noteController.text = noteTitle; + super.initState(); + } + @override + Widget build(BuildContext context) { + return Scaffold( + backgroundColor: const Color(0xEAFFFFFF), + appBar: buildAppBar(), + body: Column( + children: [ + Container( + padding: const EdgeInsets.symmetric(horizontal: 16), + child: Column( + children: [ + TextField( + controller: _noteController, + decoration: InputDecoration( + hintText: _noteController.text, + ), + ) + ], + ), + ), + ], + ), + ); + } + + AppBar buildAppBar() { + return AppBar( + backgroundColor: Colors.green, + title: const Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + "Update Note", + style: TextStyle(fontSize: 20), + ), + ], + ), + ); + } +} diff --git a/lib/screens/home.dart b/lib/screens/home.dart index fe79fcf..1191bdd 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -13,8 +13,8 @@ class Home extends StatefulWidget { class _HomeState extends State { final noteList = Note.noteList(); List foundList = []; - final _todoController = TextEditingController(); - + final _noteController = TextEditingController(); + final _searchController = TextEditingController(); @override void initState() { foundList = noteList; @@ -24,67 +24,16 @@ class _HomeState extends State { @override Widget build(BuildContext context) { return Scaffold( - backgroundColor: Colors.white70, + backgroundColor: const Color(0xEAFFFFFF), appBar: buildAppBar(), body: Stack( children: [ Container( - padding: EdgeInsets.symmetric(horizontal: 16), + padding: const EdgeInsets.symmetric(horizontal: 16), child: Column( - children: [searchBox(), listView()], + children: [searchBox(), listView(), bottomTextAndButton()], ), ), - Align( - alignment: Alignment.bottomCenter, - child: Row( - children: [ - Expanded( - child: Container( - margin: - const EdgeInsets.only(bottom: 16, right: 16, left: 16), - padding: EdgeInsets.symmetric(horizontal: 16, vertical: 5), - decoration: BoxDecoration( - color: Colors.white, - boxShadow: const [ - BoxShadow( - color: Colors.grey, - offset: Offset(0.0, 0.0), - blurRadius: 10.0, - spreadRadius: 0.0) - ], - borderRadius: BorderRadius.circular(10), - ), - child: TextField( - controller: _todoController, - decoration: InputDecoration( - hintText: "Add new note", - border: InputBorder.none, - ), - ), - ), - ), - Container( - margin: EdgeInsets.only(bottom: 16, right: 16), - child: ElevatedButton( - child: Text( - '+', - style: TextStyle( - fontSize: 40, - ), - ), - onPressed: () { - _addToDoItem(_todoController.text); - }, - style: ElevatedButton.styleFrom( - disabledBackgroundColor: Colors.green, - minimumSize: Size(60, 60), - elevation: 10, - ), - ), - ), - ], - ), - ) ], ), ); @@ -102,6 +51,12 @@ class _HomeState extends State { }); } + void _clearQuery(){ + setState(() { + foundList = noteList; + }); + _searchController.clear(); + } void _addToDoItem(String title) { setState(() { noteList.add(Note( @@ -110,7 +65,7 @@ class _HomeState extends State { )); }); // for clear text field - _todoController.clear(); + _noteController.clear(); } void _runFilter(String query) { @@ -131,15 +86,13 @@ class _HomeState extends State { AppBar buildAppBar() { return AppBar( backgroundColor: Colors.green, - title: Row( + title: const Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Icon( - Icons.menu, - color: Colors.black, - size: 30, + Text( + "Notes", + style: TextStyle(fontSize: 20), ), - Icon(Icons.settings, color: Colors.black, size: 30) ], ), ); @@ -150,10 +103,10 @@ class _HomeState extends State { child: ListView( children: [ Container( - margin: EdgeInsets.only(top: 24, bottom: 16), - child: Text( + margin: const EdgeInsets.only(top: 16, bottom: 16), + child: const Text( "All notes", - style: TextStyle(fontSize: 30, fontWeight: FontWeight.w500), + style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500), ), ), for (Note note in foundList) @@ -168,23 +121,83 @@ class _HomeState extends State { Widget searchBox() { return Container( - margin: EdgeInsets.only(top: 16), - padding: EdgeInsets.symmetric(horizontal: 16), + margin: const EdgeInsets.only(top: 16), + padding: const EdgeInsets.symmetric(horizontal: 16), decoration: BoxDecoration( - color: Colors.white54, borderRadius: BorderRadius.circular(20)), + color: Colors.white, borderRadius: BorderRadius.circular(20)), child: TextField( + controller: _searchController, + textAlignVertical: TextAlignVertical.center, onChanged: (value) => _runFilter(value), decoration: InputDecoration( - contentPadding: EdgeInsets.all(0), - prefixIcon: Icon( - Icons.search, - color: Colors.black, - size: 20, + contentPadding: const EdgeInsets.all(0), + prefixIcon: const Icon( + Icons.search, + color: Colors.black, + size: 20, + ), + prefixIconConstraints: const BoxConstraints(maxHeight: 20, minWidth: 25), + border: InputBorder.none, + hintText: "Search notes", + hintStyle: const TextStyle(color: Colors.grey), + suffixIcon: IconButton( + onPressed: _clearQuery, + icon: const Icon(Icons.close, color: Colors.grey, size: 20)), + ), + ), + ); + } + + Widget bottomTextAndButton() { + return Align( + alignment: Alignment.bottomCenter, + child: Row( + children: [ + Expanded( + child: Container( + margin: const EdgeInsets.only(bottom: 16, right: 16), + padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 5), + decoration: BoxDecoration( + color: Colors.white, + boxShadow: const [ + BoxShadow( + color: Colors.grey, + offset: Offset(0.0, 0.0), + blurRadius: 10.0, + spreadRadius: 0.0) + ], + borderRadius: BorderRadius.circular(10), + ), + child: TextField( + controller: _noteController, + decoration: const InputDecoration( + hintText: "Add new note", + border: InputBorder.none, + ), + ), ), - prefixIconConstraints: BoxConstraints(maxHeight: 20, minWidth: 25), - border: InputBorder.none, - hintText: "Search notes", - hintStyle: TextStyle(color: Colors.grey)), + ), + Container( + margin: const EdgeInsets.only(bottom: 16), + child: ElevatedButton( + onPressed: () { + _addToDoItem(_noteController.text); + }, + style: ElevatedButton.styleFrom( + backgroundColor: Colors.green, + minimumSize: const Size(60, 60), + elevation: 10, + shape: const CircleBorder(), + ), + child: const Text( + '+', + style: TextStyle( + fontSize: 25, + ), + ), + ), + ), + ], ), ); } diff --git a/lib/widgets/note_item.dart b/lib/widgets/note_item.dart index 474363b..68413a6 100644 --- a/lib/widgets/note_item.dart +++ b/lib/widgets/note_item.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import '../model/Note.dart'; +import '../screens/add_note.dart'; class NoteItem extends StatelessWidget { const NoteItem( @@ -14,13 +15,17 @@ class NoteItem extends StatelessWidget { @override Widget build(BuildContext context) { return Container( - margin: EdgeInsets.only(bottom: 16), + margin: const EdgeInsets.only(bottom: 16), child: ListTile( onTap: () { - print("GO to new page"); + Navigator.push(context, + MaterialPageRoute( + builder: (context) => AddNote(noteTitle: note.title)) + ); + // Navigator.pushNamed(context, '/addnote',arguments: {'key': note.title}); }, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)), - tileColor: Colors.white54, + tileColor: Colors.white, leading: IconButton( icon: Icon( note.isDone ? Icons.check_box : Icons.check_box_outline_blank, @@ -36,7 +41,7 @@ class NoteItem extends StatelessWidget { decoration: note.isDone ? TextDecoration.lineThrough : null), ), trailing: IconButton( - icon: Icon(Icons.delete), + icon: const Icon(Icons.delete), onPressed: () { onDeleteItem(note.id); },