【问题标题】:Flutter AppBar (Actions) issue while using tabs使用选项卡时出现 Flutter AppBar (Actions) 问题
【发布时间】:2018-10-02 09:39:20
【问题描述】:

我在尝试 Flutter 时遇到了一个问题。 我有一个带有一些操作的 AppBar。 其中一项操作是日历小部件。我想要的行为将是通过新的日期选择来相应地更改我的脚手架上的数据。 问题是,虽然我已经设法完成了这种行为,但对我的 API 的调用执行了 两次。我已经确定问题是我放置的 RefreshIndicator (以便用户按需拉动以刷新页面),但我不明白为什么...... 由于某种原因,当我更改日期并因此更改数据时,它会将其识别为刷新状态,然后执行 _handleRefresh()。问题是,我仍然想要下拉刷新行为。

上的文件(tabView.dart 文件) Scaffold 的小部件树

RefreshIndicator(
     key: _modelRefreshKey,
     child: ListView.builder(
       itemCount: this._fetchedData?.length,
       itemBuilder: (BuildContext context, int index) {
         if (this._fetchedData!= null) {
           final MyModel myModel  = this._fetchedData[index];
           return (index == 0)
               ? ResultsLayout(
                   model: myModel  ,
                   lastUpdateTxt: myModel.someTXT,
                 )
               : MyModelInheritedWidget(
                   model: myModel,
                   child: ModelCardLayout(),
                 );
         } else {
           return EmptyWidget();
         }
       },
     ),
     onRefresh: _handleRefresh,
   ),

处理刷新功能

Future<Null> _handleRefresh() async {
    Completer<Null> completer = new Completer<Null>();
    this.getData().then((_) {
      completer.complete();
    });
    return completer.future;
  }

在选择新日期时,此函数执行再次刷新调用数据(hometab.dart 文件)

if (picked != null && picked != _selectedDate) {
      _selectedDate = picked;
      modelRefreshKey.currentState.widget.selectedDate = picked;
      modelRefreshKey.currentState?.getData();
    }

值得指出的是,日期方法位于我创建选项卡的位置,要刷新的实际数据是选项卡的一部分。我会提到这一点,以防它与我的问题有什么关系。

任何见解都会非常有帮助。

提前致谢。

【问题讨论】:

  • modelRefreshKey.currentState.widget.selectedDate = picked; 在做什么?
  • 这会将从 tabView.dart 中选择的日期设置为通过密钥访问的另一个小部件 (hometab.dart)。无论如何,这是我的“工作”实现。我可以让它以任何其他方式工作。我试图新建那个小部件,但它没有用。所以我结束了这个。可能有更好的方法来做到这一点。

标签: dart flutter


【解决方案1】:

我将有一个类似以下的变量来查看应用是否正在等待 API 响应:

_isWaitingForResponse = false;

Future  getData() {
  if(_isWaitingForResponse) return;
  _isWaitingForResponse = true;
  //change _isWaitingForResponse on api's response
}

【讨论】:

  • 抱歉,回复晚了。我会试一试,我会告诉你的。
  • 抱歉,回复晚了。我会试一试,我会告诉你的。
  • 天哪!!!它确实有效......非常感谢......虽然这样做有点讨厌,但现在它符合我的目的。
  • @Pan 是的,这是一种快速而肮脏的方式,但它可以完成工作,对吧?大声笑
猜你喜欢
  • 2020-09-10
  • 2019-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多