【问题标题】:Flutter : run some code after a ListView has builtFlutter:在 ListView 构建后运行一些代码
【发布时间】:2021-03-24 16:15:52
【问题描述】:

我想在 ListView.builder() 构建后运行此代码。

if(listViewScrollController.hasClients)
     listViewScrollController.jumpTo(
         listViewScrollController.position.maxScrollExtent,
      );

这将允许用户直接出现在聊天屏幕页面的底部。 这需要在 ListView 构建其项目后执行,否则 listViewScrollController.position.maxScrollExtent 仍将是 0 而不是实际值。

【问题讨论】:

    标签: flutter listview dart scroll


    【解决方案1】:

    您可以使用可见的小部件并在 ListView 已加载时显示

    在 pubspec.yaml 文件中

    dependencies:
      flutter:
        sdk: flutter
      fluttertoast: ^3.1.0
      http: ^0.12.0+2
    

    在 main.dart 文件中

    import 'dart:async';
    import 'dart:convert';
    import 'package:flutter/foundation.dart';
    import 'package:flutter/material.dart';
    import 'package:fluttertoast/fluttertoast.dart';
    import 'package:http/http.dart' as http;
    

    在类 _MyHomePageState 中声明以下变量

    bool _visible=false;
    final List<String> entries = <String>[];
    

    创建一个函数来填充列表

    void toastmsm(msm,stad) {
        Fluttertoast.showToast(
            msg: msm,
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.BOTTOM,
            backgroundColor: stad,
            textColor: Colors.white,
            fontSize: 16.0
        );
    }
    
    Future<String> apiRequest(String url) async {
        var response = await http.get(url);
        return response.body;
    }
    
    void getprofil() async{
        //expample.
        entries.clear();
        var enlace="https://www.profilusers.com/list.json";
        var graphql=json.decode(await apiRequest(enlace));
        if(graphql.length==1){
            entries.add(graphql["graphql"]["users"]["url"].toString());
        }
        else{
            toastmsm("no data.", Colors.red);
        }
        setState(() {
          if(graphql.length==1){
            _visible=true;
          }
        });
    }
    

    小部件构建

    Visibility(
        visible: _visible,
        child: Column(
            children: [
                ListView.builder(
                    shrinkWrap: true,
                    physics: const BouncingScrollPhysics(),
                    itemCount: entries.length,
                    itemBuilder: (BuildContext context, int index) =>
                    ListBody(
                        children: [
                            Image.network('${entries[index]}'),
                        ],
                    ),
                ),
            ],
        ),
    ),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-31
      • 2019-07-24
      • 2021-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 2019-07-11
      相关资源
      最近更新 更多