gpt4 book ai didi

flutter - 如何强制PhotoViewGallery类开始显示特定图像,而不是第一个图像

转载 作者:行者123 更新时间:2023-12-03 03:39:40 28 4
gpt4 key购买 nike

我又遇到麻烦了,希望有人能再次帮助我。
我想让PhotoViewGallery Class首先显示具有特定ID的页面
而不是“页面”列表的第一页。
在下面的代码中

import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
int _currentIndex = 0;

final List<String> listaPagine = [
'https://picsum.photos/id/451/200/300',
'https://picsum.photos/id/200/200/300'
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('# $_currentIndex'),
),
body: PhotoViewGallery.builder(
itemCount: listaPagine.length,
builder: (BuildContext context, int index) {
String myImg = listaPagine[index];

return PhotoViewGalleryPageOptions(
imageProvider: NetworkImage(myImg),
);
},
onPageChanged: (int index) {
setState(() {
_currentIndex = index;
});
},
),
);
}
}

由januw a写给我,并由* Aakash Kumar link解释,
我尝试了各种方法在PhotoViewGallery.builder中处理索引,但是我无法摆脱它。
我是新手,有人可以帮助我吗?
特别感谢mario

最佳答案

您需要添加 Controller :

int firstPage = 2;
PageController _pageController = PageController(initialPage: firstPage);

并将其附加到您的构建器
PhotoViewGallery.builder(
itemCount: listaPagine.length,
pageController: _pageController,
builder: (BuildContext context, int index) {
String myImg = listaPagine[index];

return PhotoViewGalleryPageOptions(
imageProvider: NetworkImage(myImg),
);
},
onPageChanged: (int index) {
setState(() {
_currentIndex = index;
});
},
),

关于flutter - 如何强制PhotoViewGallery类开始显示特定图像,而不是第一个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58051915/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com