/// /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved. /// /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES. /// This notice may not be removed from this file. import 'package:compdfkit_flutter/core/document/cpdf_document.dart'; import 'package:compdfkit_flutter/core/document/cpdf_outline_data.dart'; import 'package:flutter/material.dart'; import 'cpdf_outline_item.dart'; class CPDFOutlinePage extends StatefulWidget { final CPDFDocument document; const CPDFOutlinePage({Key? key, required this.document}) : super(key: key); @override State createState() => _CPDFOutlinePageState(); } class _CPDFOutlinePageState extends State with AutomaticKeepAliveClientMixin { List _outlineList = List.empty(growable: true); final globalKey = GlobalKey(); @override void initState() { super.initState(); widget.document.getOutline().then((value) { setState(() { _outlineList = value; }); }); } @override Widget build(BuildContext context) { return ListView.builder( itemCount: _outlineList.length, itemBuilder: (context, index) { return CPDFOutlineItem( outlineData: _outlineList[index], onDisplayPageIndex: (pageIndex) { Navigator.pop(context, pageIndex); }, onExpanded: (expanded) { setState(() { if (expanded) { loopCollapseItem(_outlineList[index]); } else { _outlineList[index].expanded = true; _outlineList.insertAll( index + 1, _outlineList[index].childOutline ?? []); } }); }, ); }); } @override bool get wantKeepAlive => true; void loopCollapseItem(CPDFOutlineData data) { data.expanded = false; data.childOutline?.forEach((element) { if (element.childOutline != null && element.childOutline!.isNotEmpty) { loopCollapseItem(element); } _outlineList.remove(element); }); } }