Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
primera_practica
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
Yenisleydi Benitez Martinez
primera_practica
Commits
f518b35c
Commit
f518b35c
authored
Aug 05, 2024
by
yenisleydi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tarea 12
parent
6141e6e7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
186 additions
and
17 deletions
+186
-17
article_search_delegate.dart
lib/src/delegates/article_search_delegate.dart
+2
-2
articles_api.dart
lib/src/http_api/articles_api.dart
+35
-1
category_service.dart
lib/src/http_api/category_service.dart
+3
-3
carrito_model.dart
lib/src/models/carrito_model.dart
+1
-1
articles_page.dart
lib/src/pages/articles_page.dart
+21
-8
formulario_articulos.dart
lib/src/pages/formulario_articulos.dart
+122
-0
home_page.dart
lib/src/pages/home_page.dart
+2
-2
No files found.
lib/src/delegates/article_search_delegate.dart
View file @
f518b35c
...
...
@@ -70,8 +70,8 @@ class ArticleSearchDelegate extends SearchDelegate {
icon:
Icon
(
Icons
.
shopping_cart
),
onPressed:
()
{
final
carritoProvider
=
Provider
.
of
<
CarritoProvider
>(
context
,
listen:
false
);
carritoProvider
.
agregarCarrito
(
article
);
// Llama al método correcto
close
(
context
,
null
);
// Cierra el SearchDelegate
carritoProvider
.
agregarCarrito
(
article
);
close
(
context
,
null
);
},
),
);
...
...
lib/src/http_api/articles_api.dart
View file @
f518b35c
import
'package:flutter/foundation.dart'
;
import
'package:http/http.dart'
as
http
;
import
'package:primer_practica/environments/urls.dart'
as
api
;
import
'dart:convert'
;
class
ArticlesApi
{
Future
<
Map
<
String
,
dynamic
>>
getArticles
()
async
{
...
...
@@ -19,7 +20,7 @@ class ArticlesApi {
}
Future
<
Map
<
String
,
dynamic
>>
searchArticles
(
String
query
)
async
{
String
url
=
'
${api.apiApp}
/articulo?nombre="
$query
"'
;
String
url
=
'
${api.apiApp}
/articulo?nombre="
$query
"'
;
if
(
kDebugMode
)
{
print
(
'Url ->
$url
'
);
...
...
@@ -32,4 +33,37 @@ class ArticlesApi {
return
{
"statusCode"
:
501
,
"body"
:
'
$e
'
};
}
}
Future
<
Map
<
String
,
dynamic
>>
addArticle
({
required
String
clave
,
required
int
categoria
,
required
String
nombre
,
required
List
<
double
>
precios
,
required
bool
activo
,
})
async
{
String
url
=
'
${api.apiApp}
/articulo'
;
final
payload
=
{
'clave'
:
clave
,
'categoria'
:
categoria
,
'nombre'
:
nombre
,
'precios'
:
precios
.
map
((
precio
)
=>
{
'precio'
:
precio
}).
toList
(),
'activo'
:
activo
,
};
if
(
kDebugMode
)
{
print
(
'Url ->
$url
'
);
print
(
'Payload ->
$payload
'
);
}
try
{
final
resp
=
await
http
.
post
(
Uri
.
parse
(
url
),
headers:
{
'Content-Type'
:
'application/json'
},
body:
json
.
encode
(
payload
),
);
return
{
"statusCode"
:
resp
.
statusCode
,
"body"
:
resp
.
body
};
}
catch
(
e
)
{
return
{
"statusCode"
:
501
,
"body"
:
'
$e
'
};
}
}
}
lib/src/http_api/category_service.dart
View file @
f518b35c
...
...
@@ -36,7 +36,7 @@ class CategoryService {
//conversión del objeto a Json
final
categoryJson
=
category
.
toJson
();
// Imprime la URL y el cuerpo de la solicitud
if
(
kDebugMode
)
{
print
(
'Url ->
$url
'
);
print
(
'Body ->
$categoryJson
'
);
...
...
@@ -45,8 +45,8 @@ class CategoryService {
try
{
// Realiza la solicitud POST al servidor
final
response
=
await
http
.
post
(
Uri
.
parse
(
url
),
// Convierte la URL a un objeto Uri
headers:
{
'Content-Type'
:
'application/json'
},
// Establece el tipo de contenido como JSON
Uri
.
parse
(
url
),
headers:
{
'Content-Type'
:
'application/json'
},
body:
json
.
encode
(
categoryJson
),
);
...
...
lib/src/models/carrito_model.dart
View file @
f518b35c
...
...
@@ -23,7 +23,7 @@ class CarritoModel {
return
CarritoModel
(
articulo:
ArticlesModel
.
fromJson
(
json
[
'articulo'
]),
cantidad:
json
[
'cantidad'
],
precio:
json
[
'precio'
],
// Asegúrate de que se maneje correctamente el precio
precio:
json
[
'precio'
],
);
}
}
lib/src/pages/articles_page.dart
View file @
f518b35c
import
'package:flutter/material.dart'
;
import
'package:primer_practica/src/controllers/articles_controller.dart'
;
import
'package:primer_practica/src/models/articles_model.dart'
;
import
'package:primer_practica/src/pages/formulario_articulos.dart'
;
class
ArticlePage
extends
StatefulWidget
{
final
int
categoryId
;
const
ArticlePage
({
Key
?
key
,
required
this
.
categoryId
})
:
super
(
key:
key
);
@override
_ArticlePageState
createState
()
=>
_ArticlePageState
();
}
class
_ArticlePageState
extends
State
<
ArticlePage
>
{
final
ArticleController
_articleController
=
ArticleController
();
late
Future
<
Map
<
String
,
dynamic
>>
_articlesFuture
;
@override
...
...
@@ -30,15 +30,14 @@ class _ArticlePageState extends State<ArticlePage> {
backgroundColor:
Colors
.
indigoAccent
,
foregroundColor:
Colors
.
white
,
),
body:
FutureBuilder
<
Map
<
String
,
dynamic
>>(
future:
_articlesFuture
,
builder:
(
context
,
snapshot
)
{
if
(
snapshot
.
hasData
)
{
if
(
snapshot
.
data
![
'ok'
])
{
if
(
snapshot
.
data
![
'data'
]
!=
null
&&
(
snapshot
.
data
![
'data'
]
as
List
).
isNotEmpty
)
{
List
<
ArticlesModel
>
articles
=
snapshot
.
data
![
'data'
]
as
List
<
ArticlesModel
>
;
//
Lista de arti
culos
List
<
ArticlesModel
>
articles
=
(
snapshot
.
data
![
'data'
]
as
List
).
cast
<
ArticlesModel
>()
;
//
Lista de artí
culos
return
ListView
.
builder
(
itemCount:
articles
.
length
,
itemBuilder:
(
context
,
index
)
{
...
...
@@ -47,7 +46,6 @@ class _ArticlePageState extends State<ArticlePage> {
child:
ListTile
(
title:
Text
(
article
.
nombre
),
subtitle:
Text
(
'id :
${article.categoriaId}
'
),
//subtitle: Text('Clave: ${article.clave}'),
trailing:
Text
(
'
\$
${article.precios.isNotEmpty ? article.precios.first.precio.toStringAsFixed(2) : 'N/A'}
'
),
),
);
...
...
@@ -66,6 +64,22 @@ class _ArticlePageState extends State<ArticlePage> {
}
},
),
floatingActionButton:
FloatingActionButton
(
onPressed:
()
async
{
final
result
=
await
Navigator
.
push
(
context
,
MaterialPageRoute
(
builder:
(
context
)
=>
FormularioArticulos
()),
);
if
(
result
==
true
)
{
setState
(()
{
_articlesFuture
=
_articleController
.
getArticles
(
widget
.
categoryId
);
});
}
},
child:
const
Icon
(
Icons
.
add
),
tooltip:
'Agregar artículo'
,
),
floatingActionButtonLocation:
FloatingActionButtonLocation
.
endFloat
,
);
}
}
\ No newline at end of file
}
lib/src/pages/formulario_articulos.dart
0 → 100644
View file @
f518b35c
import
'package:flutter/material.dart'
;
import
'package:primer_practica/src/http_api/articles_api.dart'
;
// Asegúrate de importar la clase correcta
class
FormularioArticulos
extends
StatefulWidget
{
@override
_FormularioArticulosState
createState
()
=>
_FormularioArticulosState
();
}
class
_FormularioArticulosState
extends
State
<
FormularioArticulos
>
{
final
_formKey
=
GlobalKey
<
FormState
>();
String
_clave
=
''
;
int
_categoria
=
1
;
String
_nombre
=
''
;
List
<
double
>
_precios
=
[
0.0
,
0.0
];
bool
_activo
=
true
;
final
ArticlesApi
_articlesApi
=
ArticlesApi
();
Future
<
void
>
_guardarArticulo
()
async
{
if
(
_formKey
.
currentState
!.
validate
())
{
_formKey
.
currentState
!.
save
();
final
result
=
await
_articlesApi
.
addArticle
(
clave:
_clave
,
categoria:
_categoria
,
nombre:
_nombre
,
precios:
_precios
,
activo:
_activo
,
);
if
(
result
[
'statusCode'
]
==
200
)
{
ScaffoldMessenger
.
of
(
context
).
showSnackBar
(
SnackBar
(
content:
Text
(
'Artículo guardado exitosamente'
)),
);
Navigator
.
pop
(
context
);
// Regresa a la lista de artículos
}
else
{
ScaffoldMessenger
.
of
(
context
).
showSnackBar
(
SnackBar
(
content:
Text
(
'Error al guardar el artículo'
)),
);
}
}
}
@override
Widget
build
(
BuildContext
context
)
{
return
Scaffold
(
appBar:
AppBar
(
title:
Text
(
'Formulario de Artículo'
),
),
body:
Padding
(
padding:
EdgeInsets
.
all
(
16.0
),
child:
Form
(
key:
_formKey
,
child:
ListView
(
children:
[
_buildTextField
(
label:
'Clave'
,
onSaved:
(
value
)
=>
_clave
=
value
!,
inputType:
TextInputType
.
text
,
),
_buildTextField
(
label:
'Categoría'
,
onSaved:
(
value
)
=>
_categoria
=
int
.
parse
(
value
!),
inputType:
TextInputType
.
number
,
),
_buildTextField
(
label:
'Nombre'
,
onSaved:
(
value
)
=>
_nombre
=
value
!,
inputType:
TextInputType
.
text
,
),
_buildTextField
(
label:
'Precio 1'
,
onSaved:
(
value
)
=>
_precios
[
0
]
=
double
.
parse
(
value
!),
inputType:
TextInputType
.
number
,
),
_buildTextField
(
label:
'Precio 2'
,
onSaved:
(
value
)
=>
_precios
[
1
]
=
double
.
parse
(
value
!),
inputType:
TextInputType
.
number
,
),
SizedBox
(
height:
30
),
Center
(
child:
ElevatedButton
(
onPressed:
_guardarArticulo
,
style:
ElevatedButton
.
styleFrom
(
padding:
EdgeInsets
.
symmetric
(
horizontal:
24.0
,
vertical:
12.0
),
textStyle:
TextStyle
(
fontSize:
16
),
),
child:
Text
(
'Guardar'
),
),
),
],
),
),
),
);
}
Widget
_buildTextField
({
required
String
label
,
required
FormFieldSetter
<
String
>
onSaved
,
required
TextInputType
inputType
,
})
{
return
Padding
(
padding:
const
EdgeInsets
.
symmetric
(
vertical:
12.0
),
child:
TextFormField
(
decoration:
InputDecoration
(
labelText:
label
,
border:
OutlineInputBorder
(),
contentPadding:
EdgeInsets
.
symmetric
(
vertical:
15
,
horizontal:
10
),
),
keyboardType:
inputType
,
onSaved:
onSaved
,
validator:
(
value
)
{
if
(
value
==
null
||
value
.
isEmpty
)
{
return
'Este campo es obligatorio'
;
}
return
null
;
},
),
);
}
}
lib/src/pages/home_page.dart
View file @
f518b35c
import
'package:flutter/material.dart'
;
import
'package:primer_practica/src/pages/category/lista_categorias.dart'
;
import
'package:primer_practica/custom/custom_app_bar.dart'
;
import
'package:primer_practica/src/providers/carrito_providers.dart'
;
import
'package:provider/provider.dart'
;
//
import 'package:primer_practica/src/providers/carrito_providers.dart';
//
import 'package:provider/provider.dart';
class
HomePage
extends
StatelessWidget
{
const
HomePage
({
Key
?
key
})
:
super
(
key:
key
);
...
...
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