颤动:使用RefreshIndicator

时间:2019-01-05 10:36:17

标签: flutter pull-to-refresh

我在页面上添加了RefreshIndicator,但是在刷新时没有可见的指示器。代码如下:

class HomePage extends StatefulWidget {
  HomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<HomePage> {

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: LocalGalleryTab(),
    );
  }
}

class LocalGalleryTab extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _LocalGalleryState();
  }
}

class _LocalGalleryState extends State<LocalGalleryTab> {
  @override
  Widget build(BuildContext context) {
    return new Container(child: new Center(
      child: new RefreshIndicator(
        child: Text("Local Gallery"),
        onRefresh: _refreshLocalGallery,
      ),
    ));
  }

  Future<Null> _refreshLocalGallery() async{
    print('refreshing stocks...');

  }
}

如何使用RefreshIndicator?颤抖的doc并没有提供太多信息。

3 个答案:

答案 0 :(得分:14)

根据设计,RefreshIndicatorListView一起使用。

但是,如果您想将RefreshIndicator与不可滚动的小部件一起使用,则可以使用Stack将小部件包装到ListView中:

RefreshIndicator(
  onRefresh: () {},
  child: Stack(
    children: <Widget>[ListView(), YOUR_CHILD_WIDGET],
  ),
),

答案 1 :(得分:3)

使用AlwaysScrollableScrollPhysics()将确保滚动视图始终可滚动,因此可以触发RefreshIndicator

现在我想用BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics)而不是AlwaysScrollableScrollPhysics()来创建滚动物理。因为BouncingScrollPhysics可以用于跳出的短列表。现在,必须包含AlwaysScrollableScrollPhysics以实现预期的行为。

RefreshIndicator(
    onRefresh: _onRefresh,
    child: ListView(
      padding: EdgeInsets.all(8.0),
      physics: const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
      children: _listData.map((i) {
        return ListTile(
          title: Text("Item $i"),
        );
      }).toList(),
    )
);

enter image description here

答案 2 :(得分:2)

您需要在//previous codes... char userChoice = 'n'; do{ // previous codes which you want to be repeated std::cout << "\nDo you want to continue ? (y = Yes, n = No ) "; // ask the user to continue or to end std::cin >> userChoice; // store user's input in the userChoice variable }while(userChoice); // if user's input is 'y' then continue with next iteration //rest of the code... 内添加滚动子级 参见下面的示例

enter image description here

RefreshIndicator