Flutter - 当 PageView.builder 出现在屏幕上时,如何调用函数?

问题描述 投票:0回答:1

我的代码中有一个回调函数,当页面视图出现时,我知道有“onPageChanged”,但它只在你转到另一个页面时有效,而在你处于第一个初始页面时不起作用。

我在我的函数中使用了 PageView.builder 索引中的一些数据,这就是为什么我不能在 PageView 代码之外使用该函数的原因,所以有什么办法可以解决这个问题。

感谢您花时间帮助我。

这是一个类似的问题

如果你能解释一下那个问题,那对我来说会更好


    PageView.builder(
      itemCount: snapshot.data.length ?? 3,
      controller: controller,
      onPageChanged: (value) async {
        var prefs =
            await SharedPreferences.getInstance();
        await prefs.setString('img', img);
    
        print(img);
        getimg = img;
      },
      itemBuilder: (context, index) {
        img = '$api/wallpapers/' +
            snapshot.data[index]['wallpaper'];
        getimg = img;
        return Container(
          decoration: BoxDecoration(
            color: Colors.grey,
            image: DecorationImage(
                fit: BoxFit.fitWidth,
                image: NetworkImage(
                    '$api/wallpapers/' +
                        snapshot.data[index]
                            ['wallpaper'])),
          ),
         );
        },
       ),


flutter function dart sharedpreferences
1个回答
0
投票

我不太明白你的问题,但你可以在 itemBuilder 中调用一个函数,如果你想在特定页面上调用它,可以使用 if

itemBuilder: (context, index) {
 function();
img = '$api/wallpapers/' +
snapshot.data[index]['wallpaper']
© www.soinside.com 2019 - 2024. All rights reserved.