The original

Betterprogramming. Pub/how-to – disp…

code

Github.com/macro6461/f…

reference

  • Stackoverflow.com/questions/6…
  • Stackoverflow.com/questions/6…
  • Stackoverflow.com/users/16470…

The body of the

If you’re from my previous post, you saw that I launched my to-do app and fixed the problem that I couldn’t update NewToDo with the showModalBottomSheet method when UPDATING my status.

After asking a question on Stack Overflow, I was finally able to put my NewToDo widget in the showModalBottomSheet. This way, the NewToDo widget is only visible when I click FloatingActionButton, not necessarily all the time.

However, not long after I achieved this goal, I noticed another problem in my application.

When I choose a ToDo project to edit, I want to be able to reuse my NewToDo. I think this makes sense because it’s the same two inputs that can be used to change the same two state values, title and content.

What’s the problem?

I can’t execute the showModalBottomSheet (green arrow) anywhere except in the onPressed method in my FloatingActionButton widget (green circle).

I need to be able to trigger the onPressed method every time I click to edit the title ElevatedButton widget (red circle mark) for each ToDo project.

I considered how to find a way to emulate the onPressed event in order to execute the ShowmodBottomsheet callback.

Unable to emulate the onPressed event (and Frustrated), I took Stack Overflow to see if anyone else had any ideas on how to achieve what I was looking for.

After a while, I got my answer. I was relieved… . Be humble.

So simple… So pure

I took Eimmer’s advice and instead of putting my showModalBottomSheet in the onPressed method, I split it into its own function, renderShowModal. See below:

_renderShowModal(){
  return showModalBottomSheet<void>(
    context: context,
    builder: (BuildContext context) {
      return ValueListenableBuilder(
        valueListenable: titleController,
        builder: (context, _content, child) {
          returnNewToDo(titleController, contentController, _addTodo, _clear, _todo); }); }); }Copy the code

Once I’ve done that, I can override my onPressed method.

onPressed: _renderShowModal,
Copy the code

Now that we’ve put our showModalBottomSheet in a separate function, let’s see if it works:

That’s great! All we need to do now is call the same function at the end of the editTodo function, which will be called when you click edit EelevatedButton.

void _editTodo(ToDo todoitem){
  setState(() {
    _todo = todoitem;
    content = todoitem.content;
    title = todoitem.title;
  });
  contentController.text = todoitem.content;
  titleController.text = todoitem.title;
  _renderShowModal();
}
Copy the code

Now let’s see if it works when we click Edit next to the ToDo project.

Viola! viola

We can now use NewToDo to create NewToDo projects and edit existing ToDo projects.

By wrapping the showModalBottomSheet in a separate function, we can make our application render NewToDo invoking _renderShowModal at any time by calling renderShowModal.

All the code for the app is on GitHub.


The elder brother of the © cat

  • ducafecat.tech/

  • github.com/ducafecat

  • Ducafecat WeChat group