diff --git a/lib/main.dart b/lib/main.dart index 52aa154..f784d3b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,7 @@ +import 'package:basic_app/screens/add_note.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import './screens/home.dart'; void main() { runApp(const MyApp()); @@ -7,132 +10,20 @@ 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( + const 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 MaterialApp( + initialRoute: '/home', + routes: { + '/home': (context) => const Home(), + '/addnote': (context) => const AddNote(noteTitle: ""), + }, + debugShowCheckedModeBanner: false, + title: 'Note App', + home: const 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/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 new file mode 100644 index 0000000..1191bdd --- /dev/null +++ b/lib/screens/home.dart @@ -0,0 +1,204 @@ +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 _noteController = TextEditingController(); + final _searchController = TextEditingController(); + @override + void initState() { + foundList = noteList; + super.initState(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + backgroundColor: const Color(0xEAFFFFFF), + appBar: buildAppBar(), + body: Stack( + children: [ + Container( + padding: const EdgeInsets.symmetric(horizontal: 16), + child: Column( + children: [searchBox(), listView(), bottomTextAndButton()], + ), + ), + ], + ), + ); + } + + void _handleToDoChange(Note note) { + setState(() { + note.isDone = !note.isDone; + }); + } + + void _deleteToDoItem(int id) { + setState(() { + noteList.removeWhere((item) => item.id == id); + }); + } + + void _clearQuery(){ + setState(() { + foundList = noteList; + }); + _searchController.clear(); + } + void _addToDoItem(String title) { + setState(() { + noteList.add(Note( + id: DateTime.now().millisecondsSinceEpoch, + title: title, + )); + }); + // for clear text field + _noteController.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: const Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + "Notes", + style: TextStyle(fontSize: 20), + ), + ], + ), + ); + } + + Widget listView() { + return Expanded( + child: ListView( + children: [ + Container( + margin: const EdgeInsets.only(top: 16, bottom: 16), + child: const Text( + "All notes", + style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500), + ), + ), + for (Note note in foundList) + NoteItem( + note: note, + onToDoChanged: _handleToDoChange, + onDeleteItem: _deleteToDoItem, + ), + ], + )); + } + + Widget searchBox() { + return Container( + margin: const EdgeInsets.only(top: 16), + padding: const EdgeInsets.symmetric(horizontal: 16), + decoration: BoxDecoration( + color: Colors.white, borderRadius: BorderRadius.circular(20)), + child: TextField( + controller: _searchController, + textAlignVertical: TextAlignVertical.center, + onChanged: (value) => _runFilter(value), + decoration: InputDecoration( + 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, + ), + ), + ), + ), + 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 new file mode 100644 index 0000000..68413a6 --- /dev/null +++ b/lib/widgets/note_item.dart @@ -0,0 +1,52 @@ +import 'package:flutter/material.dart'; + +import '../model/Note.dart'; +import '../screens/add_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: const EdgeInsets.only(bottom: 16), + child: ListTile( + onTap: () { + 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.white, + 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: const 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: