flutter_form_bloc 0.12.0 flutter_form_bloc: ^0.12.0 copied to clipboard
Create Beautiful Forms in Flutter. The easiest way to Prefill, Async Validation, Update Form Fields, and Show Progress, Failures or Navigate by Reacting to the Form State.
import 'package:flutter/material.dart';
import 'package:flutter_form_bloc/flutter_form_bloc.dart';
void main() {
runApp(App());
}
class App extends StatelessWidget {
const App({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: AllFieldsForm(),
);
}
}
class AllFieldsFormBloc extends FormBloc<String, String> {
final text1 = TextFieldBloc();
final boolean1 = BooleanFieldBloc();
final boolean2 = BooleanFieldBloc();
final select1 = SelectFieldBloc(
items: ['Option 1', 'Option 2'],
);
final select2 = SelectFieldBloc(
items: ['Option 1', 'Option 2'],
);
final multiSelect1 = MultiSelectFieldBloc<String, dynamic>(
items: [
'Option 1',
'Option 2',
],
);
final date1 = InputFieldBloc<DateTime, Object>();
final dateAndTime1 = InputFieldBloc<DateTime, Object>();
final time1 = InputFieldBloc<TimeOfDay, Object>();
AllFieldsFormBloc() {
addFieldBlocs(fieldBlocs: [
text1,
boolean1,
boolean2,
select1,
select2,
multiSelect1,
date1,
dateAndTime1,
time1,
]);
}
void addErrors() {
text1.addError('Awesome Error!');
boolean1.addError('Awesome Error!');
boolean2.addError('Awesome Error!');
select1.addError('Awesome Error!');
select2.addError('Awesome Error!');
multiSelect1.addError('Awesome Error!');
date1.addError('Awesome Error!');
dateAndTime1.addError('Awesome Error!');
time1.addError('Awesome Error!');
}
@override
void onSubmitting() async {
try {
await Future<void>.delayed(Duration(milliseconds: 500));
emitSuccess(canSubmitAgain: true);
} catch (e) {
emitFailure();
}
}
}
class AllFieldsForm extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => AllFieldsFormBloc(),
child: Builder(
builder: (context) {
final formBloc = BlocProvider.of<AllFieldsFormBloc>(context);
return Theme(
data: Theme.of(context).copyWith(
inputDecorationTheme: InputDecorationTheme(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
),
),
),
child: Scaffold(
appBar: AppBar(title: Text('Built-in Widgets')),
floatingActionButton: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FloatingActionButton.extended(
heroTag: null,
onPressed: formBloc.addErrors,
icon: Icon(Icons.error_outline),
label: Text('ADD ERRORS'),
),
SizedBox(height: 12),
FloatingActionButton.extended(
heroTag: null,
onPressed: formBloc.submit,
icon: Icon(Icons.send),
label: Text('SUBMIT'),
),
],
),
body: FormBlocListener<AllFieldsFormBloc, String, String>(
onSubmitting: (context, state) {
LoadingDialog.show(context);
},
onSuccess: (context, state) {
LoadingDialog.hide(context);
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => SuccessScreen()));
},
onFailure: (context, state) {
LoadingDialog.hide(context);
Scaffold.of(context).showSnackBar(
SnackBar(content: Text(state.failureResponse)));
},
child: SingleChildScrollView(
physics: ClampingScrollPhysics(),
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
children: <Widget>[
TextFieldBlocBuilder(
textFieldBloc: formBloc.text1,
decoration: InputDecoration(
labelText: 'TextFieldBlocBuilder',
prefixIcon: Icon(Icons.text_fields),
),
),
DropdownFieldBlocBuilder<String>(
selectFieldBloc: formBloc.select1,
decoration: InputDecoration(
labelText: 'DropdownFieldBlocBuilder',
prefixIcon: Icon(Icons.sentiment_satisfied),
),
itemBuilder: (context, value) => value,
),
RadioButtonGroupFieldBlocBuilder<String>(
selectFieldBloc: formBloc.select2,
decoration: InputDecoration(
labelText: 'RadioButtonGroupFieldBlocBuilder',
prefixIcon: SizedBox(),
),
itemBuilder: (context, item) => item,
),
CheckboxGroupFieldBlocBuilder<String>(
multiSelectFieldBloc: formBloc.multiSelect1,
itemBuilder: (context, item) => item,
decoration: InputDecoration(
labelText: 'CheckboxGroupFieldBlocBuilder',
prefixIcon: SizedBox(),
),
),
DateTimeFieldBlocBuilder(
dateTimeFieldBloc: formBloc.date1,
format: DateFormat('dd-mm-yyyy'),
initialDate: DateTime.now(),
firstDate: DateTime(1900),
lastDate: DateTime(2100),
decoration: InputDecoration(
labelText: 'DateTimeFieldBlocBuilder',
prefixIcon: Icon(Icons.calendar_today),
helperText: 'Date',
),
),
DateTimeFieldBlocBuilder(
dateTimeFieldBloc: formBloc.dateAndTime1,
canSelectTime: true,
format: DateFormat('dd-mm-yyyy hh:mm'),
initialDate: DateTime.now(),
firstDate: DateTime(1900),
lastDate: DateTime(2100),
decoration: InputDecoration(
labelText: 'DateTimeFieldBlocBuilder',
prefixIcon: Icon(Icons.date_range),
helperText: 'Date and Time',
),
),
TimeFieldBlocBuilder(
timeFieldBloc: formBloc.time1,
format: DateFormat('hh:mm a'),
initialTime: TimeOfDay.now(),
decoration: InputDecoration(
labelText: 'TimeFieldBlocBuilder',
prefixIcon: Icon(Icons.access_time),
),
),
SwitchFieldBlocBuilder(
booleanFieldBloc: formBloc.boolean2,
body: Container(
alignment: Alignment.centerLeft,
child: Text('CheckboxFieldBlocBuilder'),
),
),
CheckboxFieldBlocBuilder(
booleanFieldBloc: formBloc.boolean1,
body: Container(
alignment: Alignment.centerLeft,
child: Text('CheckboxFieldBlocBuilder'),
),
),
],
),
),
),
),
),
);
},
),
);
}
}
class LoadingDialog extends StatelessWidget {
static void show(BuildContext context, {Key key}) => showDialog<void>(
context: context,
useRootNavigator: false,
barrierDismissible: false,
builder: (_) => LoadingDialog(key: key),
).then((_) => FocusScope.of(context).requestFocus(FocusNode()));
static void hide(BuildContext context) => Navigator.pop(context);
LoadingDialog({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async => false,
child: Center(
child: Card(
child: Container(
width: 80,
height: 80,
padding: EdgeInsets.all(12.0),
child: CircularProgressIndicator(),
),
),
),
);
}
}
class SuccessScreen extends StatelessWidget {
SuccessScreen({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.tag_faces, size: 100),
SizedBox(height: 10),
Text(
'Success',
style: TextStyle(fontSize: 54, color: Colors.black),
textAlign: TextAlign.center,
),
SizedBox(height: 10),
RaisedButton.icon(
onPressed: () => Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => AllFieldsForm())),
icon: Icon(Icons.replay),
label: Text('AGAIN'),
),
],
),
),
);
}
}