【问题标题】:How to get ScrollListener to work with FutureBuilder?如何让 ScrollListener 与 FutureBuilder 一起工作?
【发布时间】:2020-07-11 07:56:15
【问题描述】:

关注this 以检测何时到达列表末尾,但似乎 ScrollListener 无法正常工作,导致我调试的打印功能也没有输出

列表使用按钮成功扩展,但我的计划是去掉按钮并在到达底部时自动添加

class _AllProjectsState extends State<AllProjects> {

  ScrollController sc;

  void initstate() {
    sc = ScrollController();
    super.initState();
    sc.addListener(scListener);
  }

  scListener() {
    print("Changed");
    if( sc.offset >= sc.position.maxScrollExtent && !sc.position.outOfRange) {
      setState(() {
        print("Reached");
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Project Info'),
      ),
      backgroundColor: Colors.white,
      body:FutureBuilder<Data>(
        future: getData(),
        builder: (context, snapshot) {

          if(snapshot.hasData)
            return ListView.builder(
              controller: sc,
              itemCount: snapshot.data.projects.length,
              itemBuilder: (context,index) {
                return ListTile(
                  title: Text('${snapshot.data.projects[index].attributes.name}',style: TextStyle(fontSize: 30),),
                );
              }
            );
          else
            return Text('NotFound');
        }
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          if(!full) {
            setState(() {
              page+=1;
            }); 
          }
        },
        child: Icon(Icons.add),
      ),
    );
  }
}

【问题讨论】:

    标签: list flutter scrollview


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    你有错字void initstate() 需要@override

    代码sn-p

    @override
      void initState() {
    

    工作演示

    完整代码

    import 'package:flutter/material.dart';
    
    class Attributes {
      String name;
    
      Attributes({this.name});
    }
    
    class Project {
      Attributes attributes;
    
      Project({this.attributes});
    }
    
    class Data {
      List<Project> projects;
    
      Data({this.projects});
    }
    
    class AllProjects extends StatefulWidget {
      @override
      _AllProjectsState createState() => _AllProjectsState();
    }
    
    class _AllProjectsState extends State<AllProjects> {
      ScrollController sc;
      Project project = Project(attributes: Attributes(name: "test"));
      List<Project> projectList = [];
    
      @override
      void initState() {
        projectList.add(project);
        projectList.add(project);
        projectList.add(project);
        projectList.add(project);
        projectList.add(project);
        projectList.add(project);
        projectList.add(project);
        projectList.add(project);
        projectList.add(project);
        projectList.add(project);
        projectList.add(project);
        projectList.add(project);
        sc = ScrollController();
        super.initState();
        sc.addListener(scListener);
      }
    
      scListener() {
        print("Changed");
        if (sc.offset >= sc.position.maxScrollExtent && !sc.position.outOfRange) {
          setState(() {
            print("Reached");
          });
        }
      }
    
      Future<Data> getData() {
        Project project = Project(attributes: Attributes(name: "123"));
        projectList.add(project);
        Data data = Data(projects: projectList);
        return Future.value(data);
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Project Info'),
          ),
          backgroundColor: Colors.white,
          body: FutureBuilder<Data>(
              future: getData(),
              builder: (context, snapshot) {
                if (snapshot.hasData)
                  return ListView.builder(
                      controller: sc,
                      itemCount: snapshot.data.projects.length,
                      itemBuilder: (context, index) {
                        return ListTile(
                          title: Text(
                            '${snapshot.data.projects[index].attributes.name}',
                            style: TextStyle(fontSize: 30),
                          ),
                        );
                      });
                else
                  return Text('NotFound');
              }),
          floatingActionButton: FloatingActionButton(
            onPressed: () {
              /*if (!full) {
                setState(() {
                  page += 1;
                });
              }*/
            },
            child: Icon(Icons.add),
          ),
        );
      }
    }
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: AllProjects(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-02-02
      • 2017-06-25
      • 2020-12-13
      • 2012-02-02
      • 2011-02-22
      • 2013-03-24
      • 2016-07-22
      • 2011-08-17
      • 2021-12-18
      相关资源
      最近更新 更多