import 'package:compdfkit_flutter/core/cpdf_view_ctrl.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'dart:ui' as ui; import '../slider/cpdf_slider_theme.dart'; /// 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. class CPDFPageNavigationSliderBar extends StatefulWidget { final CPDFSliderIndicatorPosition position; final CPDFViewCtrl ctrl; const CPDFPageNavigationSliderBar( {super.key, required this.ctrl, required this.position}); @override State createState() => _CPDFPageNavigationSliderBarState(); } class _CPDFPageNavigationSliderBarState extends State { int _pageCount = 1; @override void initState() { super.initState(); _initDocumentData(); } void _initDocumentData() async { try{ int pageCount = await widget.ctrl.document.getPageCount(); setState(() { _pageCount = pageCount; }); }catch(e){ } } @override Widget build(BuildContext context) { return StreamBuilder( initialData: 0, stream: widget.ctrl.currentPageIndexStream.stream, builder: (context, pageIndex) { if (pageIndex.data != null) { return CPDFSliderBar( position: widget.position, value: (pageIndex.data! + 1).toDouble(), maxValue: _pageCount.toDouble(), callback: (pageIndex) { widget.ctrl.setDisplayPageIndex(pageIndex); }, ); } else { return CPDFSliderBar( position: widget.position, value: 1, maxValue: _pageCount.toDouble(), callback: (pageIndex) { widget.ctrl.setDisplayPageIndex(pageIndex); }, ); } }, ); } } typedef CPDFPageNavigationCallback = void Function(int pageIndex); class CPDFSliderBar extends StatefulWidget { final double sliderBarWidth; final double sliderBarHeight; final double maxValue; double value; final CPDFSliderIndicatorPosition position; final CPDFPageNavigationCallback? callback; CPDFSliderBar( {super.key, required this.value, required this.maxValue, this.position = CPDFSliderIndicatorPosition.right, this.sliderBarWidth = 36, this.sliderBarHeight = 24, this.callback}); @override State createState() => _CPDFSliderBarState(); } class _CPDFSliderBarState extends State { ui.Image? image; @override void initState() { super.initState(); _initImage(); } void _initImage() async { final data = await rootBundle .load('packages/compdfkit_flutter/assets/images/ic_slider_bar.png'); final bytes = data.buffer.asUint8List(); final image1 = await decodeImageFromList(bytes); setState(() { image = image1; }); } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { bool isVertical = true; if (widget.position == CPDFSliderIndicatorPosition.top || widget.position == CPDFSliderIndicatorPosition.bottom) { isVertical = false; } if (image != null) { return RotatedBox( quarterTurns: isVertical ? 1 : 0, child: SliderTheme( data: SliderTheme.of(context).copyWith( inactiveTickMarkColor: Colors.transparent, inactiveTrackColor: Colors.transparent, activeTrackColor: Colors.transparent, trackHeight: 10, overlayShape: SliderComponentShape.noOverlay, thumbShape: CPDFImageSliderThumbShape( image: image!, imageWidth: widget.sliderBarWidth, imageHeight: widget.sliderBarHeight), thumbColor: Colors.white, valueIndicatorShape: CPDFPageSliderValueIndicatorShape( position: widget.position), showValueIndicator: ShowValueIndicator.always, valueIndicatorColor: Colors.black, valueIndicatorTextStyle: const TextStyle(color: Colors.white, fontSize: 12)), child: Slider( value: widget.value, min: 1, label: '${widget.value.toInt()}/${widget.maxValue.toInt()}', max: widget.maxValue, onChanged: (newValue) { setState(() { widget.value = newValue; }); }, onChangeEnd: (newValue) { if (widget.callback != null) { widget.callback!((newValue - 1).toInt()); } }, ), )); } else { return const Padding(padding: EdgeInsets.zero); } } }