Flutter 跳转到颤振碎片列表中的特定碎片

Flutter 跳转到颤振碎片列表中的特定碎片,flutter,key,flutter-sliver,customscrollview,Flutter,Key,Flutter Sliver,Customscrollview,如何以编程方式跳转(或滚动)到条子列表中条子高度不同的特定条子?下面的代码将书籍文本加载到自定义滚动视图中,每个小部件都有一个章节。当按下操作按钮时,我希望视图跳到第3章,但什么也没有发生。我做错了什么 class BookPage extends StatefulWidget { BookPage({Key? key, this.title = "book"}) : super(key: key); String title; final _chapter

如何以编程方式跳转(或滚动)到条子列表中条子高度不同的特定条子?下面的代码将书籍文本加载到自定义滚动视图中,每个小部件都有一个章节。当按下操作按钮时,我希望视图跳到第3章,但什么也没有发生。我做错了什么

  class BookPage extends StatefulWidget {
  BookPage({Key? key, this.title = "book"})  : super(key: key);
  String title;
  final _chapter3key = new GlobalKey(debugLabel: "chap3key");

  get chapter3Key => _chapter3key;

  @override
  _BookState createState() => _BookState();
}

class _BookState extends State<BookPage> {
  ScrollController? scrollController = new ScrollController();
  Map<int, String> chapterHTML = {};
  String title = "";
  final chapterCount=10;

  void fetchText() async {
    for (int i = 1; i <= 10; i++) {
      chapterHTML[i] = await getChapterHtml(i);
    }
    setState(() {});
  }

  _BookState() {
    fetchText();
  }

  Widget chapterSliver(int i) {
    if (i==3) {
      return Html(key: widget.chapter3Key, data: chapterHTML[i] ?? "", );
    }
    return Html(
      data: chapterHTML[i] ?? "",
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: CustomScrollView(controller: scrollController, slivers: [
          SliverList(
              delegate: SliverChildBuilderDelegate ((BuildContext context, int index) {
                  if (index > chapterCount) return null;
                  return chapterSliver(index);
                }// first sliver is empty (chapter 0!)
              ))
        ]),
        floatingActionButton: FloatingActionButton(onPressed: () {
          Scrollable.ensureVisible(widget.chapter3Key.currentContext);
        })
    );
  }
}
class BookPage扩展StatefulWidget{
BookPage({Key?Key,this.title=“book”}):超级(Key:Key);
字符串标题;
最终的_chapter3key=新的GlobalKey(调试标签:“chapter3key”);
get chapter3Key=>\u chapter3Key;
@凌驾
_BookState createState()=>\u BookState();
}
类_BookState扩展状态{
ScrollController?ScrollController=新的ScrollController();
Map chapterHTML={};
字符串标题=”;
最终章节数=10;
void fetchText()异步{
for(int i=1;i chapterCount)返回null;
返回章条(索引);
}//第一条是空的(第0章!)
))
]),
浮动操作按钮:浮动操作按钮(按下时:(){
可滚动。可确保可修改(widget.chapter3Key.currentContext);
})
);
}
}

对于任何发现此问题的人,我已经找到了解决方案-使用scrollablePositionedList-