diff --git a/.gitignore b/.gitignore index 79c113f..d0b7d8b 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,5 @@ app.*.map.json /android/app/debug /android/app/profile /android/app/release + +.vscode \ No newline at end of file diff --git a/ios/Podfile.lock b/ios/Podfile.lock new file mode 100644 index 0000000..02bd6c3 --- /dev/null +++ b/ios/Podfile.lock @@ -0,0 +1,23 @@ +PODS: + - Flutter (1.0.0) + - shared_preferences_foundation (0.0.1): + - Flutter + - FlutterMacOS + +DEPENDENCIES: + - Flutter (from `Flutter`) + - shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`) + +EXTERNAL SOURCES: + Flutter: + :path: Flutter + shared_preferences_foundation: + :path: ".symlinks/plugins/shared_preferences_foundation/darwin" + +SPEC CHECKSUMS: + Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7 + shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78 + +PODFILE CHECKSUM: 4305caec6b40dde0ae97be1573c53de1882a07e5 + +COCOAPODS: 1.16.2 diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj index 87a2563..da4502d 100644 --- a/ios/Runner.xcodeproj/project.pbxproj +++ b/ios/Runner.xcodeproj/project.pbxproj @@ -161,7 +161,6 @@ F06BB449B7F7E40DDF2EA4DA /* Pods-RunnerTests.release.xcconfig */, 6989FDD0423CD6426AD2CF04 /* Pods-RunnerTests.profile.xcconfig */, ); - name = Pods; path = Pods; sourceTree = ""; }; @@ -471,7 +470,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; - DEVELOPMENT_TEAM = YYRTDRLFNE; + DEVELOPMENT_TEAM = ""; ENABLE_BITCODE = NO; INFOPLIST_FILE = Runner/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( @@ -654,7 +653,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; - DEVELOPMENT_TEAM = YYRTDRLFNE; + DEVELOPMENT_TEAM = ""; ENABLE_BITCODE = NO; INFOPLIST_FILE = Runner/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( @@ -677,7 +676,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; - DEVELOPMENT_TEAM = YYRTDRLFNE; + DEVELOPMENT_TEAM = ""; ENABLE_BITCODE = NO; INFOPLIST_FILE = Runner/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( diff --git a/lib/features/todo/data/datasources/todo_remote_datasource.dart b/lib/features/todo/data/datasources/todo_remote_datasource.dart index 8c3ef0b..c1bf25b 100644 --- a/lib/features/todo/data/datasources/todo_remote_datasource.dart +++ b/lib/features/todo/data/datasources/todo_remote_datasource.dart @@ -10,8 +10,7 @@ import '../models/todo_dto.dart'; @injectable class TodoRemoteDataSource { final http.Client _client; - // TODO: Replace this with the URL we provide you - final String baseUrl = 'https://6825aa0f0f0188d7e72ddd9a.mockapi.io/api/v1'; + final String baseUrl = 'https://68346d42464b49963602c7e1.mockapi.io'; TodoRemoteDataSource(this._client); @@ -32,9 +31,18 @@ class TodoRemoteDataSource { } } - Future addTodo(String title) async { + Future addTodo( + String title, + String? description, + String? imageUrl, + ) async { try { - final newTodo = {'title': title, 'createdAtSeconds': DateTime.now().millisecondsSinceEpoch ~/ 1000}; + final newTodo = { + 'title': title, + 'description': description, + 'imageUrl': imageUrl, + 'createdAtSeconds': DateTime.now().millisecondsSinceEpoch ~/ 1000, + }; log('Sending todo: ${json.encode(newTodo)}'); @@ -61,6 +69,31 @@ class TodoRemoteDataSource { } } + Future updateStatus(String id, bool isDone) async { + try { + final response = await _client.patch( + Uri.parse('$baseUrl/todo/$id'), + headers: {'Content-Type': 'application/json'}, + body: json.encode({'isDone': isDone}), + ); + + log('Response status: ${response.statusCode}'); + log('Response body: ${response.body}'); + + if (response.statusCode == 201 || response.statusCode == 200) { + final Map responseData = json.decode(response.body); + + // If the API response is not in the expected format, transform it + return TodoDTO.fromJson(responseData); + } else { + throw Exception('Failed to toggle todo: ${response.statusCode}'); + } + } catch (e) { + log('Error toggling todo: $e'); + throw Exception('Failed to toggle todo: $e'); + } + } + // For local client-side ID generation String generateId() { return const Uuid().v4(); diff --git a/lib/features/todo/data/models/todo_dto.dart b/lib/features/todo/data/models/todo_dto.dart index 8d9d649..320a4c2 100644 --- a/lib/features/todo/data/models/todo_dto.dart +++ b/lib/features/todo/data/models/todo_dto.dart @@ -10,8 +10,12 @@ class TodoDTO with _$TodoDTO { const factory TodoDTO({ required String id, required String title, + required bool isDone, + String? description, + String? imageUrl, int? createdAtSeconds, }) = _TodoDTO; - factory TodoDTO.fromJson(Map json) => _$TodoDTOFromJson(json); + factory TodoDTO.fromJson(Map json) => + _$TodoDTOFromJson(json); } diff --git a/lib/features/todo/data/models/todo_dto.freezed.dart b/lib/features/todo/data/models/todo_dto.freezed.dart index 752b03e..21e96b2 100644 --- a/lib/features/todo/data/models/todo_dto.freezed.dart +++ b/lib/features/todo/data/models/todo_dto.freezed.dart @@ -23,6 +23,9 @@ TodoDTO _$TodoDTOFromJson(Map json) { mixin _$TodoDTO { String get id => throw _privateConstructorUsedError; String get title => throw _privateConstructorUsedError; + bool get isDone => throw _privateConstructorUsedError; + String? get description => throw _privateConstructorUsedError; + String? get imageUrl => throw _privateConstructorUsedError; int? get createdAtSeconds => throw _privateConstructorUsedError; /// Serializes this TodoDTO to a JSON map. @@ -39,7 +42,14 @@ abstract class $TodoDTOCopyWith<$Res> { factory $TodoDTOCopyWith(TodoDTO value, $Res Function(TodoDTO) then) = _$TodoDTOCopyWithImpl<$Res, TodoDTO>; @useResult - $Res call({String id, String title, int? createdAtSeconds}); + $Res call({ + String id, + String title, + bool isDone, + String? description, + String? imageUrl, + int? createdAtSeconds, + }); } /// @nodoc @@ -59,6 +69,9 @@ class _$TodoDTOCopyWithImpl<$Res, $Val extends TodoDTO> $Res call({ Object? id = null, Object? title = null, + Object? isDone = null, + Object? description = freezed, + Object? imageUrl = freezed, Object? createdAtSeconds = freezed, }) { return _then( @@ -73,6 +86,21 @@ class _$TodoDTOCopyWithImpl<$Res, $Val extends TodoDTO> ? _value.title : title // ignore: cast_nullable_to_non_nullable as String, + isDone: + null == isDone + ? _value.isDone + : isDone // ignore: cast_nullable_to_non_nullable + as bool, + description: + freezed == description + ? _value.description + : description // ignore: cast_nullable_to_non_nullable + as String?, + imageUrl: + freezed == imageUrl + ? _value.imageUrl + : imageUrl // ignore: cast_nullable_to_non_nullable + as String?, createdAtSeconds: freezed == createdAtSeconds ? _value.createdAtSeconds @@ -92,7 +120,14 @@ abstract class _$$TodoDTOImplCopyWith<$Res> implements $TodoDTOCopyWith<$Res> { ) = __$$TodoDTOImplCopyWithImpl<$Res>; @override @useResult - $Res call({String id, String title, int? createdAtSeconds}); + $Res call({ + String id, + String title, + bool isDone, + String? description, + String? imageUrl, + int? createdAtSeconds, + }); } /// @nodoc @@ -111,6 +146,9 @@ class __$$TodoDTOImplCopyWithImpl<$Res> $Res call({ Object? id = null, Object? title = null, + Object? isDone = null, + Object? description = freezed, + Object? imageUrl = freezed, Object? createdAtSeconds = freezed, }) { return _then( @@ -125,6 +163,21 @@ class __$$TodoDTOImplCopyWithImpl<$Res> ? _value.title : title // ignore: cast_nullable_to_non_nullable as String, + isDone: + null == isDone + ? _value.isDone + : isDone // ignore: cast_nullable_to_non_nullable + as bool, + description: + freezed == description + ? _value.description + : description // ignore: cast_nullable_to_non_nullable + as String?, + imageUrl: + freezed == imageUrl + ? _value.imageUrl + : imageUrl // ignore: cast_nullable_to_non_nullable + as String?, createdAtSeconds: freezed == createdAtSeconds ? _value.createdAtSeconds @@ -141,6 +194,9 @@ class _$TodoDTOImpl extends _TodoDTO { const _$TodoDTOImpl({ required this.id, required this.title, + required this.isDone, + this.description, + this.imageUrl, this.createdAtSeconds, }) : super._(); @@ -152,11 +208,17 @@ class _$TodoDTOImpl extends _TodoDTO { @override final String title; @override + final bool isDone; + @override + final String? description; + @override + final String? imageUrl; + @override final int? createdAtSeconds; @override String toString() { - return 'TodoDTO(id: $id, title: $title, createdAtSeconds: $createdAtSeconds)'; + return 'TodoDTO(id: $id, title: $title, isDone: $isDone, description: $description, imageUrl: $imageUrl, createdAtSeconds: $createdAtSeconds)'; } @override @@ -166,13 +228,26 @@ class _$TodoDTOImpl extends _TodoDTO { other is _$TodoDTOImpl && (identical(other.id, id) || other.id == id) && (identical(other.title, title) || other.title == title) && + (identical(other.isDone, isDone) || other.isDone == isDone) && + (identical(other.description, description) || + other.description == description) && + (identical(other.imageUrl, imageUrl) || + other.imageUrl == imageUrl) && (identical(other.createdAtSeconds, createdAtSeconds) || other.createdAtSeconds == createdAtSeconds)); } @JsonKey(includeFromJson: false, includeToJson: false) @override - int get hashCode => Object.hash(runtimeType, id, title, createdAtSeconds); + int get hashCode => Object.hash( + runtimeType, + id, + title, + isDone, + description, + imageUrl, + createdAtSeconds, + ); /// Create a copy of TodoDTO /// with the given fields replaced by the non-null parameter values. @@ -192,6 +267,9 @@ abstract class _TodoDTO extends TodoDTO { const factory _TodoDTO({ required final String id, required final String title, + required final bool isDone, + final String? description, + final String? imageUrl, final int? createdAtSeconds, }) = _$TodoDTOImpl; const _TodoDTO._() : super._(); @@ -203,6 +281,12 @@ abstract class _TodoDTO extends TodoDTO { @override String get title; @override + bool get isDone; + @override + String? get description; + @override + String? get imageUrl; + @override int? get createdAtSeconds; /// Create a copy of TodoDTO diff --git a/lib/features/todo/data/models/todo_dto.g.dart b/lib/features/todo/data/models/todo_dto.g.dart index bada3c6..659ddc6 100644 --- a/lib/features/todo/data/models/todo_dto.g.dart +++ b/lib/features/todo/data/models/todo_dto.g.dart @@ -10,6 +10,9 @@ _$TodoDTOImpl _$$TodoDTOImplFromJson(Map json) => _$TodoDTOImpl( id: json['id'] as String, title: json['title'] as String, + isDone: json['isDone'] as bool, + description: json['description'] as String?, + imageUrl: json['imageUrl'] as String?, createdAtSeconds: (json['createdAtSeconds'] as num?)?.toInt(), ); @@ -17,5 +20,8 @@ Map _$$TodoDTOImplToJson(_$TodoDTOImpl instance) => { 'id': instance.id, 'title': instance.title, + 'isDone': instance.isDone, + 'description': instance.description, + 'imageUrl': instance.imageUrl, 'createdAtSeconds': instance.createdAtSeconds, }; diff --git a/lib/features/todo/data/repositories/todo_repository.dart b/lib/features/todo/data/repositories/todo_repository.dart index 49e63b0..9336f3f 100644 --- a/lib/features/todo/data/repositories/todo_repository.dart +++ b/lib/features/todo/data/repositories/todo_repository.dart @@ -23,7 +23,9 @@ class TodoRepository { final todos = remoteDtos.map(_mapDtoToModel).toList(); // Sort todos by creation date (newest first) - todos.sort((a, b) => b.effectiveCreatedAt.compareTo(a.effectiveCreatedAt)); + todos.sort( + (a, b) => b.effectiveCreatedAt.compareTo(a.effectiveCreatedAt), + ); return todos; } catch (e) { @@ -34,27 +36,54 @@ class TodoRepository { } // Add a new todo remotely - Future addTodo(String title) async { + Future addTodo( + String title, + String? description, + String? imageUrl, + ) async { try { // Add todo remotely - await remoteDataSource.addTodo(title); + await remoteDataSource.addTodo(title, description, imageUrl); } catch (e) { log('Error adding remote todo: $e'); } } + // Toggle a todo as done/to do + Future updateStatus(String id, bool isDone) async { + try { + await remoteDataSource.updateStatus(id, isDone); + } catch (e) { + log('Error toggling remote todo: $e'); + } + } + // Helper method to map DTOs to domain models TodoModel _mapDtoToModel(TodoDTO dto) { try { DateTime? createdAt; if (dto.createdAtSeconds != null) { - createdAt = DateTime.fromMillisecondsSinceEpoch(dto.createdAtSeconds! * 1000); + createdAt = DateTime.fromMillisecondsSinceEpoch( + dto.createdAtSeconds! * 1000, + ); } - return TodoModel(id: dto.id, title: dto.title, createdAt: createdAt); + return TodoModel( + id: dto.id, + title: dto.title, + isDone: dto.isDone, + description: dto.description, + imageUrl: dto.imageUrl, + createdAt: createdAt, + ); } catch (e) { log('Error mapping DTO to model: $e'); - return TodoModel(id: const Uuid().v4(), title: 'Unknown title', createdAt: DateTime.now()); + return TodoModel( + id: const Uuid().v4(), + title: 'Unknown title', + isDone: false, + createdAt: DateTime.now(), + ); } } } diff --git a/lib/features/todo/domain/models/todo_model.dart b/lib/features/todo/domain/models/todo_model.dart index 606a711..a40c002 100644 --- a/lib/features/todo/domain/models/todo_model.dart +++ b/lib/features/todo/domain/models/todo_model.dart @@ -8,8 +8,14 @@ part 'todo_model.g.dart'; class TodoModel with _$TodoModel { const TodoModel._(); - const factory TodoModel({required String id, required String title, DateTime? createdAt}) = - _TodoModel; + const factory TodoModel({ + required String id, + required String title, + required bool isDone, + String? description, + String? imageUrl, + DateTime? createdAt, + }) = _TodoModel; DateTime get effectiveCreatedAt => createdAt ?? DateTime.now(); @@ -18,5 +24,6 @@ class TodoModel with _$TodoModel { return dateFormat.format(effectiveCreatedAt); } - factory TodoModel.fromJson(Map json) => _$TodoModelFromJson(json); + factory TodoModel.fromJson(Map json) => + _$TodoModelFromJson(json); } diff --git a/lib/features/todo/domain/models/todo_model.freezed.dart b/lib/features/todo/domain/models/todo_model.freezed.dart index 6052b7a..7582ab1 100644 --- a/lib/features/todo/domain/models/todo_model.freezed.dart +++ b/lib/features/todo/domain/models/todo_model.freezed.dart @@ -23,6 +23,9 @@ TodoModel _$TodoModelFromJson(Map json) { mixin _$TodoModel { String get id => throw _privateConstructorUsedError; String get title => throw _privateConstructorUsedError; + bool get isDone => throw _privateConstructorUsedError; + String? get description => throw _privateConstructorUsedError; + String? get imageUrl => throw _privateConstructorUsedError; DateTime? get createdAt => throw _privateConstructorUsedError; /// Serializes this TodoModel to a JSON map. @@ -40,7 +43,14 @@ abstract class $TodoModelCopyWith<$Res> { factory $TodoModelCopyWith(TodoModel value, $Res Function(TodoModel) then) = _$TodoModelCopyWithImpl<$Res, TodoModel>; @useResult - $Res call({String id, String title, DateTime? createdAt}); + $Res call({ + String id, + String title, + bool isDone, + String? description, + String? imageUrl, + DateTime? createdAt, + }); } /// @nodoc @@ -60,6 +70,9 @@ class _$TodoModelCopyWithImpl<$Res, $Val extends TodoModel> $Res call({ Object? id = null, Object? title = null, + Object? isDone = null, + Object? description = freezed, + Object? imageUrl = freezed, Object? createdAt = freezed, }) { return _then( @@ -74,6 +87,21 @@ class _$TodoModelCopyWithImpl<$Res, $Val extends TodoModel> ? _value.title : title // ignore: cast_nullable_to_non_nullable as String, + isDone: + null == isDone + ? _value.isDone + : isDone // ignore: cast_nullable_to_non_nullable + as bool, + description: + freezed == description + ? _value.description + : description // ignore: cast_nullable_to_non_nullable + as String?, + imageUrl: + freezed == imageUrl + ? _value.imageUrl + : imageUrl // ignore: cast_nullable_to_non_nullable + as String?, createdAt: freezed == createdAt ? _value.createdAt @@ -94,7 +122,14 @@ abstract class _$$TodoModelImplCopyWith<$Res> ) = __$$TodoModelImplCopyWithImpl<$Res>; @override @useResult - $Res call({String id, String title, DateTime? createdAt}); + $Res call({ + String id, + String title, + bool isDone, + String? description, + String? imageUrl, + DateTime? createdAt, + }); } /// @nodoc @@ -113,6 +148,9 @@ class __$$TodoModelImplCopyWithImpl<$Res> $Res call({ Object? id = null, Object? title = null, + Object? isDone = null, + Object? description = freezed, + Object? imageUrl = freezed, Object? createdAt = freezed, }) { return _then( @@ -127,6 +165,21 @@ class __$$TodoModelImplCopyWithImpl<$Res> ? _value.title : title // ignore: cast_nullable_to_non_nullable as String, + isDone: + null == isDone + ? _value.isDone + : isDone // ignore: cast_nullable_to_non_nullable + as bool, + description: + freezed == description + ? _value.description + : description // ignore: cast_nullable_to_non_nullable + as String?, + imageUrl: + freezed == imageUrl + ? _value.imageUrl + : imageUrl // ignore: cast_nullable_to_non_nullable + as String?, createdAt: freezed == createdAt ? _value.createdAt @@ -140,8 +193,14 @@ class __$$TodoModelImplCopyWithImpl<$Res> /// @nodoc @JsonSerializable() class _$TodoModelImpl extends _TodoModel { - const _$TodoModelImpl({required this.id, required this.title, this.createdAt}) - : super._(); + const _$TodoModelImpl({ + required this.id, + required this.title, + required this.isDone, + this.description, + this.imageUrl, + this.createdAt, + }) : super._(); factory _$TodoModelImpl.fromJson(Map json) => _$$TodoModelImplFromJson(json); @@ -151,11 +210,17 @@ class _$TodoModelImpl extends _TodoModel { @override final String title; @override + final bool isDone; + @override + final String? description; + @override + final String? imageUrl; + @override final DateTime? createdAt; @override String toString() { - return 'TodoModel(id: $id, title: $title, createdAt: $createdAt)'; + return 'TodoModel(id: $id, title: $title, isDone: $isDone, description: $description, imageUrl: $imageUrl, createdAt: $createdAt)'; } @override @@ -165,13 +230,26 @@ class _$TodoModelImpl extends _TodoModel { other is _$TodoModelImpl && (identical(other.id, id) || other.id == id) && (identical(other.title, title) || other.title == title) && + (identical(other.isDone, isDone) || other.isDone == isDone) && + (identical(other.description, description) || + other.description == description) && + (identical(other.imageUrl, imageUrl) || + other.imageUrl == imageUrl) && (identical(other.createdAt, createdAt) || other.createdAt == createdAt)); } @JsonKey(includeFromJson: false, includeToJson: false) @override - int get hashCode => Object.hash(runtimeType, id, title, createdAt); + int get hashCode => Object.hash( + runtimeType, + id, + title, + isDone, + description, + imageUrl, + createdAt, + ); /// Create a copy of TodoModel /// with the given fields replaced by the non-null parameter values. @@ -191,6 +269,9 @@ abstract class _TodoModel extends TodoModel { const factory _TodoModel({ required final String id, required final String title, + required final bool isDone, + final String? description, + final String? imageUrl, final DateTime? createdAt, }) = _$TodoModelImpl; const _TodoModel._() : super._(); @@ -203,6 +284,12 @@ abstract class _TodoModel extends TodoModel { @override String get title; @override + bool get isDone; + @override + String? get description; + @override + String? get imageUrl; + @override DateTime? get createdAt; /// Create a copy of TodoModel diff --git a/lib/features/todo/domain/models/todo_model.g.dart b/lib/features/todo/domain/models/todo_model.g.dart index 6512f96..2a93162 100644 --- a/lib/features/todo/domain/models/todo_model.g.dart +++ b/lib/features/todo/domain/models/todo_model.g.dart @@ -10,6 +10,9 @@ _$TodoModelImpl _$$TodoModelImplFromJson(Map json) => _$TodoModelImpl( id: json['id'] as String, title: json['title'] as String, + isDone: json['isDone'] as bool, + description: json['description'] as String?, + imageUrl: json['imageUrl'] as String?, createdAt: json['createdAt'] == null ? null @@ -20,5 +23,8 @@ Map _$$TodoModelImplToJson(_$TodoModelImpl instance) => { 'id': instance.id, 'title': instance.title, + 'isDone': instance.isDone, + 'description': instance.description, + 'imageUrl': instance.imageUrl, 'createdAt': instance.createdAt?.toIso8601String(), }; diff --git a/lib/features/todo/presentation/cubits/todo_cubit.dart b/lib/features/todo/presentation/cubits/todo_cubit.dart index f1805f4..0c018f0 100644 --- a/lib/features/todo/presentation/cubits/todo_cubit.dart +++ b/lib/features/todo/presentation/cubits/todo_cubit.dart @@ -21,13 +21,37 @@ class TodoCubit extends Cubit { } } - Future addTodo(String title) async { + Future addTodo( + String title, + String? description, + String? imageUrl, + ) async { try { emit(TodosLoading()); - await _repository.addTodo(title); + await _repository.addTodo(title, description, imageUrl); await loadTodos(); } catch (e) { emit(TodosError(message: e.toString())); } } + + Future updateStatus(String id, bool isDone) async { + try { + await _repository.updateStatus(id, isDone); + + // optimistic update to prevent reloading and rendering the entire list + if (state is TodosLoaded) { + final updatedItems = + (state as TodosLoaded).todos.map((item) { + return item.id == id ? item.copyWith(isDone: isDone) : item; + }).toList(); + + emit(TodosLoaded(todos: updatedItems)); + } else { + loadTodos(); + } + } catch (e) { + emit(TodosError(message: e.toString())); + } + } } diff --git a/lib/features/todo/presentation/screens/todo_form_screen.dart b/lib/features/todo/presentation/screens/todo_form_screen.dart index cf54e93..59c602c 100644 --- a/lib/features/todo/presentation/screens/todo_form_screen.dart +++ b/lib/features/todo/presentation/screens/todo_form_screen.dart @@ -13,16 +13,24 @@ class TodoFormScreen extends StatefulWidget { class _TodoFormScreenState extends State { final _formKey = GlobalKey(); final _titleController = TextEditingController(); + final _descriptionController = TextEditingController(); + final _imageUrlController = TextEditingController(); @override void dispose() { _titleController.dispose(); + _descriptionController.dispose(); + _imageUrlController.dispose(); super.dispose(); } void _submitForm() { if (_formKey.currentState?.validate() ?? false) { - context.read().addTodo(_titleController.text); + context.read().addTodo( + _titleController.text, + _descriptionController.text, + _imageUrlController.text, + ); Navigator.pop(context); } } @@ -41,7 +49,10 @@ class _TodoFormScreenState extends State { const SizedBox(height: 16), TextFormField( controller: _titleController, - decoration: const InputDecoration(labelText: 'Title', border: OutlineInputBorder()), + decoration: const InputDecoration( + labelText: 'Title', + border: OutlineInputBorder(), + ), validator: (value) { if (value == null || value.isEmpty) { return 'Please enter a title'; @@ -49,10 +60,30 @@ class _TodoFormScreenState extends State { return null; }, ), + const SizedBox(height: 12), + TextFormField( + controller: _descriptionController, + decoration: const InputDecoration( + labelText: 'Description (optional)', + border: OutlineInputBorder(), + ), + keyboardType: TextInputType.multiline, + maxLength: 1000, + ), + const SizedBox(height: 12), + TextFormField( + controller: _imageUrlController, + decoration: const InputDecoration( + labelText: 'Image URL (optional)', + border: OutlineInputBorder(), + ), + ), const SizedBox(height: 24), ElevatedButton( onPressed: _submitForm, - style: ElevatedButton.styleFrom(padding: const EdgeInsets.symmetric(vertical: 16)), + style: ElevatedButton.styleFrom( + padding: const EdgeInsets.symmetric(vertical: 16), + ), child: const Text('Add Todo'), ), ], diff --git a/lib/features/todo/presentation/widgets/todo_item.dart b/lib/features/todo/presentation/widgets/todo_item.dart index 0930dea..d2a1abc 100644 --- a/lib/features/todo/presentation/widgets/todo_item.dart +++ b/lib/features/todo/presentation/widgets/todo_item.dart @@ -1,4 +1,6 @@ import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:flutter_todo_workshop/features/todo/presentation/cubits/todo_cubit.dart'; import '../../domain/models/todo_model.dart'; @@ -9,21 +11,75 @@ class TodoItem extends StatelessWidget { @override Widget build(BuildContext context) { + final todoCubit = context.read(); + final bool hasDescription = + todo.description != null && todo.description!.isNotEmpty; + final bool hasImage = todo.imageUrl != null && todo.imageUrl!.isNotEmpty; + return Card( elevation: 2, margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 4), - child: Padding( - padding: const EdgeInsets.all(12.0), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text(todo.title, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), - const SizedBox(height: 4), - Text( - todo.formattedDate, - style: TextStyle(fontSize: 12, color: Colors.grey[600], fontStyle: FontStyle.italic), - ), - ], + child: InkWell( + onTap: () { + todoCubit.updateStatus(todo.id, !todo.isDone); + }, + child: Padding( + padding: const EdgeInsets.all(12.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + spacing: 4, + children: [ + todo.isDone + ? Icon( + Icons.check_box_outlined, + color: Colors.green, + size: 24.0, + semanticLabel: 'Status: Done', + ) + : const Icon( + Icons.check_box_outline_blank, + size: 24.0, + semanticLabel: 'Status: To do', + ), + Text( + todo.title, + style: const TextStyle( + fontSize: 18, + fontWeight: FontWeight.bold, + ), + ), + ], + ), + hasDescription + ? Text( + todo.description ?? '', + style: const TextStyle(fontSize: 14), + maxLines: 2, + overflow: TextOverflow.ellipsis, + ) + : const SizedBox.shrink(), + const SizedBox(height: 4), + hasImage + ? Image.network( + todo.imageUrl!, + height: 100, + width: 100, + fit: BoxFit.cover, + ) + : const SizedBox.shrink(), + const SizedBox(height: 4), + Text( + todo.formattedDate, + style: TextStyle( + fontSize: 12, + color: Colors.grey[600], + fontStyle: FontStyle.italic, + ), + ), + ], + ), ), ), );