我又遇到麻烦了,希望有人能再次帮助我。
我想让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;
      });
    },
  ),

10-05 22:08