简体   繁体   English

Flutter 关闭键盘后底页重新加载问题

[英]Flutter Bottomsheet reload problem after dismissing keyboard

In a todoapp, am trying to edit a single task from a list of Tasks.在待办事项应用程序中,我试图从任务列表中编辑单个任务。 After clicking on edit icon, I was able to open a bottomsheet widget using showmodalbottomsheet function with pre filled two text fields using texteditingcontrollers.单击编辑图标后,我可以使用 showmodalbottomsheet function 打开一个底页小部件,并使用 texteditingcontrollers 预填充两个文本字段。 When I tried to edit a single text field and dismissed the keyboard, the bottomsheet is loading again the previous value.I am unable to save my edited value..pls help当我尝试编辑单个文本字段并关闭键盘时,底页再次加载以前的值。我无法保存我编辑的值..请帮助

I used stateless bottomsheet widget.我使用了无状态 bottomsheet 小部件。

Heres the Code.这是代码。 I know this not ideal way of coding.我知道这不是理想的编码方式。 but am trying to get to work the functionality, later i will work on optimisation.但我正在尝试开始使用该功能,稍后我将进行优化。

Tasklist.dart任务列表.dart

IconButton( icon: Icon(Icons.edit), onPressed: (){ _startEditTask(context, index);//index is the single task from the lisview },), IconButton( icon: Icon(Icons.edit), onPressed: (){ _startEditTask(context, index);//index是来自lisview的单个任务 },),

void _startEditTask(BuildContext ctx, int index){
    showModalBottomSheet(context: ctx, builder: (btx){
      return GestureDetector(
        onTap: (){},
        child: EditTask(_taskList[index].taskName,_taskList[index].description,index,_editTask),
      );
    });
  }

void _editTask(String taskName, String description, int index){
    setState(() {
      _taskList[index].taskName = taskName;
      _taskList[index].description = description;
    });
  }

EditTask.dart EditTask.dart

class EditTask extends StatelessWidget {
  EditTask(this.taskName,this.description,this.index,this.editTask);
  String taskName;
  String description;
  int index;
  final Function editTask;
  var taskNameController = TextEditingController();
  var taskDescriptionController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    taskNameController.text = taskName;
    taskDescriptionController.text = description;
    return Column(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        TextField(
          controller: taskNameController,
          decoration: InputDecoration(
              labelText: 'Task Name'
          ),

        ),
        TextField(
          controller: taskDescriptionController,
          decoration: InputDecoration(
              labelText: 'Description'
          ),
        ),
        TextButton(
          child: Text('Save Task'),
          onPressed: (){
            editTask(taskNameController.text,taskDescriptionController.text,index);
          Navigator.of(context).pop();
          },
        )
      ],
    );
  }
}
StatelessWidget

to

StatefulWidget 

will solve your issue ---- try now将解决您的问题----现在试试


class EditTask extends StatelessWidget

to

class EditTask extends StatefulWidget

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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