diff --git a/basic_flutter/lib/main.dart b/basic_flutter/lib/main.dart index c46d61a..6e91e99 100644 --- a/basic_flutter/lib/main.dart +++ b/basic_flutter/lib/main.dart @@ -1,31 +1,21 @@ -import 'package:basic_flutter/presentation/screens/employee_dialog.dart'; - -import '/presentation/screens/employees_list_screen.dart'; import 'package:flutter/material.dart'; -import './data/routes.dart'; - -import '/presentation/screens/employees_list_screen.dart'; -import './data/routes.dart'; -import '/presentation/screens/employee_dialog.dart'; +import 'package:animated_theme_switcher/animated_theme_switcher.dart'; +import '../themes.dart'; -void main() { +Future main() async { + WidgetsFlutterBinding.ensureInitialized(); runApp(const MyApp()); } class MyApp extends StatelessWidget { - const MyApp({Key? key}) : super(key: key); + const MyApp({Key? key, this.onClicked}) : super(key: key); + final VoidCallback? onClicked; @override Widget build(BuildContext context) { return MaterialApp( - title: "Employee App", home: const MyHomePage(), - routes: { - Routes.employeeListScreenRouteName: (context) => - const EmployeesListScreen(), - Routes.employeeDialogRouteName: (context) => const EmployeeDialog(), - }, theme: ThemeData( primaryColor: Colors.blue.shade900, splashColor: Colors.blue, @@ -49,26 +39,106 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { + late List dummyemployees; + + @override + void initState() { + super.initState(); + dummyemployees = []; + addDummyEmployees(); + } + + addDummyEmployees() { + dummyemployees.add("Employee 1"); + dummyemployees.add("Employee 2"); + dummyemployees.add("Employee 3"); + } + + removeDummyEmployee(index) { + setState(() { + dummyemployees.removeAt(index); + }); + } + + Widget swipeDeleteButton() { + return Container( + alignment: Alignment.centerRight, + padding: const EdgeInsets.only(right: 20.0), + color: Colors.red, + child: const Icon(Icons.delete, color: Colors.white), + ); + } + + undoDelete(index, dummyemployee) { + setState(() { + dummyemployees.insert(index, dummyemployee); + }); + } + + showSnackBar(context, dummyemployee, index) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text('$dummyemployee deleted'), + action: SnackBarAction( + label: "undo delete", + onPressed: () { + undoDelete(index, dummyemployee); + }), + ), + ); + } + + Widget list() { + return ListView.builder( + padding: const EdgeInsets.all(20.0), + itemCount: dummyemployees.length, + itemBuilder: (BuildContext context, int index) { + return row(context, index); + }, + ); + } + + Widget row(context, index) { + return Dismissible( + key: Key(dummyemployees[index]), + onDismissed: (direction) { + var dummyemployee = dummyemployees[index]; + showSnackBar(context, dummyemployee, index); + removeDummyEmployee(index); + }, + background: swipeDeleteButton(), + child: Card( + child: ListTile( + title: Text(dummyemployees[index]), + ), + ), + ); + } + @override Widget build(BuildContext context) { + final isDarkMode = Theme.of(context).brightness == Brightness.dark; + final icon = isDarkMode ? Icons.light_mode : Icons.dark_mode; return Scaffold( appBar: AppBar( title: const Text('Employee App'), backgroundColor: Theme.of(context).primaryColor, centerTitle: true, - ), - body: Center( - child: ElevatedButton( - onPressed: () { - Navigator.pushNamed(context, Routes.employeeListScreenRouteName); - }, - child: const Text('Get Employees List'), - style: ButtonStyle( - backgroundColor: - MaterialStateProperty.all(Theme.of(context).primaryColor), + actions: [ + ThemeSwitcher( + builder: (context) => IconButton( + icon: Icon(icon), + onPressed: () { + final theme = + isDarkMode ? MyThemes.lightTheme : MyThemes.darkTheme; + final switcher = ThemeSwitcher.of(context); + switcher.changeTheme(theme: theme); + }, + ), ), - ), + ], ), + body: list(), ); } } diff --git a/basic_flutter/lib/themes.dart b/basic_flutter/lib/themes.dart new file mode 100644 index 0000000..902b952 --- /dev/null +++ b/basic_flutter/lib/themes.dart @@ -0,0 +1,20 @@ +import 'package:flutter/material.dart'; + +class MyThemes { + static final primary = Colors.blue.shade900; + static final primaryColor = Colors.blue.shade900; + + static final darkTheme = ThemeData( + scaffoldBackgroundColor: Colors.black, + primaryColorDark: primaryColor, + colorScheme: ColorScheme.dark(primary: primary), + dividerColor: Colors.white, + ); + + static final lightTheme = ThemeData( + scaffoldBackgroundColor: Colors.white, + primaryColor: primaryColor, + colorScheme: ColorScheme.light(primary: primary), + dividerColor: Colors.black, + ); +} diff --git a/basic_flutter/pubspec.lock b/basic_flutter/pubspec.lock index c8b89cf..a07caed 100644 --- a/basic_flutter/pubspec.lock +++ b/basic_flutter/pubspec.lock @@ -1,13 +1,20 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + animated_theme_switcher: + dependency: "direct main" + description: + name: animated_theme_switcher + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.6" async: dependency: transitive description: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.8.1" + version: "2.8.2" bloc: dependency: "direct main" description: @@ -28,7 +35,7 @@ packages: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0" charcode: dependency: transitive description: @@ -122,7 +129,7 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.10" + version: "0.12.11" meta: dependency: transitive description: @@ -197,7 +204,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.2" + version: "0.4.3" typed_data: dependency: transitive description: @@ -211,7 +218,7 @@ packages: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.0" + version: "2.1.1" sdks: - dart: ">=2.14.0 <3.0.0" - flutter: ">=1.16.0" + dart: ">=2.14.4 <3.0.0" + flutter: ">=2.5.3" diff --git a/basic_flutter/pubspec.yaml b/basic_flutter/pubspec.yaml index d603bc2..31753bd 100644 --- a/basic_flutter/pubspec.yaml +++ b/basic_flutter/pubspec.yaml @@ -27,6 +27,7 @@ environment: # the latest version available on pub.dev. To see which dependencies have newer # versions available, run `flutter pub outdated`. dependencies: + animated_theme_switcher: ^2.0.6 bloc: ^7.2.1 cupertino_icons: ^1.0.2 flutter: