original
https://betterprogramming.pub/how-to-display-the-modal-bottom-sheet-programmatically-in-flutter-d1fad5fdd462
Code
https://github.com/macro6461/flutter_to_do
refer to
- https://stackoverflow.com/questions/69471054/unable-to-reflect-updated-parent-state-in-showmodalbottomsheet
- https://stackoverflow.com/questions/69486752/trigger-floatingactionbutton-onpressed-without-pressing-the-button
- https://stackoverflow.com/users/1647098/eimmer
text
If you came from my previous post, you saw that I started my to-do application, which solved the problem that I couldn’t use the showModalBottomSheet method to update NewToDo
when my status was updated.
After Stack Overflow , I can finally put my NewToDo
widget in showModalBottomSheet
. In this way, the NewToDo widget is only
FloatingActionButton
, instead of having to be visible all the time.
However, shortly after achieving this goal, I noticed that there was still a problem in my application.
When I select the ToDo
project to edit, I want to be able to reuse my NewToDo
. I think this makes sense because it is the same two inputs that can be used to change the same two state values, title
and content
.
What's the question?
onPressed
method in my FloatingActionButton
widget (green circle), I cannot execute showModalBottomSheet
(green arrow) anywhere.
I need to be able to trigger the onPressed
method, whenever I click to edit the title of the ElevatedButton
widget (marked with a red circle), for each ToDo
item.
I considered how to find a way to simulate the onPressed
event in order to execute the showmodbottomsheet
callback.
Since it was impossible to simulate the onPressed
event (and frustrated), I took Stack Overflow to see if anyone else had any ideas on how to achieve the goal I was looking for.
After a while, I got the answer. I was relieved... and humbled.
So simple...so pure
I took the eimmer's suggestion, did not put my showModalBottomSheet
in the onPressed
method, but decomposed 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) {
return NewToDo(titleController, contentController, _addTodo, _clear, _todo);
});
},
);
}
After doing this, I can rewrite my onPressed
method.
onPressed: _renderShowModal,
Now that we have put our showModalBottomSheet
in a separate function, let's see if it works:
marvelous! We now need to do is editTodo
end of the function calls the same function, when you click on the Edit EelevatedButton
when this function is called.
void _editTodo(ToDo todoitem){
setState(() {
_todo = todoitem;
content = todoitem.content;
title = todoitem.title;
});
contentController.text = todoitem.content;
titleController.text = todoitem.title;
_renderShowModal();
}
Now let's see if it works ToDo
next to the edit
Viola! Viola
We can now use NewToDo
to create new ToDo
projects and edit existing ToDo
projects.
By showModalBottomSheet
in a separate function, we can call renderShowModal
so that our application can present NewToDo
invoking _renderShowModal
at any time.
All the code for this application is on GitHub .
© Cat brother
- https://ducafecat.tech/
- https://github.com/ducafecat
- WeChat group ducafecat
- b Station https://space.bilibili.com/404904528
Past
Open source
GetX Quick Start
https://github.com/ducafecat/getx_quick_start
News client
https://github.com/ducafecat/flutter_learn_news
strapi manual translation
WeChat discussion group ducafecat
Series collection
Translation
https://ducafecat.tech/categories/%E8%AF%91%E6%96%87/
Open source project
https://ducafecat.tech/categories/%E5%BC%80%E6%BA%90/
Dart programming language basics
https://space.bilibili.com/404904528/channel/detail?cid=111585
Getting started with Flutter zero foundation
https://space.bilibili.com/404904528/channel/detail?cid=123470
Flutter actual combat from scratch news client
https://space.bilibili.com/404904528/channel/detail?cid=106755
Flutter component development
https://space.bilibili.com/404904528/channel/detail?cid=144262
Flutter Bloc
https://space.bilibili.com/404904528/channel/detail?cid=177519
Flutter Getx4
https://space.bilibili.com/404904528/channel/detail?cid=177514
Docker Yapi
https://space.bilibili.com/404904528/channel/detail?cid=130578
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。