简体   繁体   English

Flutter - 嵌套 OnTap 不适用于 ListTile

[英]Flutter - Nested OnTap doesn't work on ListTile

1. When I open Dialog, click item3 in ListView. 1.当我打开Dialog时,点击ListView中的item3。

2. But, Item3 background color doesn't changed. 2. 但是,Item3 背景颜色没有改变。

3. When I open Dialog again, item3's background color in ListView changed. 3.当我再次打开Dialog时,ListView中item3的背景颜色发生了变化。

child: TextField(
            readOnly: true,
            decoration: InputDecoration(
              icon: Icon(Icons.arrow_drop_down),
            ),
            onTap: () {
              // var dialog = AlertDialog(
              Get.defaultDialog(
                title: '',
                // shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),

                content: Container(
                    width: MediaQuery.of(context).size.width / 1.5,
                    height: MediaQuery.of(context).size.height / 3,
                    child: Scrollbar(
                      isAlwaysShown: true,
                      child: ListView.builder(
                          shrinkWrap: true,
                          itemCount: _list.length,
                          itemBuilder: (context, index) {
                            return new Container(
                                color: this.tappedIndex == index ? Colors.grey : Colors.white,
                                child: ListTile(
                                  title: Text(_list[index]),
                                  onTap: () {
                                    setState(() {
                                      this.tappedIndex = index;
                                      // dropdownValue = _list[index];
                                    });
                                  },
                                )
                            );
                          })
                    ),
                  ),
)

I'm not entirely sure how getx handles the dialogs (that is, how Get.defaultDialog works) but assuming it's simillar to showDialog , then you can't refresh a widget inside of a dialog without using an independent state management inside of a dialog or a StreamBuilder我不完全确定 getx 如何处理对话框(即Get.defaultDialog如何工作),但假设它与showDialog类似,那么如果不使用对话框内部的独立 state 管理,则无法刷新对话框内部的小部件或StreamBuilder

Basically you have to基本上你必须

  • transform tappedIndex into a StreamController , eg BehaviorSubject from rxdarttappedIndex转换为StreamController ,例如来自rxdartBehaviorSubject
  • wrap Get.defaultDialog 's content parameter with a StreamBuilder (or one of the children that depends should be rebuilt upon value change, ListView in your case)StreamBuilder包装Get.defaultDialogcontent参数(或者应该根据值更改重建依赖的子项之一,在您的情况下为ListView
  • subscribe to tappedIndex inside the newly created StreamBuilder在新创建的StreamBuilder中订阅tappedIndex

Or alternatively create a StatefulWidget that would be placed inside of your dialog, that upon state change would also update the index value of a StatefulWidget outside the dialog.或者,创建一个将放置在对话框内的StatefulWidget ,它在 state 更改时也会更新对话框外StatefulWidgetindex值。

Personally, I think the solution with Stream s is the way to go.就个人而言,我认为 Stream 的解决方案是Stream的方法。

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

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