toFormWidgetsCode method
Implementation
String toFormWidgetsCode() {
// print('toFormWidgetsCode begin');
final String tablename = table.tableName!.toLowerCase();
final String modelName = table.modelName ?? toCamelCase(table.tableName);
// print('toFormWidgetsCode begin 2: tableName:$tablename');
if (table.primaryKeyNames.isEmpty) {
throw Exception(
' SQFENTITIY: FORM GENERATOR ERROR: Table [$tablename] has no primary key. Remove this table from formTables list in your DB Model or add a primary key into the table');
}
return '''
class ${modelName}Add extends StatefulWidget {
${modelName}Add(this._$tablename);
final dynamic _$tablename;
@override
State<StatefulWidget> createState() => ${modelName}AddState(_$tablename as $modelName);
}
class ${modelName}AddState extends State {
${modelName}AddState(this.$tablename);
$modelName $tablename;
final _formKey = GlobalKey<FormState>();
${toFormDeclarationCodeTable(table).toString()}
@override
void initState() {
${toFormInitStateCodeTable(table).toString()}
super.initState();
}
@override
Widget build(BuildContext context) {
${toFormBuildDropDownCodeTable(table).toString()}
return Scaffold(
appBar: AppBar(
title: ($tablename.${table.primaryKeyNames[0]} == null)
? Text('Add a new $tablename')
: Text('Edit $tablename'),
),
body: Container(
alignment: Alignment.topCenter,
width: double.infinity,
height: double.infinity,
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.only(top: 10.0, left: 10.0, right: 10.0),
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
${toFormBuildRowWidgets(table).toString()}
TextButton(
child: saveButton(),
onPressed: () {
if (_formKey.currentState!.validate()) {
// If the form is valid, display a Snackbar.
save();
/* Scaffold.of(context).showSnackBar(SnackBar(
duration: Duration(seconds: 2),
content: Text('Processing Data')));
*/
}
},
)
],
),
)),
),
),
);
}
${toFormBuildRowCodeTable(table).toString()}
Container saveButton() {
return Container(
padding: const EdgeInsets.all(7.0),
decoration: BoxDecoration(
color: Color.fromRGBO(95, 66, 119, 1.0),
border: Border.all(color: Colors.black),
borderRadius: BorderRadius.circular(5.0)),
child: Text(
'Save',
style: TextStyle(color: Colors.white, fontSize: 20),
),
);
}
void save() async {
${toFormSaveCodeDateTimeVariables(table).toString()}
$tablename
${toFormSaveCode(table).toString()};
await $tablename.${table.relationType == RelationType.ONE_TO_ONE ? '_' : ''}save();
if ($tablename.saveResult!.success) {
Navigator.pop(context, true);
} else
{
UITools(context).alertDialog($tablename.saveResult.toString(),
title: 'save ${table.modelName} Failed!', callBack: () {
});
}
}
}
''';
}