11import 'package:animate_do/animate_do.dart' ;
2+ import 'package:async_button/async_button.dart' ;
3+ import 'package:cloud_firestore/cloud_firestore.dart' ;
24import 'package:flutter/material.dart' ;
35import 'package:flutter_gen/gen_l10n/app_localizations.dart' ;
6+ import 'package:provider/provider.dart' ;
47import 'package:todo_app/core/base/base_singleton.dart' ;
58import 'package:todo_app/core/extensions/ui_extensions.dart' ;
9+ import 'package:todo_app/uikit/button/special_async_button.dart' ;
610import 'package:todo_app/uikit/button/special_button.dart' ;
711import 'package:todo_app/uikit/decoration/special_container_decoration.dart' ;
812import 'package:todo_app/uikit/textformfield/default_text_form_field.dart' ;
13+ import 'package:uuid/uuid.dart' ;
14+
15+ import '../../viewmodels/todo_view_model.dart' ;
916
1017class AddTodoView extends StatelessWidget with BaseSingleton {
11- const AddTodoView ({super .key});
18+ final _formKey = GlobalKey <FormState >();
19+ final _titleController = TextEditingController ();
20+ final _subtitleController = TextEditingController ();
21+
22+ AddTodoView ({super .key});
1223
1324 @override
1425 Widget build (BuildContext context) {
@@ -17,13 +28,16 @@ class AddTodoView extends StatelessWidget with BaseSingleton {
1728 title: _appBarTitle (context),
1829 ),
1930 body: FadeInLeft (
20- child: ListView (
21- padding: context.padding2x,
22- children: [
23- _descriptionsSection (context),
24- context.emptySizedHeightBox3x,
25- _fieldsAndAddTodoButtonContainer (context)
26- ],
31+ child: Form (
32+ key: _formKey,
33+ child: ListView (
34+ padding: context.padding2x,
35+ children: [
36+ _descriptionsSection (context),
37+ context.emptySizedHeightBox3x,
38+ _fieldsAndAddTodoButtonContainer (context)
39+ ],
40+ ),
2741 ),
2842 ),
2943 );
@@ -117,6 +131,8 @@ class AddTodoView extends StatelessWidget with BaseSingleton {
117131 fillColor: colors.grey1,
118132 context: context,
119133 labelText: AppLocalizations .of (context)! .todoTitleLabel,
134+ controller: _titleController,
135+ validator: (title) => validators.titleCheck (title),
120136 );
121137 }
122138
@@ -127,14 +143,38 @@ class AddTodoView extends StatelessWidget with BaseSingleton {
127143 fillColor: colors.grey1,
128144 context: context,
129145 labelText: AppLocalizations .of (context)! .todoSubtitleLabel,
146+ controller: _subtitleController,
147+ validator: (subtitle) => validators.subtitleCheck (subtitle),
130148 );
131149 }
132150
133- SizedBox _addNewTodoButton (BuildContext context) {
151+ Widget _addNewTodoButton (BuildContext context) {
134152 bool isHasIcon = true ;
135153 return SizedBox (
136154 width: context.maxFinite,
137- child: SpecialButton (
155+ child: SpecialAsyncButton (
156+ onTap: (btnStateController) async {
157+ btnStateController.update (ButtonState .loading);
158+ _formKey.currentState! .save ();
159+ if (_formKey.currentState! .validate ()) {
160+ final pv = Provider .of <TodoViewModel >(context, listen: false );
161+ var uuid = const Uuid ();
162+ Map <String , dynamic > obj = {
163+ "id" : uuid.v1 (),
164+ "isActive" : true ,
165+ "isDone" : false ,
166+ "title" : _titleController.text,
167+ "subtitle" : _subtitleController.text,
168+ "createdAt" : Timestamp .now (),
169+ };
170+ int statusCode = await pv.addTodo (obj: obj);
171+ statusCode == 200
172+ ? btnStateController.update (ButtonState .success)
173+ : btnStateController.update (ButtonState .failure);
174+ } else {
175+ btnStateController.update (ButtonState .failure);
176+ }
177+ },
138178 buttonLabel: AppLocalizations .of (context)! .addNewTodoButton,
139179 borderRadius: context.borderRadius3x,
140180 padding: context.padding2x,
@@ -143,4 +183,36 @@ class AddTodoView extends StatelessWidget with BaseSingleton {
143183 ),
144184 );
145185 }
186+
187+ // SizedBox _addNewTodoButton(BuildContext context) {
188+ // bool isHasIcon = true;
189+ // return SizedBox(
190+ // width: context.maxFinite,
191+ // child: SpecialButton(
192+ // buttonLabel: AppLocalizations.of(context)!.addNewTodoButton,
193+ // borderRadius: context.borderRadius3x,
194+ // padding: context.padding2x,
195+ // isHasIcon: isHasIcon,
196+ // icon: Icons.add,
197+ // onTap: () async {
198+ // _formKey.currentState!.save();
199+ // if (_formKey.currentState!.validate()) {
200+ // final pv = Provider.of<TodoViewModel>(context, listen: false);
201+ // var uuid = const Uuid();
202+ // Map<String, dynamic> obj = {
203+ // "id": uuid.v1(),
204+ // "isActive": true,
205+ // "isDone": false,
206+ // "title": _titleController.text,
207+ // "subtitle": _subtitleController.text,
208+ // "createdAt": Timestamp.now(),
209+ // };
210+ // await pv.addTodo(obj: obj);
211+ // } else {
212+
213+ // }
214+ // },
215+ // ),
216+ // );
217+ // }
146218}
0 commit comments