简体   繁体   中英

How to change event in BLoC in Flutter?

I have an app which creates a 2FA totp key. When I click "Add" button when a form is not filled, it shows an Alert Dialog. If I click it second time (still, the form isn't filled), the alert dialog doesn't show up. How do I show the Alert Dialog for the infinite times? I used BlocConsumer to listen to changes and show the Alert Dialog when the state is ManualInputError, and a BlocBuilder to show the actual TextButton.

图片

Code:

import 'dart:io';

import 'package:duckie/blocs/manual_input/manual_input_bloc.dart';
import 'package:duckie/screens/widgets/alert_dialog.dart';
import 'package:duckie/screens/widgets/custom_text_field.dart';
import 'package:duckie/shared/text_styles.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class ManualInputScreen extends StatefulWidget {
  @override
  _ManualInputScreenState createState() => _ManualInputScreenState();
}

class _ManualInputScreenState extends State<ManualInputScreen> {
  String secretKey;
  String issuer;
  String accountName;
  String numberOfDigits = '6';
  String timeStep = '30';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'manual-input',
          style: TextStyles.appBarText,
        ).tr(),
        centerTitle: true,
        elevation: 0.0,
        actions: [
          BlocConsumer<ManualInputBloc, ManualInputState>(
            listener: (context, state) {
              if (state is ManualInputError) {
                Platform.isAndroid
                    ? CustomAlertDialog.showAndroidAlertDialog(
                        context,
                        state.alertDialogErrorTitle,
                        state.alertDialogErrorContent)
                    : CustomAlertDialog.showIosAlertDialog(
                        context,
                        state.alertDialogErrorTitle,
                        state.alertDialogErrorContent);

                BlocProvider.of<ManualInputBloc>(context).close();
              }
            },
            builder: (context, state) {
              if (state is ManualInputInitial || state is ManualInputFinal) {
                return TextButton(
                  onPressed: () {
                    BlocProvider.of<ManualInputBloc>(context).add(
                        GetFormTextEvent(secretKey, issuer, accountName,
                            numberOfDigits, timeStep));
                  },
                  child: Text('add').tr(),
                );
              }
              return TextButton(
                onPressed: () {},
                child: Text('add').tr(),
              );
            },
          )
        ],
      ),
      body: Container(
        padding: EdgeInsets.all(8.0),
        child: ListView(
          children: [
            CustomTextField(
              labelText: 'secret-key'.tr(),
              onChanged: (value) {
                setState(() {
                  secretKey = value;
                });
              },
            ),
            SizedBox(
              height: 8.0,
            ),
            CustomTextField(
              labelText: 'issuer'.tr(),
              onChanged: (value) {
                issuer = value;
              },
            ),
            SizedBox(
              height: 8.0,
            ),
            CustomTextField(
              labelText: 'account-name'.tr(),
              onChanged: (value) {
                setState(() {
                  accountName = value;
                });
              },
            ),
            SizedBox(
              height: 8.0,
            ),
            Platform.isAndroid
                ? ListBody(
                    children: [
                      Text('number-of-digits').tr(),
                      SizedBox(
                        height: 5.0,
                      ),
                      DropdownButton(
                        value: numberOfDigits,
                        onChanged: (value) {
                          setState(() {
                            numberOfDigits = value;
                          });
                        },
                        items: [
                          DropdownMenuItem(
                            value: '6',
                            child: Text('6'),
                          ),
                          DropdownMenuItem(
                            value: '8',
                            child: Text('8'),
                          ),
                        ],
                      )
                    ],
                  )
                : ListBody(
                    children: [
                      Text('number-of-digits').tr(),
                      SizedBox(
                        height: 5.0,
                      ),
                      CupertinoSegmentedControl(
                        groupValue: numberOfDigits,
                        children: {
                          '6': Text('6'),
                          '8': Text('8'),
                        },
                        onValueChanged: (value) {
                          setState(() {
                            numberOfDigits = value;
                          });
                        },
                      ),
                    ],
                  ),
            SizedBox(
              height: 8.0,
            ),
            Platform.isAndroid
                ? ListBody(
                    children: [
                      Text('time-step').tr(),
                      SizedBox(
                        height: 5.0,
                      ),
                      DropdownButton(
                        value: timeStep,
                        onChanged: (value) {
                          setState(() {
                            timeStep = value;
                          });
                        },
                        items: [
                          DropdownMenuItem(
                            value: '30',
                            child: Text('30'),
                          ),
                          DropdownMenuItem(
                            value: '60',
                            child: Text('60'),
                          ),
                        ],
                      )
                    ],
                  )
                : ListBody(
                    children: [
                      Text('time-step').tr(),
                      SizedBox(
                        height: 5.0,
                      ),
                      CupertinoSegmentedControl(
                        groupValue: timeStep,
                        children: {
                          '30': Text('30'),
                          '60': Text('60'),
                        },
                        onValueChanged: (value) {
                          setState(() {
                            timeStep = value;
                          });
                        },
                      ),
                    ],
                  ),
          ],
        ),
      ),
    );
  }
}

The reason is that you close(kill) the BLoC here

       BlocProvider.of<ManualInputBloc>(context).close();

Change the listener to that

       listener: (context, state) {
              if (state is ManualInputError) {
                Platform.isAndroid
                    ? CustomAlertDialog.showAndroidAlertDialog(
                        context,
                        state.alertDialogErrorTitle,
                        state.alertDialogErrorContent)
                    : CustomAlertDialog.showIosAlertDialog(
                        context,
                        state.alertDialogErrorTitle,
                        state.alertDialogErrorContent);
              }
            },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM