首页 > 解决方案 > BloC 的 TextEditingController 异常

问题描述

有人可以帮帮我吗?

我不明白为什么它会给出这个例外:

“处理后使用了 TextEditingController。”

按照我的逻辑,它不应该给我这个例外。

这是有问题的课程:

class _MobileLayoutState extends State<_MobileLayout> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  TextEditingController _textEditingController;

  @override
  void initState() {
    super.initState();
    _textEditingController = TextEditingController();
  }

  @override
  void dispose() {
    _textEditingController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: LayoutBuilder(builder: (context, constraints) {
        return FractionallySizedBox(
          widthFactor: 1.0,
          heightFactor: 1.0,
          child: Stack(
            alignment: Alignment.center,
            children: [
              Column(
                children: [
                  _buildSelectButton(
                      context,
                      "delivery_background.jpg",
                      S.of(context).orderDeliverySelectString,
                      constraints, onPressed: () {
                    orderType = OrderType.Delivery;
                  }),
                  _buildSelectButton(
                      context,
                      "restaurant_background.jpg",
                      S.of(context).orderRestaurantSelectString,
                      constraints, onPressed: () {
                    orderType = OrderType.Restaurant;
                    _showTableSelect(context);
                  }),
                ],
              ),
              _CenterWidget(),
            ],
          ),
        );
      }),
    );
  }

  _showTableSelect(BuildContext context) async {
    await showCustomRoundedDialog(
      context,
      title: S.of(context).tableSelectDialogTitle,
      body: Form(
        autovalidate: true,
        key: _formKey,
        child: TextFormField(
          controller: _textEditingController,
          decoration: InputDecoration(
            icon: Icon(Icons.restaurant_menu),
            hintText: S.of(context).tableSelectDialogTitle,
          ),
          keyboardType: TextInputType.number,
          validator: (String value) {
            return (value.isEmpty || !Validators.isNumber(value))
                ? S.of(context).tableSelectDialogErrorMsg
                : null;
          },
        ),
      ),
      actions: [
        Flexible(
          child: GestureDetector(
            child: Container(
              width: sv(300),
              child: Text(
                "Ok",
                style: bigTitleStyle.copyWith(
                  color: Colors.white,
                ),
                textAlign: TextAlign.center,
              ),
            ),
            onTap: () {
              if (_formKey.currentState.validate()) {
                if (mounted) {
                  currentTable = int.parse(_textEditingController.text.trim());
                }
                Navigator.pop(context);
              }
            },
          ),
        ),
      ],
    );
    if (currentTable != null) {
      BlocProvider.of<MainNavigationBloc>(context)
          .add(MainNavigationEvent.ProdCategoryShown);
    }
  }

  Widget _buildSelectButton(BuildContext context, String imageName, String text,
      BoxConstraints constraints,
      {Function onPressed}) {
    return GestureDetector(
      onTap: onPressed,
      child: Container(
        width: constraints.maxWidth,
        height: (constraints.maxHeight * 0.5),
        decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage("$imageAssetsPath/$imageName"),
              fit: BoxFit.cover),
        ),
        child: Container(
          color: Colors.black45.withAlpha(120),
          child: Center(
            child: Text(
              text,
              style: bigTitleStyle.copyWith(color: Colors.white),
            ),
          ),
        ),
      ),
    );
  }
}

弹出对话框后我没有使用控制器,那么为什么它会给我这个异常呢?

该应用程序是使用带有 flutter_bloc 包的 BloC 模式开发的。

标签: flutterbloc

解决方案


推荐阅读