Flutter RefreshIndicator'无数据'小部件

时间:2019-02-19 19:24:02

标签: flutter pull-to-refresh

我有一个RefreshIndicator,可以在Feature完成时加载ListView,但是,我想这样做,以便如果Feature返回一个空的数据集-显示另一个小部件-'No data'代替ListView。

我能够使用三元运算符来实现这一点,但是,刷新期间会显示“无数据”小部件,直到被ListView取代为止。

仅在刷新完成后如何显示“无数据”小部件?

这是我的代码:

return RefreshIndicator(
    key: _refreshIndicatorKey,
    onRefresh: AppConfig.of(context).flavorName != FLAVOR_NAME.DEV
        ? () {
            bloc.page = 1;
            return bloc.getTasks(status, context).then((list) {
              setState(() {
                tasksList = list;
              });
            });
          }
        : () => Future.delayed(Duration(seconds: 2)),
    child: tasksList.length > 0
        ? ListView.builder(
            physics: const AlwaysScrollableScrollPhysics(),
            controller: _scrollController,
            itemCount: tasksList.length + 1,
            itemBuilder: (context, index) {
              if (index == tasksList.length) {
                return _buildProgressIndicator();
              } else {
                Task item = tasksList != null ? tasksList[index] : null;
                return GestureDetector(
                    onTap: () {
                      Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) {
                                return TaskDetailsScreen(index, item);
                              },
                              settings: RouteSettings(
                                  name: ScreenName.taskDetails)));
                    },
                    child: MyTasksListItem(taskData: item));
              }
            },
          )
        : Stack(children: [
            Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Image.asset(
                    'assets/tumbleweed.gif',
                  ),
                ]),
            Positioned(
                bottom: 50,
                left: 0.0,
                right: 0.0,
                child: Align(child: Text(allTranslations.text('no_tasks'))))
          ]));

在我的情况下,“无数据”小部件是堆叠的。

1 个答案:

答案 0 :(得分:0)

也许这样的解决方案会起作用:

 onRefresh: AppConfig.of(context).flavorName != FLAVOR_NAME.DEV
    ? () {
        setState(() {
            loading = true;
        });

        bloc.page = 1;
        return bloc.getTasks(status, context).then((list) {
          setState(() {
            loading = false;
            tasksList = list;
          });
        });
      }

并在您的指标孩子中:

child: Container(
    child: loading ?
       null :
       tasksList.length > 0
          ? ListView.builder(..........