Skip to content

Commit

Permalink
add 요청시 progress 추가 #15
Browse files Browse the repository at this point in the history
  • Loading branch information
korca0220 committed Mar 14, 2021
1 parent 30299c5 commit 776c2ed
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 146 deletions.
4 changes: 2 additions & 2 deletions shop_app/lib/providers/products.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class Products with ChangeNotifier {
return _items.firstWhere((element) => element.id == id);
}

void addProduct(Product product) {
Future<void> addProduct(Product product) {
const url = '';
http
return http
.post(Uri.parse(url),
body: json.encode({
'title': product.title,
Expand Down
307 changes: 163 additions & 144 deletions shop_app/lib/screens/edit_product_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class _EditProductScreenState extends State<EditProductScreen> {
'imageUrl': '',
};
bool _isInit = true;
bool _isLoading = false;

@override
void dispose() {
Expand All @@ -50,11 +51,25 @@ class _EditProductScreenState extends State<EditProductScreen> {
}
// save 메쇠드 사용시 각 TextFormFiled 의 onSave가 동작
_formKey.currentState.save();
setState(() {
_isLoading = true;
});
if (_editedProduct.id != null) {
Provider.of<Products>(context, listen: false)
.updateProduct(_editedProduct.id, _editedProduct);
Navigator.of(context).pop();
setState(() {
_isLoading = false;
});
}
Provider.of<Products>(context, listen: false).addProduct(_editedProduct);
Provider.of<Products>(context, listen: false)
.addProduct(_editedProduct)
.then(((_) {
Navigator.of(context).pop();
setState(() {
_isLoading = false;
});
}));
}

@override
Expand Down Expand Up @@ -87,170 +102,174 @@ class _EditProductScreenState extends State<EditProductScreen> {
icon: Icon(Icons.save),
onPressed: () {
_saveForm();
Navigator.of(context).pop();
}),
],
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: ListView(
children: <Widget>[
TextFormField(
initialValue: _initValue['title'],
decoration: InputDecoration(labelText: 'Title'),
textInputAction: TextInputAction.next,
onFieldSubmitted: (_) {
FocusScope.of(context).requestFocus(_priceFocusNode);
},
validator: (value) {
if (value.isEmpty) {
return 'Please provide a value';
}
return null;
},
onSaved: (value) {
_editedProduct = Product(
title: value,
price: _editedProduct.price,
description: _editedProduct.description,
imageUrl: _editedProduct.imageUrl,
id: _editedProduct.id,
isFavorite: _editedProduct.isFavorite,
);
},
),
TextFormField(
initialValue: _initValue['price'],
decoration: InputDecoration(labelText: 'Price'),
textInputAction: TextInputAction.next,
keyboardType: TextInputType.number,
focusNode: _priceFocusNode,
onFieldSubmitted: (_) {
FocusScope.of(context).requestFocus(_descriptionFocusNode);
},
validator: (value) {
if (value.isEmpty) {
return 'Please provide a price';
}
if (double.tryParse(value) == null) {
return 'Please enter a valid number';
}
if (double.parse(value) <= 0) {
return 'Please enater a number greater than zero';
}
return null;
},
onSaved: (value) {
_editedProduct = Product(
title: _editedProduct.title,
price: double.parse(value),
description: _editedProduct.description,
imageUrl: _editedProduct.imageUrl,
id: _editedProduct.id,
isFavorite: _editedProduct.isFavorite,
);
},
),
TextFormField(
initialValue: _initValue['description'],
decoration: InputDecoration(labelText: 'Description'),
maxLines: 3,
textInputAction: TextInputAction.next,
keyboardType: TextInputType.multiline,
focusNode: _descriptionFocusNode,
onFieldSubmitted: (_) {
FocusScope.of(context).requestFocus(_imageUrlFocusNode);
},
validator: (value) {
if (value.isEmpty) {
return 'Please provide a description';
}
if (value.length < 10) {
return 'Should be at least 10 characters long';
}
return null;
},
onSaved: (value) {
_editedProduct = Product(
title: _editedProduct.title,
price: _editedProduct.price,
description: value,
imageUrl: _editedProduct.imageUrl,
id: _editedProduct.id,
isFavorite: _editedProduct.isFavorite,
);
},
),
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Container(
width: 100,
height: 100,
margin: EdgeInsets.only(top: 8, right: 10),
decoration: BoxDecoration(
border: Border.all(
width: 1,
color: Colors.grey,
),
body: _isLoading
? Center(
child: CircularProgressIndicator(),
)
: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: ListView(
children: <Widget>[
TextFormField(
initialValue: _initValue['title'],
decoration: InputDecoration(labelText: 'Title'),
textInputAction: TextInputAction.next,
onFieldSubmitted: (_) {
FocusScope.of(context).requestFocus(_priceFocusNode);
},
validator: (value) {
if (value.isEmpty) {
return 'Please provide a value';
}
return null;
},
onSaved: (value) {
_editedProduct = Product(
title: value,
price: _editedProduct.price,
description: _editedProduct.description,
imageUrl: _editedProduct.imageUrl,
id: _editedProduct.id,
isFavorite: _editedProduct.isFavorite,
);
},
),
child: _imageUrlController.text.isEmpty
? Text(
'Enter a URL',
textAlign: TextAlign.center,
)
: FittedBox(
child: Image.network(
_imageUrlController.text,
fit: BoxFit.cover,
),
),
),
Expanded(
child: TextFormField(
decoration: InputDecoration(labelText: 'Image URL'),
keyboardType: TextInputType.url,
textInputAction: TextInputAction.done,
controller: _imageUrlController,
focusNode: _imageUrlFocusNode,
onEditingComplete: () {
setState(() {});
TextFormField(
initialValue: _initValue['price'],
decoration: InputDecoration(labelText: 'Price'),
textInputAction: TextInputAction.next,
keyboardType: TextInputType.number,
focusNode: _priceFocusNode,
onFieldSubmitted: (_) {
FocusScope.of(context)
.requestFocus(_descriptionFocusNode);
},
validator: (value) {
if (value.isEmpty) {
return 'Please enter an image URL';
return 'Please provide a price';
}
if (!value.startsWith('http') &&
!value.startsWith('https')) {
return 'Please enter a valid URL';
if (double.tryParse(value) == null) {
return 'Please enter a valid number';
}
if (!value.endsWith('.png') &&
!value.endsWith('.jpg') &&
!value.endsWith('.jpeg')) {
return 'Please enter a valid image URL';
if (double.parse(value) <= 0) {
return 'Please enater a number greater than zero';
}
return null;
},
onSaved: (value) {
_editedProduct = Product(
title: _editedProduct.title,
price: _editedProduct.price,
price: double.parse(value),
description: _editedProduct.description,
imageUrl: value,
imageUrl: _editedProduct.imageUrl,
id: _editedProduct.id,
isFavorite: _editedProduct.isFavorite,
);
},
),
)
],
)
],
),
),
),
TextFormField(
initialValue: _initValue['description'],
decoration: InputDecoration(labelText: 'Description'),
maxLines: 3,
textInputAction: TextInputAction.next,
keyboardType: TextInputType.multiline,
focusNode: _descriptionFocusNode,
onFieldSubmitted: (_) {
FocusScope.of(context).requestFocus(_imageUrlFocusNode);
},
validator: (value) {
if (value.isEmpty) {
return 'Please provide a description';
}
if (value.length < 10) {
return 'Should be at least 10 characters long';
}
return null;
},
onSaved: (value) {
_editedProduct = Product(
title: _editedProduct.title,
price: _editedProduct.price,
description: value,
imageUrl: _editedProduct.imageUrl,
id: _editedProduct.id,
isFavorite: _editedProduct.isFavorite,
);
},
),
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Container(
width: 100,
height: 100,
margin: EdgeInsets.only(top: 8, right: 10),
decoration: BoxDecoration(
border: Border.all(
width: 1,
color: Colors.grey,
),
),
child: _imageUrlController.text.isEmpty
? Text(
'Enter a URL',
textAlign: TextAlign.center,
)
: FittedBox(
child: Image.network(
_imageUrlController.text,
fit: BoxFit.cover,
),
),
),
Expanded(
child: TextFormField(
decoration: InputDecoration(labelText: 'Image URL'),
keyboardType: TextInputType.url,
textInputAction: TextInputAction.done,
controller: _imageUrlController,
focusNode: _imageUrlFocusNode,
onEditingComplete: () {
setState(() {});
},
validator: (value) {
if (value.isEmpty) {
return 'Please enter an image URL';
}
if (!value.startsWith('http') &&
!value.startsWith('https')) {
return 'Please enter a valid URL';
}
if (!value.endsWith('.png') &&
!value.endsWith('.jpg') &&
!value.endsWith('.jpeg')) {
return 'Please enter a valid image URL';
}
return null;
},
onSaved: (value) {
_editedProduct = Product(
title: _editedProduct.title,
price: _editedProduct.price,
description: _editedProduct.description,
imageUrl: value,
id: _editedProduct.id,
isFavorite: _editedProduct.isFavorite,
);
},
),
)
],
)
],
),
),
),
);
}
}

0 comments on commit 776c2ed

Please sign in to comment.