Search code examples
flutterdartflutter-layoutflutter-dependenciesdart-pub

Why List-View Builder returns children with fixed height and width in SmartRefresher Widget?


I'm using the pull-to-refresh package, and by using the SmartRefresher widget I can't change the height or the width of List-View builder's children. It always gives me children of a full-screen width and fixed height.

I know that the problem is because I'm using SmartRefresher, but how can I solve it?

SmartRefresher(
      enablePullDown: true,
      enablePullUp: true,
      header: BezierCircleHeader(
        bezierColor: Colors.deepPurpleAccent,
        enableChildOverflow: false,
      ),
      footer: CustomFooter(
        builder: (BuildContext context, LoadStatus mode) {
          Widget body;
          if (mode == LoadStatus.idle) {
            body = Text("pull up load");
          } else if (mode == LoadStatus.loading) {
            body = CircularProgressIndicator();
          } else if (mode == LoadStatus.failed) {
            body = Text("Load Failed!Click retry!");
          } else if (mode == LoadStatus.canLoading) {
            body = Text("release to load more");
          } else {
            body = Text("No more Data");
          }
          return Container(
            height: 55.0,
            child: Center(child: body),
          );
        },
      ),
      controller: _refreshController,
      onRefresh: _onRefresh,
      onLoading: _onLoading,
      child: ListView.builder(
          itemCount: items.length,
          itemExtent: 100.0,
          itemBuilder: (ctx, i) {
Container(
                  width: 50,
                  height: 600,)
          }

It gives me this UI:

ScreenShot

And I want something like this:

ScreenShot

Ps: I can't stop using Pull-to-refresh


Solution

  • You can copy paste run full code below
    You can remove itemExtent

    code snippet

    ListView.builder(
                    padding: const EdgeInsets.all(8),
                    //itemExtent: 100,
                    itemCount: items.length,
                    itemBuilder: (BuildContext context, int index) {
                      return Container(
                        height: 400,
                        color: Colors.blue,
                        child: Card(child: Text('Entry ${items[index]}')),
                      );
                    })
    

    working demo

    enter image description here

    full code

    import 'package:flutter/material.dart';
    import 'package:pull_to_refresh/pull_to_refresh.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      List<String> items = ["1", "2", "3", "4", "5", "6", "7", "8"];
      RefreshController _refreshController =
          RefreshController(initialRefresh: false);
    
      void _onRefresh() async {
        // monitor network fetch
        await Future.delayed(Duration(milliseconds: 1000));
        // if failed,use refreshFailed()
        _refreshController.refreshCompleted();
      }
    
      void _onLoading() async {
        // monitor network fetch
        await Future.delayed(Duration(milliseconds: 1000));
        // if failed,use loadFailed(),if no data return,use LoadNodata()
        items.add((items.length + 1).toString());
        if (mounted) setState(() {});
        _refreshController.loadComplete();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: SmartRefresher(
                enablePullDown: true,
                enablePullUp: true,
                header: WaterDropHeader(),
                footer: CustomFooter(
                  builder: (BuildContext context, LoadStatus mode) {
                    Widget body;
                    if (mode == LoadStatus.idle) {
                      body = Text("pull up load");
                    } else if (mode == LoadStatus.loading) {
                      body = CircularProgressIndicator();
                    } else if (mode == LoadStatus.failed) {
                      body = Text("Load Failed!Click retry!");
                    } else if (mode == LoadStatus.canLoading) {
                      body = Text("release to load more");
                    } else {
                      body = Text("No more Data");
                    }
                    return Container(
                      height: 55.0,
                      child: Center(child: body),
                    );
                  },
                ),
                controller: _refreshController,
                onRefresh: _onRefresh,
                onLoading: _onLoading,
                child: ListView.builder(
                    padding: const EdgeInsets.all(8),
                    //itemExtent: 100,
                    itemCount: items.length,
                    itemBuilder: (BuildContext context, int index) {
                      return Container(
                        height: 400,
                        color: Colors.blue,
                        child: Card(child: Text('Entry ${items[index]}')),
                      );
                    })));
      }
    }