Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
Practica_Flutter_E
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Efren David Jimenez Martinez
Practica_Flutter_E
Commits
a3c2ebea
Commit
a3c2ebea
authored
Aug 02, 2024
by
Efren David Jimenez Martinez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Subiendo actividad 8 Formulario categoria
parent
3d1df331
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
13 deletions
+67
-13
category_service.dart
lib/src/controllers/category_service.dart
+28
-0
category_form.dart
lib/src/pages/category_form.dart
+38
-13
home_page.dart
lib/src/pages/home_page.dart
+1
-0
No files found.
lib/src/controllers/category_service.dart
0 → 100644
View file @
a3c2ebea
import
'dart:convert'
;
import
'package:http/http.dart'
as
http
;
import
'package:practica1_flutter/src/models/category_model.dart'
;
class
CategoryService
{
static
const
String
_baseUrl
=
'https://basic2.visorus.com.mx'
;
Future
<
Map
<
String
,
dynamic
>>
addCategory
(
CategoryModel
category
)
async
{
final
String
url
=
'
$_baseUrl
/categoria'
;
final
Map
<
String
,
dynamic
>
categoryData
=
category
.
toJson
();
try
{
final
http
.
Response
response
=
await
http
.
post
(
Uri
.
parse
(
url
),
body:
json
.
encode
({
"data"
:
[
categoryData
]}),
headers:
{
'Content-Type'
:
'application/json'
},
);
if
(
response
.
statusCode
==
200
)
{
return
{
"statusCode"
:
response
.
statusCode
,
"body"
:
json
.
decode
(
response
.
body
)};
}
else
{
return
{
"statusCode"
:
response
.
statusCode
,
"body"
:
response
.
body
};
}
}
catch
(
e
)
{
return
{
"statusCode"
:
501
,
"body"
:
'
$e
'
};
}
}
}
lib/src/pages/category_form.dart
View file @
a3c2ebea
import
'package:flutter/material.dart'
;
import
'package:flutter/material.dart'
;
class
CategoryForm
extends
StatefulWidget
{
class
CategoryForm
extends
StatelessWidget
{
@override
final
GlobalKey
<
FormState
>
_formKey
=
GlobalKey
<
FormState
>();
_NewCategoryFormState
createState
()
=>
_NewCategoryFormState
();
}
class
_NewCategoryFormState
extends
State
<
CategoryForm
>
{
final
_formKey
=
GlobalKey
<
FormState
>();
final
TextEditingController
_claveController
=
TextEditingController
();
final
TextEditingController
_claveController
=
TextEditingController
();
final
TextEditingController
_fechaCreadoController
=
TextEditingController
();
final
TextEditingController
_fechaCreadoController
=
TextEditingController
();
final
TextEditingController
_nombreController
=
TextEditingController
();
final
TextEditingController
_nombreController
=
TextEditingController
();
// Función para obtener la fecha en milisegundos
int
_getFechaCreadoEnMilisegundos
(
String
fecha
)
{
try
{
// convertimos el String a DateTime y luego obtenemos el tiempo en milisegundos
DateTime
fechaCreado
=
DateTime
.
parse
(
fecha
);
return
fechaCreado
.
millisecondsSinceEpoch
;
}
catch
(
e
)
{
// Si hay un error en la conversión, devolvemos 0
return
0
;
}
}
@override
@override
Widget
build
(
BuildContext
context
)
{
Widget
build
(
BuildContext
context
)
{
return
Scaffold
(
return
Scaffold
(
appBar:
AppBar
(
appBar:
AppBar
(
title:
const
Text
(
'Agregar Categoría'
),
title:
const
Text
(
'Agregar Categoría'
),
backgroundColor:
Colors
.
red
),
),
body:
Padding
(
body:
Padding
(
padding:
const
EdgeInsets
.
all
(
16.0
),
padding:
const
EdgeInsets
.
all
(
16.0
),
...
@@ -25,7 +33,9 @@ class _NewCategoryFormState extends State<CategoryForm> {
...
@@ -25,7 +33,9 @@ class _NewCategoryFormState extends State<CategoryForm> {
children:
[
children:
[
TextFormField
(
TextFormField
(
controller:
_claveController
,
controller:
_claveController
,
decoration:
InputDecoration
(
labelText:
'Clave'
),
decoration:
InputDecoration
(
labelText:
'Clave'
,
border:
OutlineInputBorder
()
),
validator:
(
value
)
{
validator:
(
value
)
{
if
(
value
==
null
||
value
.
isEmpty
)
{
if
(
value
==
null
||
value
.
isEmpty
)
{
return
'Por favor ingrese la clave'
;
return
'Por favor ingrese la clave'
;
...
@@ -33,20 +43,35 @@ class _NewCategoryFormState extends State<CategoryForm> {
...
@@ -33,20 +43,35 @@ class _NewCategoryFormState extends State<CategoryForm> {
return
null
;
return
null
;
},
},
),
),
SizedBox
(
height:
20
),
TextFormField
(
TextFormField
(
controller:
_fechaCreadoController
,
controller:
_fechaCreadoController
,
decoration:
InputDecoration
(
labelText:
'Fecha Creado (milisegundos)'
),
decoration:
InputDecoration
(
labelText:
'Fecha Creado (YYYY-MM-DD)'
,
border:
OutlineInputBorder
()),
validator:
(
value
)
{
validator:
(
value
)
{
if
(
value
==
null
||
value
.
isEmpty
)
{
if
(
value
==
null
||
value
.
isEmpty
)
{
return
'Por favor ingrese la fecha en milisegundos'
;
return
'Por favor ingrese la fecha en formato YYYY-MM-DD'
;
}
// Validación de la fecha
try
{
DateTime
.
parse
(
value
);
}
catch
(
e
)
{
return
'Por favor ingrese una fecha válida'
;
}
}
return
null
;
return
null
;
},
},
keyboardType:
TextInputType
.
number
,
),
),
SizedBox
(
height:
20
),
TextFormField
(
TextFormField
(
//padding: const EdgeInsets.symmetric(horizontal: 80.0), // añado mi margen horizontal
controller:
_nombreController
,
controller:
_nombreController
,
decoration:
InputDecoration
(
labelText:
'Nombre'
),
decoration:
InputDecoration
(
labelText:
'Nombre'
,
border:
OutlineInputBorder
(),
),
validator:
(
value
)
{
validator:
(
value
)
{
if
(
value
==
null
||
value
.
isEmpty
)
{
if
(
value
==
null
||
value
.
isEmpty
)
{
return
'Por favor ingrese el nombre'
;
return
'Por favor ingrese el nombre'
;
...
@@ -61,7 +86,7 @@ class _NewCategoryFormState extends State<CategoryForm> {
...
@@ -61,7 +86,7 @@ class _NewCategoryFormState extends State<CategoryForm> {
// Guardar los valores y mostrar mensaje en consola
// Guardar los valores y mostrar mensaje en consola
print
({
print
({
"clave"
:
_claveController
.
text
,
"clave"
:
_claveController
.
text
,
"fechaCreado"
:
int
.
parse
(
_fechaCreadoController
.
text
),
"fechaCreado"
:
_getFechaCreadoEnMilisegundos
(
_fechaCreadoController
.
text
),
"nombre"
:
_nombreController
.
text
,
"nombre"
:
_nombreController
.
text
,
});
});
}
}
...
...
lib/src/pages/home_page.dart
View file @
a3c2ebea
...
@@ -12,6 +12,7 @@ class HomeSellerPage extends StatelessWidget {
...
@@ -12,6 +12,7 @@ class HomeSellerPage extends StatelessWidget {
child:
Scaffold
(
child:
Scaffold
(
appBar:
AppBar
(
appBar:
AppBar
(
title:
const
Text
(
"ShopMaster"
),
title:
const
Text
(
"ShopMaster"
),
centerTitle:
true
,
foregroundColor:
Colors
.
white
,
foregroundColor:
Colors
.
white
,
backgroundColor:
Colors
.
indigoAccent
,
backgroundColor:
Colors
.
indigoAccent
,
bottom:
const
TabBar
(
bottom:
const
TabBar
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment