轻按文本字段时,表单中的TextFieldBlocBuilder无法正常工作

时间:2019-10-27 23:01:04

标签: flutter

我有一个小的表格,我想在其中点击文本字段,然后会弹出一个对话框。当我运行我的应用程序并点击文本字段时,onTap函数内部的代码未得到执行。这是我的代码

import 'package:form_bloc/form_bloc.dart';

class IncomeFormBloc extends FormBloc<String, String> {
  final expectedAmount = TextFieldBloc(
    validators: [FieldBlocValidators.requiredTextFieldBloc],
  );

  final frequency = TextFieldBloc(
    validators: [FieldBlocValidators.requiredTextFieldBloc],
  );

  final payoutDate = TextFieldBloc(
    validators: [FieldBlocValidators.requiredTextFieldBloc],

  );

  final category = TextFieldBloc(
    validators: [FieldBlocValidators.requiredTextFieldBloc],
  );

  final depositTo = TextFieldBloc(
    validators: [FieldBlocValidators.requiredTextFieldBloc],
  );

  final description = TextFieldBloc(
    validators: [FieldBlocValidators.requiredTextFieldBloc],
  );



  final booleanField = BooleanFieldBloc();

  final selectField1 = SelectFieldBloc(
    items: ['Option 1', 'Option 2', 'Option 3'],
  );

  final selectField2 = SelectFieldBloc(
    items: ['Option 1', 'Option 2'],
  );

  final multiSelectField = MultiSelectFieldBloc<String>(
    items: ['Option 1', 'Option 2', 'Option 3'],
  );

  @override
  List<FieldBloc> get fieldBlocs => [
    expectedAmount,
    frequency,
    payoutDate,
    category,
    depositTo,
    description,
    selectField1
  ];

  @override
  Stream<FormBlocState<String, String>> onSubmitting() async* {
    // Awesome logic...


    await Future<void>.delayed(Duration(seconds: 2));
    yield state.toSuccess('Success');

    // yield `state.toLoaded()` because
    // you can't submit if the current state is `FormBlocSuccess`.
    // In most cases you don't need to do this,
    // because you only want to submit only once,
    // but in this case you want the user to submit more than once.
    // See: https://pub.dev/documentation/form_bloc/latest/form_bloc/FormBloc/onSubmitting.html
    yield state.toLoaded();
  }
}


import 'package:flutter/material.dart';
import 'package:finsec/widget/row_text_input.dart';
import 'package:finsec/widget/text_form_field.dart';
import 'package:finsec/widget/save_button.dart';
import 'package:finsec/utils/strings.dart';
import 'package:finsec/utils/dimens.dart';
import 'package:finsec/utils/colors.dart';
import 'package:finsec/widget/column_text_input.dart';
import 'package:finsec/data/blocs/transaction_bloc.dart';
import 'package:finsec/model/income/income.dart';
import 'package:finsec/model/income/income_dao.dart';
import 'package:finsec/data/db_provider.dart';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:finsec/flutter_form_bloc.dart';
import 'package:finsec/data/blocs/income_form_bloc.dart';
import 'package:finsec/widget/widgets.dart';
import 'package:form_bloc/form_bloc.dart';
import 'package:rflutter_alert/rflutter_alert.dart';

class AddEditIncome extends StatefulWidget {

  final String appBarTitle;
  final Income income;

  AddEditIncome({this.income, this.appBarTitle});

  @override
  State<StatefulWidget> createState() {
    return _AddEditIncomeState();
  }

}

class _AddEditIncomeState extends State<AddEditIncome> {

  var _formKey = GlobalKey<FormState>();
  var _currentItemSelected = '';

  String appBarTitle;
  Income income;

  @override
  void initState() {
    super.initState();
  }



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

          return Scaffold(
            appBar: AppBar(title: Text('Form Fields Example')),
            body: FormBlocListener<IncomeFormBloc, String, String>(
              onSubmitting: (context, state) => LoadingDialog.show(context),
              onSuccess: (context, state) {
                LoadingDialog.hide(context);
                Notifications.showSnackBarWithSuccess(
                    context, state.successResponse);
              },
              onFailure: (context, state) {
                LoadingDialog.hide(context);
                Notifications.showSnackBarWithError(
                    context, state.failureResponse);
              },
              child: Column(

                children: <Widget>[
                  TextFieldBlocBuilder(
                    textFieldBloc: formBloc.expectedAmount,
                  focusNode: AlwaysDisabledFocusNode(),
                  onTap: () {

                    print('SHOW DIALOG');
                  },


                    decoration: InputDecoration(
                      labelText: 'TextFieldBlocBuilder',
                      prefixIcon: Icon(Icons.sentiment_very_satisfied),
                        border: OutlineInputBorder(
                            borderSide:  BorderSide(width:5.0),
                            borderRadius: BorderRadius.circular(5.0)
                        )
                    ),
                    errorBuilder: (context, error) {
                      switch (error) {
                        case FieldBlocValidatorsErrors.requiredTextFieldBloc:
                          return 'You must write amazing text.';
                          break;
                        default:
                          return 'This text is nor valid.';
                      }
                    },
                  ),
                  DropdownFieldBlocBuilder<String>(
                    selectFieldBloc: formBloc.selectField1,
                    decoration: InputDecoration(
                      labelText: 'DropdownFieldBlocBuilder',
                      prefixIcon: Icon(Icons.sentiment_very_dissatisfied),
                    ),
                    itemBuilder: (context, value) => value,
                    padding: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),  //size of textfield,
                  ),
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: RaisedButton(
                      onPressed: formBloc.submit,
                      child: Center(child: Text('SUBMIT')),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 8.0),
                    child: RaisedButton(
                      onPressed: formBloc.clear,
                      child: Center(child: Text('CLEAR')),
                    ),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }


  _showDialog(BuildContext context) {
    Scaffold.of(context)
        .showSnackBar(SnackBar(content: Text('Submitting form')));
  }
}


void _showDialog(context) {
  // flutter defined function
  showDialog(
    context: context,
    builder: (BuildContext context) {
      // return alert dialog object
      return AlertDialog(
        title: new Text('I am Title'),
        content: Container(
          height: 150.0,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              new Row(
                children: <Widget>[
                  new Icon(Icons.toys),
                  Padding(
                    padding: const EdgeInsets.only(left: 8.0),
                    child: new Text(' First Item'),
                  ),
                ],
              ),
              new SizedBox(
                height: 20.0,
              ),
              new Row(
                children: <Widget>[
                  new Icon(Icons.toys),
                  Padding(
                    padding: const EdgeInsets.only(left: 8.0),
                    child: new Text(' Second Item'),
                  ),
                ],
              ),
              new SizedBox(
                height: 20.0,
              ),
              new Row(
                children: <Widget>[
                  new Icon(Icons.toys),
                  Padding(
                    padding: const EdgeInsets.only(left: 8.0),
                    child: new Text('Third Item'),
                  ),
                ],
              ),
            ],
          ),
        ),
      );
    },
  );
}

class AlwaysDisabledFocusNode extends FocusNode {
  @override
  bool get hasFocus => false;
}

如果您注意到,我正在onTap函数中打印一条消息

child: Column(

                children: <Widget>[
                  TextFieldBlocBuilder(
                    textFieldBloc: formBloc.expectedAmount,
                  focusNode: AlwaysDisabledFocusNode(),
                  onTap: () {

                    print('SHOW DIALOG');
                  },

我想做的是,当我点击文本字段时,不应显示软键盘,并且文本字段中不应显示任何光标,而应显示一个对话框,我可以在其中选择一个值并设置具有该值的文本字段。现在,我只是打印SHOW DIALOG消息,以查看onTap功能是否正常运行,但不能正常工作。如果我删除焦点代码focusNode: AlwaysDisabledFocusNode(),,那么当我点击文本字段时,光标将焦点对准文本字段和键盘,但是onTap代码不会执行。

有人可以对onTap为什么在我的代码中不起作用提出建议吗?

1 个答案:

答案 0 :(得分:0)

那是一个错误,已在flutter_form_bloc: ^0.6.0中解决。