reactive_forms_generator 0.0.10-beta reactive_forms_generator: ^0.0.10-beta copied to clipboard
Generator for reactive_forms. Generates form classes based on model.
import 'package:example/login.dart';
import 'package:example/login.gform.dart';
import 'package:flutter/material.dart';
import 'package:reactive_forms/reactive_forms.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: LoginFormBuilder(
builder: (context, formModel, child) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
ReactiveTextField<String>(
formControlName: formModel.email,
validationMessages: (control) => {
ValidationMessage.required: 'The email must not be empty',
ValidationMessage.email:
'The email value must be a valid email',
'unique': 'This email is already in use',
},
textInputAction: TextInputAction.next,
decoration: const InputDecoration(
labelText: 'Email',
helperText: '',
helperStyle: TextStyle(height: 0.7),
errorStyle: TextStyle(height: 0.7),
),
),
const SizedBox(height: 16.0),
ReactiveTextField<String>(
formControlName: formModel.password,
obscureText: true,
validationMessages: (control) => {
ValidationMessage.required:
'The password must not be empty',
ValidationMessage.minLength:
'The password must be at least 8 characters',
},
textInputAction: TextInputAction.done,
decoration: const InputDecoration(
labelText: 'Password',
helperText: '',
helperStyle: TextStyle(height: 0.7),
errorStyle: TextStyle(height: 0.7),
),
),
ElevatedButton(
onPressed: () {
if (formModel.form.valid) {
print(formModel.model);
print(formModel.model.email);
} else {
formModel.form.markAllAsTouched();
}
},
child: const Text('Sign Up'),
),
ReactiveLoginFormConsumer(
builder: (context, form, child) {
return ElevatedButton(
child: Text('Submit'),
onPressed: form.form.valid ? () {} : null,
);
},
),
],
),
);
},
model: Login(
password: '1234',
categories: [],
friends: [],
userData: UserData(
skills: [],
lastName: '',
firstName: '',
),
),
),
);
}
}