Skip to content

Instantly share code, notes, and snippets.

@a-oboh
Created July 16, 2020 18:36
Show Gist options
  • Save a-oboh/c0890570773c588f61c8ca213d4810bd to your computer and use it in GitHub Desktop.
Save a-oboh/c0890570773c588f61c8ca213d4810bd to your computer and use it in GitHub Desktop.
inputItemDialog(BuildContext context, String action, [int index]) {
var inventoryDb = Provider.of<HomeModel>(context, listen: false);
showDialog(
context: context,
builder: (context) {
return Dialog(
child: Container(
padding: EdgeInsets.only(
left: 15,
right: 15,
top: 40,
),
height: 45.height,
child: SingleChildScrollView(
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
Text(
action == 'add' ? 'Add Item' : 'Update Item',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
SizedBox(
height: 10,
),
TextFormField(
controller: nameController,
validator: (value) {
if (value.isEmpty) {
return 'Item name cannot be empty';
}
return null;
},
decoration: InputDecoration(
labelText: 'Item name',
),
),
SizedBox(
height: 40,
),
TextFormField(
controller: descriptionController,
validator: (value) {
if (value.isEmpty) {
return 'Item description cannot be empty';
}
return null;
},
decoration: InputDecoration(
labelText: 'Item description',
),
),
SizedBox(
height: 40,
),
RaisedButton(
onPressed: () async {
if (_formKey.currentState.validate()) {
if (action == 'add') {
await inventoryDb.addItem(Inventory(
name: nameController.text,
description: descriptionController.text,
));
} else {
}
nameController.clear();
descriptionController.clear();
inventoryDb.getInventory();
Navigator.pop(context);
}
},
color: Colors.green[600],
child: Text(
action == 'add' ? 'Add' : 'update',
style: TextStyle(
fontSize: 16,
color: Colors.white,
),
),
),
SizedBox(
height: 20,
)
],
),
),
),
),
);
},
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment