Commit 346a9d54 authored by Dasun Madushanka's avatar Dasun Madushanka

update form_m

parent e86359f8
import 'package:DMTI_app/screens/Form/user.dart';
import 'package:DMTI_app/screens/product/products_screen.dart';
import 'package:flutter/material.dart';
import 'empty_state.dart';
import 'form.dart';
class MultiForm extends StatefulWidget {
@override
_MultiFormState createState() => _MultiFormState();
}
class _MultiFormState extends State<MultiForm> {
List<UserForm> users = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: .0,
leading: IconButton(
onPressed: () {
Navigator.of(context)
.push(MaterialPageRoute(builder: (c) => ProductsScreen()));
},
icon: Icon(
Icons.home,
),
),
title: Text('Patients Records'),
actions: <Widget>[
FlatButton(
child: Text('Save'),
textColor: Colors.white,
onPressed: onSave,
)
],
),
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Color(0xFF30C1FF),
Color(0xFF2AA7DC),
],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
child: users.length <= 0
? Center(
child: EmptyState(
title: 'No Records',
message: 'Add form by tapping add button below',
),
)
: ListView.builder(
addAutomaticKeepAlives: true,
itemCount: users.length,
itemBuilder: (_, i) => users[i],
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: onAddForm,
foregroundColor: Colors.white,
),
);
}
///on form user deleted
void onDelete(User _user) {
setState(() {
var find = users.firstWhere(
(it) => it.user == _user,
orElse: () => null,
);
if (find != null) users.removeAt(users.indexOf(find));
});
}
///on add form
void onAddForm() {
setState(() {
var _user = User();
users.add(UserForm(
user: _user,
onDelete: () => onDelete(_user),
));
});
}
///on save forms
void onSave() {
if (users.length > 0) {
var allValid = true;
users.forEach((form) => allValid = allValid && form.isValid());
if (allValid) {
var data = users.map((it) => it.user).toList();
Navigator.push(
context,
MaterialPageRoute(
fullscreenDialog: true,
builder: (_) => Scaffold(
appBar: AppBar(
title: Text('List of Users'),
),
body: ListView.builder(
itemCount: data.length,
itemBuilder: (_, i) => ListTile(
leading: CircleAvatar(
child: Text(data[i].fullName.substring(0, 1)),
),
title: Text(data[i].fullName),
subtitle: Text(
data[i].mentalage,
),
),
),
),
),
);
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment