Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form bloc no longer required and many other changes #308

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions form_bloc_web/lib/examples/all_fields_form.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_bloc/flutter_form_bloc.dart';

void main() => runApp(const App());
Expand Down Expand Up @@ -44,7 +45,7 @@ class AllFieldsFormBloc extends FormBloc<String, String> {
final time1 = InputFieldBloc<TimeOfDay?, Object>(initialValue: null);

AllFieldsFormBloc() {
addFieldBlocs(fieldBlocs: [
addStep(ListFieldBloc<FieldBloc, dynamic>(fieldBlocs: [
text1,
boolean1,
boolean2,
Expand All @@ -54,7 +55,7 @@ class AllFieldsFormBloc extends FormBloc<String, String> {
date1,
dateAndTime1,
time1,
]);
]));
}

@override
Expand Down Expand Up @@ -90,7 +91,8 @@ class AllFieldsForm extends StatelessWidget {
),
child: Scaffold(
appBar: AppBar(title: const Text('Built-in Widgets')),
body: FormBlocListener<AllFieldsFormBloc, String, String>(
body: FormBlocListener<String, String>(
formBloc: formBloc,
onSubmitting: (context, state) {
LoadingDialog.show(context);
},
Expand Down
7 changes: 4 additions & 3 deletions form_bloc_web/lib/examples/async_field_validation_form.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_bloc/flutter_form_bloc.dart';

void main() => runApp(const App());
Expand All @@ -25,7 +26,7 @@ class AsyncFieldValidationFormBloc extends FormBloc<String, String> {
);

AsyncFieldValidationFormBloc() {
addFieldBlocs(fieldBlocs: [username]);
addStep(username);

username.addAsyncValidators(
[_checkUsername],
Expand Down Expand Up @@ -75,8 +76,8 @@ class AsyncFieldValidationForm extends StatelessWidget {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(title: const Text('Async Field Validation')),
body:
FormBlocListener<AsyncFieldValidationFormBloc, String, String>(
body: FormBlocListener<String, String>(
formBloc: formBloc,
onSubmitting: (context, state) {
LoadingDialog.show(context);
},
Expand Down
201 changes: 96 additions & 105 deletions form_bloc_web/lib/examples/conditional_fields_form.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_form_bloc/flutter_form_bloc.dart';
import 'package:form_bloc_web/widgets/app_form_bloc_provider.dart';

void main() => runApp(const App());

Expand All @@ -16,6 +17,8 @@ class App extends StatelessWidget {
}

class ConditionalFieldsFormBloc extends FormBloc<String, String> {
final _step = ListFieldBloc();

final doYouLikeFormBloc = SelectFieldBloc(
validators: [FieldBlocValidators.required],
items: ['No', 'Yes'],
Expand All @@ -32,38 +35,30 @@ class ConditionalFieldsFormBloc extends FormBloc<String, String> {
);

ConditionalFieldsFormBloc() {
addFieldBlocs(
fieldBlocs: [
doYouLikeFormBloc,
],
);
addStep(_step..addFieldBloc(doYouLikeFormBloc));

showSecretField.onValueChanges(
onData: (previous, current) async* {
if (current.value) {
addFieldBlocs(fieldBlocs: [secretField]);
_step.addFieldBloc(secretField);
} else {
removeFieldBlocs(fieldBlocs: [secretField]);
_step.removeFieldBloc(secretField);
}
},
);

doYouLikeFormBloc.onValueChanges(
onData: (previous, current) async* {
removeFieldBlocs(
fieldBlocs: [
whyNotYouLikeFormBloc,
showSecretField,
secretField,
],
);
_step.removeFieldBlocs([
whyNotYouLikeFormBloc,
showSecretField,
secretField,
]);

if (current.value == 'No') {
addFieldBlocs(fieldBlocs: [
whyNotYouLikeFormBloc,
]);
_step.addFieldBloc(whyNotYouLikeFormBloc);
} else if (current.value == 'Yes') {
addFieldBlocs(fieldBlocs: [
_step.addFieldBlocs([
showSecretField,
if (showSecretField.value) secretField,
]);
Expand All @@ -88,13 +83,9 @@ class ConditionalFieldsFormBloc extends FormBloc<String, String> {
debugPrint(showSecretField.value.toString());
debugPrint(secretField.value);

try {
await Future<void>.delayed(const Duration(milliseconds: 500));
await Future<void>.delayed(const Duration(milliseconds: 500));

emitSuccess();
} catch (e) {
emitFailure();
}
emitSuccess();
}
}

Expand All @@ -103,98 +94,97 @@ class ConditionalFieldsForm extends StatelessWidget {

@override
Widget build(BuildContext context) {
return BlocProvider(
return AppFormBlocProvider<ConditionalFieldsFormBloc>(
create: (context) => ConditionalFieldsFormBloc(),
child: Builder(
builder: (context) {
final formBloc = BlocProvider.of<ConditionalFieldsFormBloc>(context);

return Theme(
data: Theme.of(context).copyWith(
inputDecorationTheme: InputDecorationTheme(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
),
builder: (context, formBloc) {
return Theme(
data: Theme.of(context).copyWith(
inputDecorationTheme: InputDecorationTheme(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
),
),
child: Scaffold(
appBar: AppBar(title: const Text('Conditional Fields')),
body: FormBlocListener<ConditionalFieldsFormBloc, String, String>(
onSubmitting: (context, state) {
LoadingDialog.show(context);
},
onSubmissionFailed: (context, state) {
LoadingDialog.hide(context);
},
onSuccess: (context, state) {
LoadingDialog.hide(context);

Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => const SuccessScreen()));
},
onFailure: (context, state) {
LoadingDialog.hide(context);

ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(state.failureResponse!)));
},
child: SingleChildScrollView(
physics: const ClampingScrollPhysics(),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
RadioButtonGroupFieldBlocBuilder(
selectFieldBloc: formBloc.doYouLikeFormBloc,
itemBuilder: (context, dynamic value) =>
FieldItem(child: Text(value)),
decoration: const InputDecoration(
labelText: 'Do you like form bloc?',
prefixIcon: SizedBox(),
),
),
TextFieldBlocBuilder(
textFieldBloc: formBloc.whyNotYouLikeFormBloc,
keyboardType: TextInputType.multiline,
maxLines: 1,
decoration: const InputDecoration(
labelText: 'Why?',
prefixIcon: Icon(Icons.sentiment_very_dissatisfied),
),
),
child: Scaffold(
appBar: AppBar(title: const Text('Conditional Fields')),
body: FormBlocListener<String, String>(
formBloc: formBloc,
onSubmitting: (context, state) {
LoadingDialog.show(context);
},
onSubmissionFailed: (context, state) {
LoadingDialog.hide(context);
},
onSuccess: (context, state) {
LoadingDialog.hide(context);

Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => const SuccessScreen()));
},
onFailure: (context, state) {
LoadingDialog.hide(context);

ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('${state.failureResponse}'),
));
},
child: SingleChildScrollView(
physics: const ClampingScrollPhysics(),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
RadioButtonGroupFieldBlocBuilder(
selectFieldBloc: formBloc.doYouLikeFormBloc,
itemBuilder: (context, dynamic value) =>
FieldItem(child: Text(value)),
decoration: const InputDecoration(
labelText: 'Do you like form bloc?',
prefixIcon: SizedBox(),
),
SizedBox(
width: 200,
child: CheckboxFieldBlocBuilder(
booleanFieldBloc: formBloc.showSecretField,
controlAffinity:
FieldBlocBuilderControlAffinity.trailing,
body: Container(
alignment: Alignment.center,
child: const Text('Do you want to see a secret field?'),
),
),
),
TextFieldBlocBuilder(
textFieldBloc: formBloc.whyNotYouLikeFormBloc,
keyboardType: TextInputType.multiline,
maxLines: 1,
decoration: const InputDecoration(
labelText: 'Why?',
prefixIcon: Icon(Icons.sentiment_very_dissatisfied),
),
TextFieldBlocBuilder(
textFieldBloc: formBloc.secretField,
keyboardType: TextInputType.multiline,
decoration: const InputDecoration(
labelText: 'Secret field',
prefixIcon: Icon(Icons.sentiment_very_satisfied),
),
SizedBox(
width: 200,
child: CheckboxFieldBlocBuilder(
booleanFieldBloc: formBloc.showSecretField,
controlAffinity:
FieldBlocBuilderControlAffinity.trailing,
body: Container(
alignment: Alignment.center,
child: const Text(
'Do you want to see a secret field?'),
),
),
ElevatedButton(
onPressed: formBloc.submit,
child: const Text('SUBMIT'),
),
TextFieldBlocBuilder(
textFieldBloc: formBloc.secretField,
keyboardType: TextInputType.multiline,
decoration: const InputDecoration(
labelText: 'Secret field',
prefixIcon: Icon(Icons.sentiment_very_satisfied),
),
],
),
),
ElevatedButton(
onPressed: formBloc.submit,
child: const Text('SUBMIT'),
),
],
),
),
),
),
);
},
),
),
);
},
);
}
}
Expand Down Expand Up @@ -249,7 +239,8 @@ class SuccessScreen extends StatelessWidget {
const SizedBox(height: 10),
ElevatedButton.icon(
onPressed: () => Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => const ConditionalFieldsForm())),
MaterialPageRoute(
builder: (_) => const ConditionalFieldsForm())),
icon: const Icon(Icons.replay),
label: const Text('AGAIN'),
),
Expand Down
Loading