TextEditingController的先前状态未在Flutter中处理

时间:2019-01-05 20:30:53

标签: flutter

我正在使用自己的自定义TextEditingController将文本添加到// update all proyectos // that have a field "reuniones" which is an array // in which each item is an object with a field "participantes" that is an array // in which each item is an object that has a field "nomina" equal to 2 db.proyectos.update({}, { $set: { "reuniones.$[].participantes.$[j].firma": <your update> }, }, { arrayFilters: [ { "j.nomina": 2 } ] }) 中。我正在TextField方法中放置TextEditingController。问题是,当我在TextField中输入一些文本,然后返回上一屏幕并再次返回时。文本保留在TextField中,我不希望它发生。我想在屏幕从堆栈中弹出后立即将其清除。我该怎么解决?

dispose()

我的BLoC班:

class AddGoalsState extends State<AddGoalScreen> {
  GoalsBloc _bloc;
  TextEditingController myController = TextEditingController();

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _bloc = GoalsBlocProvider.of(context);
  }

  @override
  void dispose() {
    myController.dispose();
    _bloc.dispose();
    super.dispose();
  }
  Future<bool> _onWillPop(){
    Navigator.pop(context, false);
    return Future.value(false);
  }
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onWillPop,
      child: Scaffold(
        appBar: AppBar(
          title: Text(
            "Add Goal",
            style: TextStyle(color: Colors.black),
          ),
          backgroundColor: Colors.amber,
          elevation: 0.0,
        ),
        body: Container(
          padding: EdgeInsets.all(16.0),
          alignment: Alignment(0.0, 0.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              nameField(),
              Container(margin: EdgeInsets.only(top: 5.0, bottom: 5.0)),
              goalField(),
              Container(margin: EdgeInsets.only(top: 5.0, bottom: 5.0)),
              buttons(),
            ],
          ),
        ),
      ),
    );
  }

  Widget goalField() {
    return StreamBuilder(
        stream: _bloc.goalMessage,
        builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
          myController.value = myController.value.copyWith(text: snapshot.data);
          return TextField(
            controller: myController,
            keyboardType: TextInputType.multiline,
            maxLines: 3,
            onChanged: _bloc.changeGoalMessage,
            decoration: InputDecoration(
                hintText: "Enter your goal here", errorText: snapshot.error),
          );
        });
  }

0 个答案:

没有答案