Bloc: not closing

Created on 22 Jun 2020  路  9Comments  路  Source: felangel/bloc

https://github.com/felangel/bloc/blob/8daf13762929e0966bf955f0beb5aa844303b40f/examples/flutter_form_validation/lib/main.dart#L152

There is no error in the project, but Raised button not closing, after validation performed

RaisedButton(child: Text('OK'), onPressed: onDismissed),

i am using..

Dart version 2.9.0
Flutter version 1.19

flutter_bloc: 4.0.0
equatable: 1.0.0
formz: 0.2.0

example question

Most helpful comment

Thanks @RollyPeres 馃檹
I've updated the example 馃槃

Closing this for now but feel free to comment with any additional questions and I'm happy to continue the conversation 馃憤

All 9 comments

Hi @aamadmin 馃憢
Thanks for opening an issue!

Can you please share the link to a sample app which illustrates the issue? Thanks 馃檹

* main.dart *
import 'package:flutter/material.dart';
import 'package:flutter_form_validation/blocs/reg_form_bloc.dart';
import 'package:flutter_form_validation/screens/register_screen.dart';
import 'package:flutter_form_validation/themes/style.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Register',
theme: primarytheme,
home: Scaffold(
appBar: AppBar(
title: Text('Bloc Register'),
),
body: BlocProvider(
create: (context) => RegFormBloc(),
child: RegistrationForm()),
),
);
}
}

* register_screen.dart *

import 'package:flutter/material.dart';
import 'package:flutter_form_validation/blocs/reg_form_bloc.dart';
import 'package:formz/formz.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class RegistrationForm extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocListener(
listener: (context, state) {
if (state.status.isSubmissionSuccess) {
showDialog(
context: context,
builder: (_) => SuccessDialog(onDismissed: () {
context.bloc().add(FormReset());
}),
);
}
if (state.status.isSubmissionInProgress) {
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(
SnackBar(content: Text('Submitting..')),
);
}
},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
//Text('Fluter Bloc Register'),
EmailInput(),
PasswordInput(),
SizedBox(
height: 20.0,
),
SubmitButton(),
],
),
),
);
}
}

class EmailInput extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder(
condition: (previous, current) => previous.email != current.email,
builder: (context, state) {
return TextFormField(
decoration: InputDecoration(
icon: Icon(
Icons.email,
color: Colors.blue,
),
labelText: 'Email',
hintText: '[email protected]',
errorText: state.email.invalid ? 'Invaild Email' : null,
),
keyboardType: TextInputType.emailAddress,
onChanged: (value) {
context.bloc().add(EmailChanged(email: value));
},
);
},
);
}
}

class PasswordInput extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder(
condition: (previous, current) => previous.password != current.password,
builder: (context, state) {
return TextFormField(
decoration: InputDecoration(
icon: Icon(Icons.lock),
labelText: 'Password',
hintText: '*',
errorText: state.password.invalid ? 'Invaild Password' : null,
),
obscureText: true,
onChanged: (value) {
context.bloc().add(PasswordChanged(password: value));
},
);
},
);
}
}

class SubmitButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder(
condition: (previous, current) => previous.status != current.status,
builder: (context, state) {
return RaisedButton(
onPressed: state.status.isValidated
? () => context.bloc().add(FormSubmitted())
: null,
child: Text('Submit'),
);
},
);
}
}

class SuccessDialog extends StatelessWidget {
final VoidCallback onDismissed;
SuccessDialog({Key key, @required this.onDismissed}) : super(key: key);
@override
Widget build(BuildContext context) {
return Dialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.0)),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisSize: MainAxisSize.max,
children: [
Icon(Icons.info),
Flexible(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Text(
'Form Submitted..',
softWrap: true,
),
),
),
],
),
RaisedButton(child: Text('OK'), onPressed: onDismissed),
],
),
),
);
}
}

* reg_form_bloc.dart *

import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:formz/formz.dart';
import 'package:flutter_form_validation/models/models.dart';

part 'reg_form_event.dart';
part 'reg_form_state.dart';

class RegFormBloc extends Bloc {
@override
RegFormState get initialState => const RegFormState();

void onTransition(Transition transition) {
print(transition);
super.onTransition(transition);
}

@override
Stream mapEventToState(RegFormEvent event) async* {
if (event is EmailChanged) {
final email = Email.dirty(event.email);
yield state.copyWith(
email: email,
status: Formz.validate([email, state.password]),
);
} else if (event is PasswordChanged) {
final password = Password.dirty(event.password);
yield state.copyWith(
password: password,
status: Formz.validate([state.email, password]),
);
} else if (event is FormSubmitted) {
if (state.status.isValidated) {
yield state.copyWith(status: FormzStatus.submissionInProgress);
await Future.delayed(const Duration(seconds: 1));
yield state.copyWith(status: FormzStatus.submissionSuccess);
}
} else if (event is FormReset) {
yield const RegFormState();
}
}
}

@aamadmin are you able to share a link to a sample app which I can run locally? It would be much easier for me help if I can quickly run the app locally and reproduce the issue, thanks 馃憤

@aamadmin would it be possible for you to make a public github repo which I can clone? Thanks so much 馃檹

Hi @aamadmin 馃憢

Your dialog is not closing because you omitted to explicitly close it. Your dismissing callback should look something like:

onDismissed: () {
  context.bloc().add(FormReset());
  Navigator.pop(context);
}

Thanks @RollyPeres 馃檹
I've updated the example 馃槃

Closing this for now but feel free to comment with any additional questions and I'm happy to continue the conversation 馃憤

Hi @aamadmin

Your dialog is not closing because you omitted to explicitly close it. Your dismissing callback should look something like:

onDismissed: () {
  context.bloc().add(FormReset());
  Navigator.pop(context);
}
        builder: (_) => SuccessDialog(onDismissed: () {
          context.bloc<RegFormBloc>().add(FormReset());
          Navigator.pop(context);
        }),

In this popup closes but form doesn't REST

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rsnider19 picture rsnider19  路  3Comments

ricktotec picture ricktotec  路  3Comments

1AlexFix1 picture 1AlexFix1  路  3Comments

frankrod picture frankrod  路  3Comments

RobPFarley picture RobPFarley  路  3Comments