Untitled

mail@pastecode.io avatar
unknown
plain_text
16 days ago
102 kB
0
Indexable
Never
import 'dart:collection';
import 'dart:convert';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get_connect/http/src/utils/utils.dart';
import 'package:gurbani_app/models/baani_lines_model.dart';
import 'package:gurbani_app/models/calculated_items.dart';
import 'package:gurbani_app/models/db_result.dart';
import 'package:gurbani_app/models/history_item.dart';
import 'package:gurbani_app/screens/settings.dart';
import 'package:gurbani_app/services/nitnem_service.dart';
import 'package:gurbani_app/services/reader.dart';
import 'package:gurbani_app/utils/contexts.dart';
import 'package:gurbani_app/utils/languages.dart';
import 'package:gurbani_app/utils/spans.dart';
import 'package:gurbani_app/utils/theme.dart';
import 'package:gurbani_app/widgets/baani_view_widget.dart';
import 'dart:math' as math;

import 'package:flip_widget/flip_widget.dart';
import 'package:gurbani_app/widgets/calculated_view_widget.dart';
import 'package:gurbani_app/widgets/constraints.dart';
import 'package:gurbani_app/widgets/helper.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

//ToDo: This class has to be renamed because this won't be the homescreen

// Future<void> updateGurbaniLists() async {
//   // print(_linesPerPage);
//   currentAng = await Reader.getAngs(sourcePageNo: _sourcePageNo,pageNo: _pageNo, loadSourceLines: true, lines: _linesPerPage, bookNo: _bookNo);
//   if(currentAng.baaniLines.isEmpty){
//     _sourcePageNo = _sourcePageNo + 1;
//     _previousChapterTotalPages.add(_pageNo > 1? _pageNo-1 :1);
//     _pageNo = 1;
//     currentAng = await Reader.getAngs(sourcePageNo: _sourcePageNo,pageNo: _pageNo, lines: _linesPerPage, bookNo: _bookNo);
//   }
//   nextAng = await Reader.getAngs(sourcePageNo: _sourcePageNo,pageNo: _pageNo + 1, lines: _linesPerPage, bookNo: _bookNo);
//   previousAng = _sourcePageNo ==1 && _pageNo ==1 ? DBResult(baaniLines: List.empty(), count: 0) : _sourcePageNo >=1 && _pageNo ==1 ? _previousChapterTotalPages.isEmpty ? DBResult(baaniLines: List.empty(), count: 0) : await Reader.getAngs(sourcePageNo: _sourcePageNo - 1,pageNo: _previousChapterTotalPages.last, lines: _linesPerPage) : await Reader.getAngs(sourcePageNo: _sourcePageNo,pageNo: _pageNo - 1, lines: _linesPerPage);
//   setState(() {});
// }

class PageScreen extends StatefulWidget {
  final int pageNo;
  final int bookNo;
  final bool isNitnem;
  // final bool isEnglishTransliteration;
  final int nitnemId;
  final bool searchView;
  final int searchedAng;
  final String title;
  final BaaniLineModel? searchedBaaniLine;

  const PageScreen(
      {super.key,
      required this.title,
      this.pageNo = 1,
      this.bookNo = 1,
      // this.isEnglishTransliteration = false,
      this.isNitnem = false,
      this.searchView = false,
      this.nitnemId = 1,
      this.searchedAng = 100,
      this.searchedBaaniLine});

  @override
  State<PageScreen> createState() => _PageScreenState();
}

const double _MinNumber = 0.008;
double _clampMin(double v) {
  if (v < _MinNumber && v > -_MinNumber) {
    if (v >= 0) {
      v = _MinNumber;
    } else {
      v = -_MinNumber;
    }
  }
  return v;
}

class _PageScreenState extends State<PageScreen> {
  final GlobalKey<FlipWidgetState> _flipKey = GlobalKey();
  final GlobalKey myPageKey = GlobalKey();
  final TextEditingController _searchController = TextEditingController();

  DBResult currentAng =
      DBResult(baaniLines: LinkedList<BaaniLineModel>(), count: 0);
  DBResult previousAng =
      DBResult(baaniLines: LinkedList<BaaniLineModel>(), count: 0);
  DBResult nextAng =
      DBResult(baaniLines: LinkedList<BaaniLineModel>(), count: 0);
  DBResult searchedAng =
      DBResult(baaniLines: LinkedList<BaaniLineModel>(), count: 0);
  LinkedList<CalculatedItems> calculatedPages = LinkedList<CalculatedItems>();
  LinkedList<HistoryItem> _history = LinkedList<HistoryItem>();
  late SharedPreferences prefs;
  bool _showEnglishTransliteration = false;
  bool _showEnglishTranslation = false;
  bool _showPunjabiTranslation = false;
  bool _showPunjabiTeekaTranslation = false;
  bool _showHindiTranslation = false;
  bool _showHindiTeekaTranslation = false;
  bool nightmodevalue = false;
  bool _showFaridkotTeekaTranslation = false;
  bool _loadSourceLines = true;
  bool _isBookMarked = false;
  bool _initialized = false;
  bool _isbold = false;
  bool isbold = false;
  bool _searchviewm = false;
  Languages _language = Languages.Gurmukhi;
  int _loadedLines = 0;
  int _linesPerProcess = 20;
  double _extraBottomPadding = 0;
  bool _isDragging = false;
  bool _showSearch = false;
  int maxvalueofBook1 = 1430;
  int maxvalueofBook2 = 1428;
  int maxvalueofBook3 = 41;
  int maxvalueofBook4 = 675;
  int maxvalueofBook5 = 65;
  int maxvalueofBook6 = 1;
  int maxvalueofBook7 = 10;
  int maxvalueofBook8 = 2;
  String errorMessage = '';
  final ItemScrollController _itemScrollController = ItemScrollController();
  final ItemPositionsListener _itemPositionListener =
      ItemPositionsListener.create();

  //late Size size;

  Future<void> updateGurbaniLists() async {
    currentAng = await Reader.getAngs(
        sourcePageNo: _sourcePageNo,
        pageNo: _pageNo,
        loadSourceLines: _loadSourceLines,
        lines: _linesPerPage,
        bookNo: _bookNo);
    if (currentAng.baaniLines.isEmpty) {
      if (_loadSourceLines) {
        currentAng =
            DBResult(baaniLines: LinkedList<BaaniLineModel>(), count: 0);
        nextAng = DBResult(baaniLines: LinkedList<BaaniLineModel>(), count: 0);
        placeWidgetsToPage();
        setState(() {});
        _loadedLines = _linesPerProcess;
        return;
      }
      _sourcePageNo = _sourcePageNo + 1;
      _previousChapterTotalPages.add(_pageNo > 1 ? _pageNo - 1 : 1);
      _pageNo = 1;
      currentAng = await Reader.getAngs(
          sourcePageNo: _sourcePageNo,
          pageNo: _pageNo,
          lines: _linesPerPage,
          bookNo: _bookNo);
      // placeWidgetsToPage();
    } else {
      if (_loadSourceLines) {
        if (_loadedLines >= currentAng.count) {
          return;
        }
        placeWidgetsToPage();
        setState(() {});
        _loadedLines += _linesPerProcess;
        return;
      }
    }
    if (_loadedLines >= currentAng.count) {
      return;
    }
    placeWidgetsToPage();
    nextAng = await Reader.getAngs(
        sourcePageNo: _sourcePageNo,
        pageNo: _pageNo + 1,
        lines: _linesPerPage,
        bookNo: _bookNo);
    previousAng = _sourcePageNo == 1 && _pageNo == 1
        ? DBResult(baaniLines: LinkedList<BaaniLineModel>(), count: 0)
        : _sourcePageNo >= 1 && _pageNo == 1
            ? _previousChapterTotalPages.isEmpty
                ? DBResult(baaniLines: LinkedList<BaaniLineModel>(), count: 0)
                : await Reader.getAngs(
                    sourcePageNo: _sourcePageNo - 1,
                    pageNo: _previousChapterTotalPages.last,
                    lines: _linesPerPage)
            : await Reader.getAngs(
                sourcePageNo: _sourcePageNo,
                pageNo: _pageNo - 1,
                lines: _linesPerPage);
    setState(() {
      // print("calcualated");
      // print(calculatedPages.length);
    });
    _loadedLines += _linesPerProcess;
  }

  double _getAppbarAndStatusBarHeight() {
    if (!_initialized) {
      return 0.0;
    }
    double h = AppBar().preferredSize.height +
        MediaQuery.of(context).padding.top +
        MediaQuery.of(context).padding.bottom +
        _extraBottomPadding;
    return h;
  }

  placeWidgetsToPage() {
    if (currentAng.baaniLines.isEmpty) {
      calculatedPages.clear();
      return;
    }

    TextSpan angSpan = getSpan("Ang-",
        color: Colors.white,
        style: const TextStyle(
            fontWeight: FontWeight.bold)); // Set the text color to red

    double height =
        getTextHeight(span: angSpan, maxWidth: Contexts.size!.width - 2 * 8.w);
    double additionalHeight = 8.h;
    height += additionalHeight;
    CalculatedItems calculatedItems = CalculatedItems(
        textSpans: List.empty(growable: true),
        pageNo: 0,
        totalPages: 0,
        angNo: _sourcePageNo,

        // angNo: _pageNo,
        height: height);
    if (_loadedLines != 0) {
      calculatedItems = calculatedPages.last;
      height = calculatedItems.height;
      calculatedPages.last.unlink();
    }

    SizedBox(
      height: 12.h,
    );

    currentAng.baaniLines
        .skip(_loadedLines)
        .take(_linesPerProcess)
        .forEach((element) {
      if (_language == Languages.Both) {
        if (element.gurmukhi.isNotEmpty) {
          TextSpan span = getAsscciiSpan(element.gurmukhi,
              style: baaniTextStyle(
                color: nightmodevalue ? blackColor : whiteColor,
                fontWeight: _isbold ? FontWeight.bold : FontWeight.normal,
              ));
          if (height +
                  _getAppbarAndStatusBarHeight() +
                  getTextHeight(
                      span: span, maxWidth: Contexts.size!.width - 2 * 8.w) >
              Contexts.size!.height) {
            calculatedItems.pageNo += 1;
            calculatedItems.totalPages += 1;
            calculatedItems.height = height;
            calculatedPages.add(calculatedItems);
            semiUpdate();
            height = getTextHeight(
                span: angSpan, maxWidth: Contexts.size!.width - 2 * 8.w);
            calculatedItems = CalculatedItems(
                textSpans: List.empty(growable: true),
                pageNo: calculatedItems.pageNo,
                totalPages: calculatedItems.totalPages,
                angNo: _sourcePageNo);
            height += additionalHeight;
            height += getTextHeight(
                span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
            calculatedItems.textSpans.add(span);
          } else {
            TextSpan span = getAsscciiSpan(element.gurmukhi,
                style: baaniTextStyle(
                  fontWeight: _isbold ? FontWeight.bold : FontWeight.normal,
                  color: nightmodevalue ? blackColor : whiteColor,
                ));
            height += getTextHeight(
                span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
            calculatedItems.textSpans.add(span);
            height += additionalHeight;
            printSizes(height);
          }
        }
        if (element.hindiTransliteration != null &&
            (element.hindiTransliteration ?? "").isNotEmpty) {
          TextSpan span2 = getAsscciiSpan(element.hindiTransliteration,
              style: baaniTextStyle(
                color: nightmodevalue ? kButtonColor : klightbrown,
                fontWeight: _isbold ? FontWeight.bold : FontWeight.normal,
              ));
          if (height +
                  _getAppbarAndStatusBarHeight() +
                  getTextHeight(
                      span: span2, maxWidth: Contexts.size!.width - 2 * 8.w) >
              Contexts.size!.height) {
            calculatedItems.pageNo += 1;
            calculatedItems.totalPages += 1;
            calculatedItems.height = height;
            calculatedPages.add(calculatedItems);
            semiUpdate();
            height = getTextHeight(
                span: angSpan, maxWidth: Contexts.size!.width - 2 * 8.w);
            calculatedItems = CalculatedItems(
                textSpans: List.empty(growable: true),
                pageNo: calculatedItems.pageNo,
                totalPages: calculatedItems.totalPages,
                angNo: _sourcePageNo);
            height += additionalHeight;
            height += getTextHeight(
                span: span2, maxWidth: Contexts.size!.width - 2 * 8.w);
            calculatedItems.textSpans.add(span2);
          } else {
            height += getTextHeight(
                span: span2, maxWidth: Contexts.size!.width - 2 * 8.w);
            calculatedItems.textSpans.add(span2);
            height += additionalHeight;
            printSizes(height);
          }
        }
      } else if (_language == Languages.Gurmukhi) {
        if (element.gurmukhi.isNotEmpty) {
          TextSpan span = getAsscciiSpan(element.gurmukhi,
              style: baaniTextStyle(
                  color: nightmodevalue ? blackColor : whiteColor,
                  fontWeight: _isbold ? FontWeight.bold : FontWeight.normal));
          if (height +
                  _getAppbarAndStatusBarHeight() +
                  getTextHeight(
                      span: span, maxWidth: Contexts.size!.width - 2 * 8.w) >
              Contexts.size!.height) {
            calculatedItems.pageNo += 1;
            calculatedItems.totalPages += 1;
            calculatedItems.height = height;
            calculatedPages.add(calculatedItems);
            semiUpdate();
            height = getTextHeight(
                span: angSpan, maxWidth: Contexts.size!.width - 2 * 8.w);
            calculatedItems = CalculatedItems(
                textSpans: List.empty(growable: true),
                pageNo: calculatedItems.pageNo,
                totalPages: calculatedItems.totalPages,
                angNo: _sourcePageNo);
            height += additionalHeight;
            height += getTextHeight(
                span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
            calculatedItems.textSpans.add(span);
          } else {
            height += getTextHeight(
                span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
            calculatedItems.textSpans.add(span);
            height += additionalHeight;
            printSizes(height);
          }
        }
      } else {
        if (element.hindiTransliteration != null &&
            (element.hindiTransliteration ?? "").isNotEmpty) {
          TextSpan span = getAsscciiSpan(element.hindiTransliteration,
              style: baaniTextStyle(
                color: nightmodevalue ? kButtonColor : whiteColor,
                fontWeight: _isbold ? FontWeight.bold : FontWeight.normal,
              ));
          if (height +
                  _getAppbarAndStatusBarHeight() +
                  getTextHeight(
                      span: span, maxWidth: Contexts.size!.width - 2 * 8.w) >
              Contexts.size!.height) {
            calculatedItems.pageNo += 1;
            calculatedItems.totalPages += 1;
            calculatedItems.height = height;
            calculatedPages.add(calculatedItems);
            semiUpdate();
            height = getTextHeight(
                span: angSpan, maxWidth: Contexts.size!.width - 2 * 8.w);
            calculatedItems = CalculatedItems(
                textSpans: List.empty(growable: true),
                pageNo: calculatedItems.pageNo,
                totalPages: calculatedItems.totalPages,
                angNo: _sourcePageNo);
            height += additionalHeight;
            height += getTextHeight(
                span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
            calculatedItems.textSpans.add(span);
          } else {
            height += getTextHeight(
                span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
            calculatedItems.textSpans.add(span);
            height += additionalHeight;
            printSizes(height);
          }
        }
      }
      //english transliteration
      if (_showEnglishTransliteration &&
          element.englishTransliteration != null &&
          (element.englishTransliteration ?? "").isNotEmpty) {
        // print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        // print(element.englishTransliteration);
        // print(element.englishTransliteration?.replaceAll(RegExp(",|!|'|;"), ""));
        TextSpan span = getSpan(
            element.englishTransliteration?.replaceAll(RegExp(",|!|'|;"), ""),
            style: baaniTextStyle(
              color: nightmodevalue ? darkGreeenColor : kgoldyellowBackground,
              fontWeight: _isbold ? FontWeight.bold : FontWeight.normal,
            ));
        if (height +
                _getAppbarAndStatusBarHeight() +
                getTextHeight(
                    span: span, maxWidth: Contexts.size!.width - 2 * 8.w) >
            Contexts.size!.height) {
          calculatedItems.pageNo += 1;
          calculatedItems.totalPages += 1;
          calculatedItems.height = height;
          calculatedPages.add(calculatedItems);
          semiUpdate();
          height = getTextHeight(
              span: angSpan, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems = CalculatedItems(
              textSpans: List.empty(growable: true),
              pageNo: calculatedItems.pageNo,
              totalPages: calculatedItems.totalPages,
              angNo: _sourcePageNo);
          height += additionalHeight;
          height += getTextHeight(
              span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems.textSpans.add(span);
        } else {
          height += getTextHeight(
              span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems.textSpans.add(span);
          height += additionalHeight;
          printSizes(height);
        }
      }

      if (_showEnglishTranslation &&
          element.translationEnglish != null &&
          (element.translationEnglish ?? "").isNotEmpty) {
        TextSpan span = getSpan(element.translationEnglish,
            style: baaniTextStyle(
              color: nightmodevalue ? purpleColor : whiteColor,
              fontWeight: _isbold ? FontWeight.bold : FontWeight.normal,
            ));
        if (height +
                _getAppbarAndStatusBarHeight() +
                getTextHeight(
                    span: span, maxWidth: Contexts.size!.width - 2 * 8.w) >
            Contexts.size!.height) {
          calculatedItems.pageNo += 1;
          calculatedItems.totalPages += 1;
          calculatedItems.height = height;
          calculatedPages.add(calculatedItems);
          semiUpdate();
          height = getTextHeight(
              span: angSpan, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems = CalculatedItems(
              textSpans: List.empty(growable: true),
              pageNo: calculatedItems.pageNo,
              totalPages: calculatedItems.totalPages,
              angNo: _sourcePageNo);
          height += additionalHeight;
          height += getTextHeight(
              span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems.textSpans.add(span);
        } else {
          height += getTextHeight(
              span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems.textSpans.add(span);
          height += additionalHeight;
          printSizes(height);
        }
      }

      if (_showPunjabiTranslation &&
          element.translationPunjabi != null &&
          (element.translationPunjabi ?? "").isNotEmpty) {
        TextSpan span = getAsscciiSpan(element.translationPunjabi,
            style: baaniTextStyle(
                color: nightmodevalue ? darkRedColor : klightgreen,
                fontWeight: _isbold ? FontWeight.bold : FontWeight.normal));
        if (height +
                _getAppbarAndStatusBarHeight() +
                getTextHeight(
                    span: span, maxWidth: Contexts.size!.width - 2 * 8.w) >
            Contexts.size!.height) {
          calculatedItems.pageNo += 1;
          calculatedItems.totalPages += 1;
          calculatedItems.height = height;
          calculatedPages.add(calculatedItems);
          semiUpdate();
          height = getTextHeight(
              span: angSpan, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems = CalculatedItems(
            textSpans: List.empty(growable: true),
            pageNo: calculatedItems.pageNo,
            totalPages: calculatedItems.totalPages,
            angNo: _sourcePageNo,
          );
          height += additionalHeight;
          height += getTextHeight(
              span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems.textSpans.add(span);
        } else {
          height += getTextHeight(
              span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems.textSpans.add(span);
          height += additionalHeight;
          printSizes(height);
        }
      }

      if (_showPunjabiTeekaTranslation &&
          element.translationPunjabiTeeka != null &&
          (element.translationPunjabiTeeka ?? "").isNotEmpty) {
        TextSpan span = getAsscciiSpan(element.translationPunjabiTeeka,
            style: baaniTextStyle(
              color: nightmodevalue ? kFbColor : kPinkColor,
              fontWeight: _isbold ? FontWeight.bold : FontWeight.normal,
            ));
        if (height +
                _getAppbarAndStatusBarHeight() +
                getTextHeight(
                    span: span, maxWidth: Contexts.size!.width - 2 * 8.w) >
            Contexts.size!.height) {
          calculatedItems.pageNo += 1;
          calculatedItems.totalPages += 1;
          calculatedItems.height = height;
          calculatedPages.add(calculatedItems);
          semiUpdate();
          height = getTextHeight(
              span: angSpan, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems = CalculatedItems(
              textSpans: List.empty(growable: true),
              pageNo: calculatedItems.pageNo,
              totalPages: calculatedItems.totalPages,
              angNo: _sourcePageNo);
          height += additionalHeight;
          height += getTextHeight(
              span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems.textSpans.add(span);
        } else {
          height += getTextHeight(
              span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems.textSpans.add(span);
          height += additionalHeight;
          printSizes(height);
        }
      }

      if (_showFaridkotTeekaTranslation &&
          element.translationFaridkotTeeka != null &&
          (element.translationFaridkotTeeka ?? "").isNotEmpty) {
        TextSpan span = getAsscciiSpan(element.translationFaridkotTeeka,
            style: baaniTextStyle(
              color: nightmodevalue ? MongiaColor : klightbluee,
              fontWeight: _isbold ? FontWeight.bold : FontWeight.normal,
            ));
        if (height +
                _getAppbarAndStatusBarHeight() +
                getTextHeight(
                    span: span, maxWidth: Contexts.size!.width - 2 * 8.w) >
            Contexts.size!.height) {
          calculatedItems.pageNo += 1;
          calculatedItems.totalPages += 1;
          calculatedItems.height = height;
          calculatedPages.add(calculatedItems);
          semiUpdate();
          height = getTextHeight(
              span: angSpan, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems = CalculatedItems(
              textSpans: List.empty(growable: true),
              pageNo: calculatedItems.pageNo,
              totalPages: calculatedItems.totalPages,
              angNo: _sourcePageNo);
          height += additionalHeight;
          height += getTextHeight(
              span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems.textSpans.add(span);
        } else {
          height += getTextHeight(
              span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems.textSpans.add(span);
          height += additionalHeight;
          printSizes(height);
        }
      }

      if (_showHindiTranslation &&
          element.translationHindi != null &&
          (element.translationHindi ?? "").isNotEmpty) {
        TextSpan span =
            getAsscciiSpan(element.translationHindi, style: baaniTextStyle());
        if (height +
                _getAppbarAndStatusBarHeight() +
                getTextHeight(
                    span: span, maxWidth: Contexts.size!.width - 2 * 8.w) >
            Contexts.size!.height) {
          calculatedItems.pageNo += 1;
          calculatedItems.totalPages += 1;
          calculatedItems.height = height;
          calculatedPages.add(calculatedItems);
          semiUpdate();
          height = getTextHeight(
              span: angSpan, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems = CalculatedItems(
              textSpans: List.empty(growable: true),
              pageNo: calculatedItems.pageNo,
              totalPages: calculatedItems.totalPages,
              angNo: _sourcePageNo);
          height += additionalHeight;
          height += getTextHeight(
              span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems.textSpans.add(span);
        } else {
          height += getTextHeight(
              span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems.textSpans.add(span);
          height += additionalHeight;
          printSizes(height);
        }
      }

      if (_showHindiTeekaTranslation &&
          element.translationHindiTeeka != null &&
          (element.translationHindiTeeka ?? "").isNotEmpty) {
        TextSpan span = getAsscciiSpan(element.translationHindiTeeka,
            style: baaniTextStyle());
        if (height +
                _getAppbarAndStatusBarHeight() +
                getTextHeight(
                    span: span, maxWidth: Contexts.size!.width - 2 * 8.w) >
            Contexts.size!.height) {
          calculatedItems.pageNo += 1;
          calculatedItems.totalPages += 1;
          calculatedItems.height = height;
          calculatedPages.add(calculatedItems);
          semiUpdate();
          height = getTextHeight(
              span: angSpan, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems = CalculatedItems(
              textSpans: List.empty(growable: true),
              pageNo: calculatedItems.pageNo,
              totalPages: calculatedItems.totalPages,
              angNo: _sourcePageNo);
          height += additionalHeight;
          height += getTextHeight(
              span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems.textSpans.add(span);
        } else {
          height += getTextHeight(
              span: span, maxWidth: Contexts.size!.width - 2 * 8.w);
          calculatedItems.textSpans.add(span);
          height += additionalHeight;
          printSizes(height);
        }
      }
    });

    if (calculatedItems.textSpans.isNotEmpty) {
      calculatedItems.height = height;
      calculatedPages.add(calculatedItems);
    }
    //   change total pages of all calculated items to the length of calculated pages
    int p = 0;
    calculatedPages.forEach((element) {
      element.pageNo = p + 1;
      element.totalPages = calculatedPages.length;
      p++;
    });
  }

  printSizes(height) {
    // print("========== Sizes ===========");
    // print(size.height);
    // print(height + _getAppbarAndStatusBarHeight());
  }

  Future<void> getSearchedAng() async {
    setState(() {
      _isLoading = true;
    });

    searchedAng = await Reader.search(
        searchType: 1,
        searchText: widget.searchedAng.toString(),
        shabadId: widget.searchedBaaniLine?.shabadId,
        // angNo :
        // angno : widget.searchedAng,

        bookNo: widget.searchedBaaniLine == null
            ? widget.bookNo
            : widget.searchedBaaniLine!.orderId > 60555
                ? widget.searchedBaaniLine!.orderId > 128313
                    ? widget.searchedBaaniLine!.orderId > 135898
                        ? widget.searchedBaaniLine!.orderId > 138659
                            ? widget.searchedBaaniLine!.orderId > 139465
                                ? widget.searchedBaaniLine!.orderId > 140486
                                    ? widget.searchedBaaniLine!.orderId > 140817
                                        ? 8
                                        : 7
                                    : 6
                                : 5
                            : 4
                        : 3
                    : 2
                : 1);
    setState(() {
      _isLoading = false;
    });
  }

  Future<void> goToSearchPage() async {
    setState(() {
      _isLoading = true;
    });
    searchedAng = await Reader.search(
        searchType: 1,
        searchText: widget.searchedAng.toString(),
        shabadId: widget.searchedBaaniLine?.shabadId,
        // angno : widget.searchedAng,
        bookNo: widget.searchedBaaniLine == null
            ? widget.bookNo
            : widget.searchedBaaniLine!.orderId > 60555
                ? 2
                : 1);
    setState(() {
      _isLoading = false;
    });
  }

  isBookmarked() async {
    final prefs = SharedPreferences.getInstance();
    final List<BaaniLineModel>? storedItems = await prefs.then((value) => value
        .getStringList('myBookmarksList')
        ?.map((e) => BaaniLineModel.fromJson(jsonDecode(e)))
        .toList());
    if (storedItems != null) {
      if (storedItems.contains(widget.searchedBaaniLine)) {
        _isBookMarked = true;
      } else {
        _isBookMarked = false;
      }
      return;
    }
    _isBookMarked = false;
  }

  saveToBookmarks(BaaniLineModel baaniLine) async {
    final List<BaaniLineModel>? storedItems = prefs
        .getStringList('myBookmarksList')
        ?.map((e) => BaaniLineModel.fromJson(jsonDecode(e)))
        .toList();
    if (storedItems != null) {
      if (storedItems.contains(baaniLine)) {
        storedItems.remove(baaniLine);
        prefs.setStringList('myBookmarksList',
            storedItems.map((e) => jsonEncode(e.toJson())).toList());
        setState(() {
          _isBookMarked = false;
        });
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: const Text('Removed from bookmark'),
            duration: const Duration(seconds: 2),
            action: SnackBarAction(
              label: 'Close',
              onPressed: () {
                // Code to execute.
              },
            ),
          ),
        );
        return;
      }
      storedItems.add(baaniLine);
      prefs.setStringList('myBookmarksList',
          storedItems.map((e) => jsonEncode(e.toJson())).toList());
      // await isBookmarked();
      setState(() {
        _isBookMarked = true;
      });
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: const Text('Bookmarked'),
          duration: const Duration(seconds: 2),
          action: SnackBarAction(
            label: 'Close',
            onPressed: () {
              // Code to execute.
            },
          ),
        ),
      );
    } else {
      prefs.setStringList('myBookmarksList', [jsonEncode(baaniLine.toJson())]);
      setState(() {
        _isBookMarked = true;
      });
    }
  }

  Future<void> updateNitnemLists() async {
    // print(_linesPerPage);
    print(currentAng.count);
    // print("================================");
    // DateTime t = DateTime.now();
    if (currentAng.count == 0) {
      setState(() {
        _isLoading = true;
      });
      currentAng = await Reader.getNitnemAngs(nitnemId: _nitnemId);
      // currentAng = NitnemService.getNitnemById(id: _nitnemId);
      // placeWidgetsToPage();
    }
    if (currentAng.baaniLines.isEmpty) {
      currentAng = DBResult(baaniLines: LinkedList<BaaniLineModel>(), count: 0);
      placeWidgetsToPage();
      setState(() {
        _isLoading = false;
      });
      _loadedLines = 0;
      return;
    }
    // print( DateTime.now().millisecondsSinceEpoch - t.millisecondsSinceEpoch);
    if (_loadedLines >= currentAng.count) {
      return;
    }
    placeWidgetsToPage();
    // nextAng = await Reader.getAngs(sourcePageNo: _sourcePageNo,pageNo: _pageNo + 1, lines: _linesPerPage, bookNo: _bookNo);
    // previousAng = _sourcePageNo ==1 && _pageNo ==1 ? DBResult(baaniLines: List.empty(), count: 0) : _sourcePageNo >=1 && _pageNo ==1 ? _previousChapterTotalPages.isEmpty ? DBResult(baaniLines: List.empty(), count: 0) : await Reader.getAngs(sourcePageNo: _sourcePageNo - 1,pageNo: _previousChapterTotalPages.last, lines: _linesPerPage) : await Reader.getAngs(sourcePageNo: _sourcePageNo,pageNo: _pageNo - 1, lines: _linesPerPage);
    setState(() {
      _isLoading = false;
    });
    _loadedLines += _linesPerProcess;
    // if(_pageNo >1 && currentAng.baaniLines.length-1 < _linesPerPage*_pageNo){
    //   // print("reducing page no");
    //   // _pageNo -= 1;
    //
    //   // _sourcePageNo += 1;
    //   // return;
    // }
  }

  int _pageNo = 1;
  int _sourcePageNo = 1;
  int _linesPerPage = 10;
  int _bookNo = 1;
  bool _isNitnem = false;
  int _nitnemId = 1;
  // bool _searchviewm = false;
  bool _isLoading = false;
  bool? isLeftToRight;
  String? _title = '';
  List<int> _previousChapterTotalPages = <int>[];

  Offset _oldPosition = Offset.zero;
  @override
  void initState() {
    _sourcePageNo = widget.pageNo;
    // _bookNo = widget.bookNo;

    _bookNo = widget.searchedBaaniLine == null
        ? widget.bookNo
        : widget.searchedBaaniLine!.orderId > 60555
            ? widget.searchedBaaniLine!.orderId > 128313
                ? widget.searchedBaaniLine!.orderId > 135898
                    ? widget.searchedBaaniLine!.orderId > 138659
                        ? widget.searchedBaaniLine!.orderId > 139465
                            ? widget.searchedBaaniLine!.orderId > 140486
                                ? widget.searchedBaaniLine!.orderId > 140817
                                    ? 8
                                    : 7
                                : 6
                            : 5
                        : 4
                    : 3
                : 2
            : 1;

    // _isbold = widget.
    // _showEnglishTransliteration = widget.isEnglishTransliteration;
    _isNitnem = widget.isNitnem;

    _nitnemId = widget.nitnemId;
    _extraBottomPadding = 10.h;
    _title = widget.title;
    _searchController.clear();

    _searchviewm = widget.searchView;

    super.initState();
    // postFrameCallback();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      // await Future.delayed(Duration(milliseconds: 300));
      WakelockPlus.enable();
      prefs = await SharedPreferences.getInstance();
      _isbold = prefs.getBool("boldValue") ?? false;
      isbold = prefs.getBool("boldValue") ?? false;
      nightmodevalue = prefs.getBool("nightmodevalue") ?? false;
      print("$_isbold");
      _showEnglishTransliteration =
          prefs.getBool('showEnglishTransliteration') ?? false;
      _showEnglishTranslation =
          prefs.getBool('showEnglishTranslation') ?? false;
      _showPunjabiTranslation =
          prefs.getBool('showPunjabiTranslation') ?? false;
      _showPunjabiTeekaTranslation =
          prefs.getBool('showPunjabiTeekaTranslation') ?? false;
      _showHindiTranslation = prefs.getBool('showHindiTranslation') ?? false;
      _showHindiTeekaTranslation =
          prefs.getBool('showHindiTeekaTranslation') ?? false;
      _showFaridkotTeekaTranslation =
          prefs.getBool('showFaridkotTeekaTranslation') ?? false;
      _language = Languages.values[prefs.getInt('language') ?? 0];
      print('$_language');

      _initialized = true;
      resetLinesPerPage();
      isBookmarked();
      if (widget.searchView) {
        //
        // searchedAng = _pageNo as DBResult;
        //  widget.searchedBaaniLine!.sourcePage!;
        //
      }
    });
    // updateGurbaniLists(); //kro
    // setState(() {

    // });
  }

  @override
  void dispose() {
    WakelockPlus.disable();
    super.dispose();
  }

  semiUpdate() {
    if (calculatedPages.length == 1) {
      setState(() {});
    }
  }

  resetLinesPerPage() {
    if (widget.searchView) {
      getSearchedAng();
      return;
    }
    int linesToShow = 0;
    if (_language == Languages.Both) {
      linesToShow = 2;
    } else {
      linesToShow = 1;
    }
    if (_bookNo == 1) {
      if (_showEnglishTransliteration) {
        linesToShow += 1;
      }
      if (_showEnglishTranslation) {
        linesToShow += 1;
      }
      if (_showPunjabiTranslation) {
        linesToShow += 1;
      }
      if (_showPunjabiTeekaTranslation) {
        linesToShow += 1;
      }
      if (_showHindiTranslation) {
        linesToShow += 1;
      }
      if (_showHindiTeekaTranslation) {
        linesToShow += 1;
      }
      if (_showFaridkotTeekaTranslation) {
        linesToShow += 1;
      }
    } else {
      if (_showEnglishTransliteration) {
        linesToShow += 1;
      }
      if (_showEnglishTranslation) {
        linesToShow += 1;
      }
    }
    _linesPerPage = (10 - linesToShow);
    // _linesPerPage = _language == Languages.Both && _showEnglishTransliteration ? 6 : _language == Languages.Both && !_showEnglishTransliteration ? 7 : _showEnglishTransliteration ? 7 : 10;
    if (_isNitnem) {
      updateNitnemLists();
    } else {
      updateGurbaniLists();
    }
  }

  // double _lineHeight({required Text textWidget}) {
  //   final textPainter = TextPainter(
  //     text: textWidget.text!.textSpan,
  //     textDirection: TextDirection.ltr,
  //     maxLines: 999, // A large number to ensure we get an accurate estimate
  //   );
  //
  //   textPainter.layout(maxWidth: widget.maxWidth);
  //   textPainter.computeLineMetrics().length;
  //   setState(() {
  //   });
  // }

  goToSourcePage(int sourcePageNo) {
    _sourcePageNo = sourcePageNo;
    _pageNo = 1;
    _previousChapterTotalPages.clear();
    updateGurbaniLists();
  }

  scrollToPosition() {
    var p = _itemPositionListener.itemPositions.value;
    // print("======================================================");
    // print(p.first.index);
    // print(p.last.index);
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      await Future.delayed(const Duration(milliseconds: 100));
      _itemScrollController.scrollTo(
          index: p.last.index, duration: const Duration(milliseconds: 100));
    });
    // _itemScrollController.scrollTo(index: p.first.index, duration: const Duration(milliseconds: 100));
  }

  @override
  Widget build(BuildContext context) {
    final Size mqSize = MediaQuery.of(context).size;
    print(widget.bookNo);
    print("=================================");
    // print(searchedAng.baaniLines.first.sp);
    print(widget.searchedBaaniLine?.sourcePage);
    return SafeArea(
      child: Scaffold(
        backgroundColor: nightmodevalue ? whiteColor : blackColor,
        drawer: Drawer(
          width: mqSize.width * 0.8,
          child: ListView(
            children: _isNitnem
                ? [
                    const SizedBox(height: 15),
                    const Row(
                      children: [
                        // Logo
                        Padding(
                          padding: EdgeInsets.only(left: 18.0),
                          child: Icon(
                            Icons.info,
                            color: Colors.red, // specify color if needed
                            size: 26, // specify size if needed
                          ),
                        ),
                        // Data Sources text

                        Expanded(
                          child: Padding(
                            padding:
                                EdgeInsets.only(left: 10.0, top: 5, bottom: 5),
                            child: Text(
                              'Data Sources: Sundar Gutka',
                              // overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                  color: blackColor,
                                  fontSize: 18,
                                  fontWeight: FontWeight.bold),
                            ),
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 10),
                    Row(
                      children: [
                        const Padding(
                          padding: EdgeInsets.only(left: 18.0),
                          child: Icon(
                            Icons.info,
                            color: Colors.red, // specify color if needed
                            size: 26, // specify size if needed
                          ),
                        ),
                        const SizedBox(width: 10),
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.only(
                                left: 3.0, top: 5, bottom: 5, right: 7),
                            child: Text(
                              'Baani: ${_title}',
                              style: const TextStyle(
                                  color: blackColor,
                                  fontSize: 18,
                                  fontWeight: FontWeight.bold),
                            ),
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 15),
                    const Divider(
                      height: 1,
                      color: blackColor,
                    ),
                    const SizedBox(height: 20),

                    const Padding(
                      padding: EdgeInsets.only(left: 18.0, top: 10),
                      child: Text(
                        'Translations : -',
                        style: TextStyle(
                          color: blackColor,
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                    // const SizedBox(height: 15),

                    const SizedBox(height: 10),
                    ListTile(
                      title: const Text('English Transliteration',
                          style: TextStyle(
                              fontWeight: FontWeight.bold, fontSize: 18)),
                      trailing: Switch(
                        value: _showEnglishTransliteration,
                        onChanged: (value) {
                          _showEnglishTransliteration = value;
                          prefs.setBool('showEnglishTransliteration', value);
                          _pageNo = 1;
                          _loadedLines = 0;
                          calculatedPages.clear();
                          resetLinesPerPage();
                        },
                      ),
                    ),
                    if (!Contexts.nitnemEngTranslationExceptions
                        .contains(_nitnemId))
                      ListTile(
                        title: const Text('English Translation',
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 18)),
                        trailing: Switch(
                          value: _showEnglishTranslation,
                          onChanged: (value) {
                            _showEnglishTranslation = value;
                            prefs.setBool('showEnglishTranslation', value);
                            _pageNo = 1;
                            _loadedLines = 0;
                            calculatedPages.clear();
                            resetLinesPerPage();
                          },
                        ),
                      ),
                    if (!Contexts.nitnemPunTranslationExceptions
                        .contains(_nitnemId))
                      ListTile(
                        title: const Text('Punjabi Translation',
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 18)),
                        trailing: Switch(
                          value: _showPunjabiTranslation,
                          onChanged: (value) {
                            _showPunjabiTranslation = value;
                            prefs.setBool('showPunjabiTranslation', value);
                            _pageNo = 1;
                            _loadedLines = 0;
                            calculatedPages.clear();
                            resetLinesPerPage();
                          },
                        ),
                      ),
                    if (!Contexts.nitnemPTTranslationExceptions
                        .contains(_nitnemId))
                      ListTile(
                        title: const Text('Punjabi Teeka',
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 18)),
                        trailing: Switch(
                          value: _showPunjabiTeekaTranslation,
                          onChanged: (value) {
                            _showPunjabiTeekaTranslation = value;
                            prefs.setBool('showPunjabiTeekaTranslation', value);
                            _pageNo = 1;
                            _loadedLines = 0;
                            calculatedPages.clear();
                            resetLinesPerPage();
                          },
                        ),
                      ),
                    if (!Contexts.nitnemFTTranslationExceptions
                        .contains(_nitnemId))
                      ListTile(
                        title: const Text('Faridkot Teeka',
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 18)),
                        trailing: Switch(
                          value: _showFaridkotTeekaTranslation,
                          onChanged: (value) {
                            _showFaridkotTeekaTranslation = value;
                            prefs.setBool(
                                'showFaridkotTeekaTranslation', value);
                            _pageNo = 1;
                            _loadedLines = 0;
                            calculatedPages.clear();
                            resetLinesPerPage();
                          },
                        ),
                      ),
                    const Divider(
                      height: 1,
                      color: blackColor,
                    ),
                    const SizedBox(height: 15),
                    const Padding(
                      padding:
                          EdgeInsets.only(left: 18.0, top: 10.0, bottom: 3.0),
                      child: Text(
                        'Select Baani Language :',
                        style: TextStyle(
                          color: blackColor,
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                    const SizedBox(height: 10),
                    ListTile(
                      title: const Text(
                        'Language ',
                        style: TextStyle(
                          color: blackColor,
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      trailing: DropdownButton<Languages>(
                        value: _language,
                        onChanged: (Languages? newValue) {
                          _language = newValue!;
                          prefs.setInt('language', newValue.index);
                          _pageNo = 1;
                          _loadedLines = 0;
                          calculatedPages.clear();
                          resetLinesPerPage();
                        },
                        items: Languages.values
                            .map<DropdownMenuItem<Languages>>(
                                (Languages value) {
                          return DropdownMenuItem<Languages>(
                            value: value,
                            child: Text(
                              value.toString().split('.').last,
                              style: const TextStyle(
                                  fontWeight: FontWeight.bold, fontSize: 18),
                            ),
                          );
                        }).toList(),
                      ),
                    ),
                  ]
                : _bookNo == 1
                    ? [
                        const SizedBox(height: 20),
                        const Row(
                          children: [
                            // Logo
                            Padding(
                              padding: EdgeInsets.only(left: 10.0),
                              child: Icon(
                                Icons.info,
                                color: Colors.red, // specify color if needed
                                size: 24, // specify size if needed
                              ),
                            ),
                            // Data Sources text

                            Expanded(
                              child: Padding(
                                padding: EdgeInsets.only(
                                    left: 10.0, top: 5, bottom: 5, right: 15),
                                child: Text(
                                  'Source: Guru Granth Sahib Ji',
                                  style: TextStyle(
                                    color: blackColor,
                                    fontSize: 18,
                                    fontWeight: FontWeight.bold,
                                  ),
                                  overflow: TextOverflow.ellipsis,
                                  maxLines: 2,
                                  softWrap: true,
                                ),
                              ),
                            ),
                          ],
                        ),
                        Row(
                          children: [
                            // Logo
                            const Padding(
                              padding: EdgeInsets.only(left: 10.0),
                              child: Icon(
                                Icons.info,
                                color: Colors.red, // specify color if needed
                                size: 24, // specify size if needed
                              ),
                            ),
                            // Data Sources text

                            _searchviewm
                                ? Padding(
                                    padding: const EdgeInsets.only(
                                        left: 10.0, top: 5, bottom: 5),
                                    child: Text(
                                      'Ang - : ${widget.searchedAng}',
                                      style: const TextStyle(
                                          color: blackColor,
                                          fontSize: 18,
                                          fontWeight: FontWeight.bold),
                                    ))
                                : Padding(
                                    padding: const EdgeInsets.only(
                                        left: 10.0, top: 5, bottom: 5),
                                    child: Text(
                                      'Ang - : $_sourcePageNo',
                                      style: const TextStyle(
                                          color: blackColor,
                                          fontSize: 18,
                                          fontWeight: FontWeight.bold),
                                    )),
                          ],
                        ),
                        const SizedBox(height: 10),
                        const Divider(
                          height: 1,
                          color: blackColor,
                        ),
                        const SizedBox(height: 10),
                        const Padding(
                          padding: EdgeInsets.only(left: 18.0, top: 10),
                          child: Text(
                            'Translations :',
                            overflow: TextOverflow.ellipsis,
                            maxLines: 2,
                            softWrap: true,
                            style: TextStyle(
                              color: blackColor,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                        const SizedBox(height: 15),

                        ListTile(
                          title: const Text(
                            'English Transliteration',
                            style: TextStyle(
                              color: blackColor,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                            overflow: TextOverflow.ellipsis,
                            maxLines: 2,
                            softWrap: true,
                          ),
                          trailing: Switch(
                            value: _showEnglishTransliteration,
                            onChanged: (value) {
                              _showEnglishTransliteration = value;
                              prefs.setBool(
                                  'showEnglishTransliteration', value);
                              _pageNo = 1;
                              _loadedLines = 0;
                              calculatedPages.clear();
                              resetLinesPerPage();
                              if (widget.searchView) {
                                scrollToPosition();
                              }
                            },
                          ),
                        ),
                        ListTile(
                          title: const Text(
                            'English Translation',
                            overflow: TextOverflow.ellipsis,
                            maxLines: 2,
                            softWrap: true,
                            style: TextStyle(
                              color: blackColor,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          trailing: Switch(
                            value: _showEnglishTranslation,
                            onChanged: (value) {
                              _showEnglishTranslation = value;
                              prefs.setBool('showEnglishTranslation', value);
                              _pageNo = 1;
                              _loadedLines = 0;
                              calculatedPages.clear();
                              resetLinesPerPage();
                              if (widget.searchView) {
                                scrollToPosition();
                              }
                            },
                          ),
                        ),
                        ListTile(
                          title: const Text(
                            'Punjabi Translation',
                            overflow: TextOverflow.ellipsis,
                            maxLines: 2,
                            softWrap: true,
                            style: TextStyle(
                              color: blackColor,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          trailing: Switch(
                            value: _showPunjabiTranslation,
                            onChanged: (value) {
                              _showPunjabiTranslation = value;
                              prefs.setBool('showPunjabiTranslation', value);
                              _pageNo = 1;
                              _loadedLines = 0;
                              calculatedPages.clear();
                              resetLinesPerPage();
                              if (widget.searchView) {
                                scrollToPosition();
                              }
                            },
                          ),
                        ),
                        ListTile(
                          title: const Text(
                            'Punjabi Teeka',
                            overflow: TextOverflow.ellipsis,
                            maxLines: 2,
                            softWrap: true,
                            style: TextStyle(
                              color: blackColor,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          trailing: Switch(
                            value: _showPunjabiTeekaTranslation,
                            onChanged: (value) {
                              _showPunjabiTeekaTranslation = value;
                              prefs.setBool(
                                  'showPunjabiTeekaTranslation', value);
                              _pageNo = 1;
                              _loadedLines = 0;
                              calculatedPages.clear();
                              resetLinesPerPage();
                              if (widget.searchView) {
                                scrollToPosition();
                              }
                            },
                          ),
                        ),
                        ListTile(
                          title: const Text(
                            'Faridkot Teeka',
                            overflow: TextOverflow.ellipsis,
                            maxLines: 2,
                            softWrap: true,
                            style: TextStyle(
                              color: blackColor,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          trailing: Switch(
                            value: _showFaridkotTeekaTranslation,
                            onChanged: (value) {
                              _showFaridkotTeekaTranslation = value;
                              prefs.setBool(
                                  'showFaridkotTeekaTranslation', value);
                              _pageNo = 1;
                              _loadedLines = 0;
                              calculatedPages.clear();
                              resetLinesPerPage();
                              if (widget.searchView) {
                                scrollToPosition();
                              }
                            },
                          ),
                        ),

                        // ListTile(
                        //   title: const Text('Hindi Translation'),
                        //   trailing: Switch(
                        //     value: _showHindiTranslation,
                        //     onChanged: (value) {
                        //         _showHindiTranslation = value;
                        //         resetLinesPerPage();
                        //     },
                        //   ),
                        // ),
                        // ListTile(
                        //   title: const Text('Hindi Teeka'),
                        //   trailing: Switch(
                        //     value: _showHindiTeekaTranslation,
                        //     onChanged: (value) {
                        //         _showHindiTeekaTranslation = value;
                        //         resetLinesPerPage();
                        //     },
                        //   ),
                        // ),
                        const SizedBox(height: 10),
                        const Divider(
                          height: 1,
                          color: blackColor,
                        ),
                        const SizedBox(height: 15),
                        const Padding(
                          padding: EdgeInsets.only(
                              left: 18.0, top: 10.0, bottom: 3.0),
                          child: Text(
                            'Select Baani Language :',
                            overflow: TextOverflow.ellipsis,
                            maxLines: 2,
                            softWrap: true,
                            style: TextStyle(
                              color: blackColor,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                        const SizedBox(height: 10),
                        ListTile(
                          title: const Text(
                            'Language',
                            overflow: TextOverflow.ellipsis,
                            maxLines: 2,
                            softWrap: true,
                            style: TextStyle(
                              color: blackColor,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          trailing: DropdownButton<Languages>(
                            value: _language,
                            onChanged: (Languages? newValue) {
                              _language = newValue!;
                              prefs.setInt('language', newValue.index);
                              _pageNo = 1;
                              _loadedLines = 0;
                              calculatedPages.clear();
                              resetLinesPerPage();
                            },
                            items: Languages.values
                                .map<DropdownMenuItem<Languages>>(
                                    (Languages value) {
                              return DropdownMenuItem<Languages>(
                                value: value,
                                child: Text(value.toString().split('.').last,
                                    style: const TextStyle(
                                        fontWeight: FontWeight.bold,
                                        fontSize: 18)),
                              );
                            }).toList(),
                          ),
                        ),
                      ]
                    : [
                        const SizedBox(height: 20),
                        Row(
                          children: [
                            // Logo
                            const Padding(
                              padding: EdgeInsets.only(left: 10.0),
                              child: Icon(
                                Icons.info,
                                color: Colors.red, // specify color if needed
                                size: 26, // specify size if needed
                              ),
                            ),
                            // Data Sources text

                            Expanded(
                              child: Padding(
                                padding: const EdgeInsets.only(
                                    left: 10.0, top: 5, bottom: 5, right: 10),
                                child: Text(
                                  'Source: ${_bookNo == 2 ? "Dasam Granth Sahib JI" : _bookNo == 3 ? "Vaaran Bhai Gurdas Ji" : _bookNo == 4 ? "Kabit Savaiye Bhai Gurdas Ji" : _bookNo == 5 ? "Ghazals Bhai Nand Lal Ji" : _bookNo == 6 ? "Zindagi-Nama Bhai Nand Lal Ji" : _bookNo == 7 ? "Ghazal-Nama Bhai Nand Lal Ji" : _bookNo == 8 ? "Jot Bigas Bhai Nand Lal Ji" : 'Shabad'}',
                                  overflow: TextOverflow.ellipsis,
                                  maxLines: 3,
                                  softWrap: true,
                                  style: TextStyle(
                                    color: blackColor,
                                    fontSize: 18,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                        Row(
                          children: [
                            // Logo
                            const Padding(
                              padding: EdgeInsets.only(
                                left: 10.0,
                              ),
                              child: Icon(
                                Icons.info,
                                color: Colors.red, // specify color if needed
                                size: 24, // specify size if needed
                              ),
                            ),
                            // Data Sources text

                            _searchviewm
                                ? Padding(
                                    padding: const EdgeInsets.only(
                                        left: 10.0, top: 5, bottom: 5),
                                    child: Text(
                                      'Ang - : ${widget.searchedAng}',
                                      style: const TextStyle(
                                          color: blackColor,
                                          fontSize: 18,
                                          fontWeight: FontWeight.bold),
                                    ))
                                : Padding(
                                    padding: const EdgeInsets.only(
                                        left: 10.0, top: 5, bottom: 5),
                                    child: Text(
                                      'Ang - : $_sourcePageNo',
                                      style: const TextStyle(
                                          color: blackColor,
                                          fontSize: 18,
                                          fontWeight: FontWeight.bold),
                                    )),

                            //
                          ],
                        ),
                        const SizedBox(height: 10),
                        const Divider(
                          height: 1,
                          color: blackColor,
                        ),
                        const SizedBox(height: 20),
                        const Padding(
                          padding: EdgeInsets.only(left: 18.0, top: 10),
                          child: Text(
                            'Translations : -',
                            style: TextStyle(
                              color: blackColor,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                            overflow: TextOverflow.ellipsis,
                            maxLines: 2,
                            softWrap: true,
                          ),
                        ),
                        const SizedBox(height: 10),
                        ListTile(
                          title: const Text(
                            'English Transliteration',
                            style: TextStyle(
                              color: blackColor,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                            overflow: TextOverflow.ellipsis,
                            maxLines: 2,
                            softWrap: true,
                          ),
                          trailing: Switch(
                            value: _showEnglishTransliteration,
                            onChanged: (value) {
                              _showEnglishTransliteration = value;
                              prefs.setBool(
                                  'showEnglishTransliteration', value);
                              _pageNo = 1;
                              _loadedLines = 0;
                              calculatedPages.clear();
                              resetLinesPerPage();
                              if (widget.searchView) {
                                scrollToPosition();
                              }
                            },
                          ),
                        ),
                        ListTile(
                          title: const Text(
                            'English Translation',
                            style: TextStyle(
                              color: blackColor,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                            overflow: TextOverflow.ellipsis,
                            maxLines: 2,
                            softWrap: true,
                          ),
                          trailing: Switch(
                            value: _showEnglishTranslation,
                            onChanged: (value) {
                              _showEnglishTranslation = value;
                              prefs.setBool('showEnglishTranslation', value);
                              _pageNo = 1;
                              _loadedLines = 0;
                              calculatedPages.clear();
                              resetLinesPerPage();
                              if (widget.searchView) {
                                scrollToPosition();
                              }
                            },
                          ),
                        ),
                        ListTile(
                          title: const Text(
                            'Punjabi Translation',
                            style: TextStyle(
                              color: blackColor,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                            overflow: TextOverflow.ellipsis,
                            maxLines: 2,
                            softWrap: true,
                          ),
                          trailing: Switch(
                            value: _showPunjabiTranslation,
                            onChanged: (value) {
                              _showPunjabiTranslation = value;
                              prefs.setBool('showPunjabiTranslation', value);
                              _pageNo = 1;
                              _loadedLines = 0;
                              calculatedPages.clear();
                              resetLinesPerPage();
                              if (widget.searchView) {
                                scrollToPosition();
                              }
                            },
                          ),
                        ),
                        const SizedBox(height: 10),
                        const Divider(
                          height: 1,
                          color: blackColor,
                        ),
                        const SizedBox(height: 10),
                        const Padding(
                          padding: EdgeInsets.only(
                              left: 18.0, top: 10.0, bottom: 3.0),
                          child: Text(
                            'Select Baani Language :',
                            style: TextStyle(
                              color: blackColor,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                            overflow: TextOverflow.ellipsis,
                            maxLines: 2,
                            softWrap: true,
                          ),
                        ),
                        const SizedBox(height: 10),
                        ListTile(
                          title: const Text(
                            'Language',
                            style: TextStyle(
                              color: blackColor,
                              fontSize: 19,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          trailing: DropdownButton<Languages>(
                            value: _language,
                            onChanged: (Languages? newValue) {
                              _language = newValue!;
                              prefs.setInt('language', newValue.index);
                              _pageNo = 1;
                              _loadedLines = 0;
                              calculatedPages.clear();
                              resetLinesPerPage();
                              // if(!value) {
                              //   scrollToPosition();
                              // }
                            },
                            items: Languages.values
                                .map<DropdownMenuItem<Languages>>(
                                    (Languages value) {
                              return DropdownMenuItem<Languages>(
                                value: value,
                                child: Text(value.toString().split('.').last,
                                    style: const TextStyle(
                                        fontWeight: FontWeight.bold,
                                        fontSize: 18)),
                              );
                            }).toList(),
                          ),
                        ),
                      ],
          ),
        ),
        appBar: AppBar(
          title: Text(
            _bookNo == 1
                ? "Guru Granth Sahib JI"
                : _bookNo == 2
                    ? "Dasam Granth Sahib JI"
                    : _bookNo == 3
                        ? "Vaaran Bhai Gurdas Ji"
                        : _bookNo == 4
                            ? "Kabit Savaiye Bhai Gurdas Ji"
                            : _bookNo == 5
                                ? "Ghazals Bhai Nand Lal Ji"
                                : _bookNo == 6
                                    ? "Zindagi-Nama Bhai Nand Lal Ji"
                                    : _bookNo == 7
                                        ? "Ghazal-Nama Bhai Nand Lal Ji"
                                        : _bookNo == 8
                                            ? "Jot Bigas Bhai Nand Lal Ji"
                                            : 'Shabad',
          ),
          actions: [
            if (!widget.searchView && !_isNitnem)
              IconButton(
                icon: const Icon(Icons.search),
                onPressed: () {
                  _showSearch = !_showSearch;
                  setState(() {});
                },
              ),
            widget.searchView && widget.searchedBaaniLine != null
                ? IconButton(
                    icon: Icon(
                      _isBookMarked ? Icons.favorite : Icons.favorite_outline,
                      color: whiteColor,
                    ),
                    onPressed: () async {
                      await saveToBookmarks(widget.searchedBaaniLine!);
                      setState(() {
                        // _isBookMarked = !_isBookMarked;
                      });
                    },
                  )
                : Container(),
          ],
        ),
        body: widget.searchView
            ? BaaniPageView(
                baaniLines: searchedAng,
                linesPerPage: searchedAng.count,
                language: _language,
                showEnglishTransliteration: _showEnglishTransliteration,
                showEnglishTranslation: _showEnglishTranslation,
                showPunjabiTranslation: _showPunjabiTranslation,
                showPunjabiTeekaTranslation: _showPunjabiTeekaTranslation,
                showFaridkotTeekaTranslation: _showFaridkotTeekaTranslation,
                showHindiTranslation: _showHindiTranslation,
                showHindiTeekaTranslation: _showHindiTeekaTranslation,
                // language: _language,
                isLoading: _isLoading,
                searchedLine: widget.searchedBaaniLine,
                showPageNo: false,
                itemPositionListener: _itemPositionListener,
                itemScrollController: _itemScrollController, isbold: isbold,
                nightmodevalue: nightmodevalue,
              )
            : Stack(
                children: [
                  Container(
                    color: nightmodevalue ? whiteColor : blackColor,
                    height: Contexts.size!.height -
                        _getAppbarAndStatusBarHeight() +
                        _extraBottomPadding,
                    width: Contexts.size!.width,
                    child: Center(
                      child: CalculatedViewWidget(
                        baaniLines: calculatedPages.isEmpty ||
                                calculatedPages.length <= 1 ||
                                calculatedPages.length <= _pageNo
                            ? CalculatedItems(
                                textSpans: List.empty(growable: true),
                                pageNo: 0,
                                totalPages: 0)
                            : calculatedPages.elementAt(_pageNo),
                        isLoading: _isLoading,
                        nightmod: nightmodevalue,
                        boldt: _isbold,
                      ),
                    ),
                  ),

                  SizedBox(
                    width: Contexts.size!.width,
                    height: Contexts.size!.height -
                        _getAppbarAndStatusBarHeight() +
                        _extraBottomPadding,
                    child: GestureDetector(
                      child: FlipWidget(
                        key: _flipKey,
                        textureSize: Size(
                                Contexts.size!.width,
                                Contexts.size!.height -
                                    _getAppbarAndStatusBarHeight() +
                                    _extraBottomPadding) *
                            1,
                        // leftToRight: true, //
                        child: Container(
                          color: nightmodevalue ? whiteColor : blackColor,
                          child: Center(
                            child: CalculatedViewWidget(
                              baaniLines: calculatedPages.isEmpty
                                  ? CalculatedItems(
                                      textSpans: List.empty(growable: true),
                                      pageNo: 0,
                                      totalPages: 0)
                                  : calculatedPages.elementAt(
                                      (_pageNo < 1 ? 1 : _pageNo) - 1),
                              isLoading: _isLoading,
                              boldt: _isbold,
                              nightmod: nightmodevalue,
                            ),
                          ),
                        ),
                      ),
                      onHorizontalDragStart: (details) {
                        _oldPosition = details.globalPosition;
                        _flipKey.currentState?.startFlip();
                      },
                      onHorizontalDragUpdate: (details) {
                        Offset off = details.globalPosition - _oldPosition;
                        double tilt = 1 / _clampMin((-off.dy + 20) / 100);
                        double percent =
                            math.max(0, -off.dx / Contexts.size!.width * 1.4);
                        percent = percent - percent / 2 * (1 - 1 / tilt);
                        _flipKey.currentState?.flip(percent, tilt);
                        if (details.primaryDelta! > 0) {
                          isLeftToRight = true;
                        } else if (details.primaryDelta! < 0) {
                          isLeftToRight = false;
                        } else {
                          isLeftToRight = null;
                        }
                        _isDragging = true;
                      },
                      onHorizontalDragEnd: (details) {
                        if (!_isDragging) {
                          return;
                        }
                        _isDragging = false;
                        _flipKey.currentState?.stopFlip();
                        if (isLeftToRight == true) {
                          if (_sourcePageNo == 1 && _pageNo == 1) {
                            return;
                          } else if (_sourcePageNo >= 1 && _pageNo == 1) {
                            _sourcePageNo -= 1;
                            if (_sourcePageNo < 1) {
                              _sourcePageNo = 1;
                            }
                            _pageNo = _previousChapterTotalPages.isEmpty
                                ? 0
                                : _previousChapterTotalPages.last;
                            if (_previousChapterTotalPages.isNotEmpty) {
                              _previousChapterTotalPages.removeLast();
                            }
                            // print(_history.length);
                            if (_history.length > 0) {
                              calculatedPages.clear();
                              calculatedPages
                                  .addAll(_history.last.calculatedItems);
                              currentAng = _history.last.lines;
                              _loadedLines = _history.last.loadedLines;
                              _pageNo = _history.last.pageNo;
                              _sourcePageNo = _history.last.sourcePageNo;
                              _history.last.unlink();
                            }
                          } else if (_sourcePageNo >= 1 && _pageNo >= 1) {
                            _pageNo -= 1;
                            // print(_pageNo);
                          }
                        } else if (isLeftToRight == false) {
                          _pageNo += 1;
                        }
                        if (isLeftToRight != null) {
                          if (_isNitnem) {
                            // if(currentAng.baaniLines.length - (_linesPerPage * (_pageNo)) <0){
                            //   _pageNo = (currentAng.baaniLines.length/_linesPerPage).ceil();
                            //   // return;
                            // }

                            if (_pageNo > calculatedPages.length) {
                              _pageNo = calculatedPages.length;
                              return;
                            }

                            updateNitnemLists();
                          } else {
                            if (_loadSourceLines) {
                              if (_loadedLines >= currentAng.count) {
                                if (calculatedPages.length - (_pageNo) < 0) {
                                  HistoryItem history = HistoryItem(
                                      calculatedItems: calculatedPages.toList(),
                                      lines: currentAng,
                                      sourcePageNo: _sourcePageNo,
                                      pageNo: _pageNo - 1,
                                      loadedLines: _loadedLines);
                                  _history.add(history);
                                  _pageNo = 1;
                                  _sourcePageNo += 1;
                                  calculatedPages.clear();
                                  _loadedLines = 0;
                                  // return;
                                } else {
                                  // return;
                                }
                              }

                              // if((currentAng.baaniLines.length - (_linesPerPage * (_pageNo)) <0) && (currentAng.baaniLines.length - (_linesPerPage * (_pageNo-1)) <=0)){
                              //   _pageNo = 1;
                              //   _sourcePageNo += 1;
                              //   // return;
                              // }
                              // updateGurbaniLists();
                              // return;
                            }
                            updateGurbaniLists(); //kro
                          }
                          setState(() {});
                        }
                        // Navigator.push(
                        //     context,
                        //     MaterialPageRoute(
                        //         builder: (context) => HomeScreen(
                        //               offset: widget.offset + 10,
                        //             )));
                      },
                      onHorizontalDragCancel: () {
                        _flipKey.currentState?.stopFlip();
                      },
                    ),
                  ),

                  // Positioned(
                  //     bottom: 10,
                  //     right: 10,
                  //     child: Column(
                  //       children: [
                  //         IconButton(
                  //             onPressed: () => Navigator.push(
                  //                 context,
                  //                 MaterialPageRoute(
                  //                     builder: (context) => HomeScreen(
                  //                           offset: widget.offset - 10,
                  //                         ))),
                  //             icon: const Icon(
                  //               Icons.arrow_back,
                  //               color: Colors.white,
                  //             )),
                  //         IconButton(
                  //             onPressed: () => Navigator.push(
                  //                 context,
                  //                 MaterialPageRoute(
                  //                     builder: (context) => HomeScreen(
                  //                           offset: widget.offset + 10,
                  //                         ))),
                  //             icon: const Icon(Icons.arrow_forward,
                  //                 color: Colors.white)),
                  //       ],
                  //     ))
                ],
              ),
        floatingActionButton: _showSearch
            ? Container(
                margin: EdgeInsets.only(top: 60.h, left: 10.w, right: 10.w),
                color: Colors.white.withOpacity(.9),
                child: TextField(
                  controller: _searchController,
                  textInputAction: TextInputAction.search,
                  keyboardType: TextInputType.number,
                  inputFormatters: [
                    FilteringTextInputFormatter.allow(RegExp(r'^[0-9]+$')),
                  ],
                  onSubmitted: (value) {
                    int inputPageNo = int.parse(value);
                    if ((_bookNo == 1 &&
                            (inputPageNo < 1 ||
                                inputPageNo > maxvalueofBook1)) ||
                        (_bookNo == 2 &&
                            (inputPageNo < 1 ||
                                inputPageNo > maxvalueofBook2)) ||
                        (_bookNo == 3 &&
                            (inputPageNo < 1 ||
                                inputPageNo > maxvalueofBook3)) ||
                        (_bookNo == 4 &&
                            (inputPageNo < 1 ||
                                inputPageNo > maxvalueofBook4)) ||
                        (_bookNo == 5 &&
                            (inputPageNo < 1 ||
                                inputPageNo > maxvalueofBook5)) ||
                        (_bookNo == 6 &&
                            (inputPageNo < 1 ||
                                inputPageNo > maxvalueofBook6)) ||
                        (_bookNo == 7 &&
                            (inputPageNo < 1 ||
                                inputPageNo > maxvalueofBook7)) ||
                        (_bookNo == 8 &&
                            (inputPageNo < 1 ||
                                inputPageNo > maxvalueofBook8))
                                
                                ) {
                      if (_bookNo == 1) {
                        errorMessage =
                            "Please enter a page number between 1 and $maxvalueofBook1.";
                      } else if (_bookNo == 2) {
                        errorMessage =
                            "Please enter an ang number between 1 and $maxvalueofBook2.";
                      }
                      else if (_bookNo == 3) {
                        errorMessage =
                            "Please enter an ang number between 1 and $maxvalueofBook3.";
                      }else if (_bookNo == 4) {
                        errorMessage =
                            "Please enter an ang number between 1 and $maxvalueofBook4.";
                      }else if (_bookNo == 5) {
                        errorMessage =
                            "Please enter an ang number between 1 and $maxvalueofBook5.";
                      }else if (_bookNo == 6) {
                        errorMessage =
                            "Please enter an ang number between 1 and $maxvalueofBook6.";
                      }else if (_bookNo == 7) {
                        errorMessage =
                            "Please enter an ang number between 1 and $maxvalueofBook7.";
                      }else if (_bookNo == 8) {
                        errorMessage =
                            "Please enter an ang number between 1 and $maxvalueofBook8.";
                      }
                      showDialog(
                        context: context,
                        builder: (context) {
                          return AlertDialog(
                            title: const Text(
                              "Invalid Input",
                              style: TextStyle(
                                  color: Colors.black,
                                  fontWeight: FontWeight.bold),
                            ),
                            content: Text(
                              errorMessage,
                              style: const TextStyle(
                                  color: Colors.black,
                                  fontWeight: FontWeight.bold),
                            ),
                            actions: <Widget>[
                              TextButton(
                                onPressed: () {
                                  Navigator.of(context).pop();
                                  _searchController.clear();
                                },
                                child: const Text("OK"),
                              ),
                            ],
                          );
                        },
                      );
                    } else {
                      HistoryItem history = HistoryItem(
                          calculatedItems: calculatedPages.toList(),
                          lines: currentAng,
                          sourcePageNo: _sourcePageNo,
                          pageNo: _pageNo,
                          loadedLines: _loadedLines);
                      _history.add(history);
                      calculatedPages.clear();
                      _loadedLines = 0;
                      _sourcePageNo = int.parse(value);
                      _pageNo = 1;
                      updateGurbaniLists();
                      setState(() {
                        _showSearch = false;
                        _searchController.clear();
                      });
                    }
                  },
                  decoration: InputDecoration(
                    hintText: 'Search',
                    hintStyle: const TextStyle(
                        color: Colors.black, fontWeight: FontWeight.bold),
                    prefixIcon: const Icon(
                      Icons.search,
                      color: Colors.black,
                    ),
                    suffixIcon: IconButton(
                      icon: const Icon(
                        Icons.close,
                        color: Colors.black,
                      ),
                      onPressed: () {
                        _searchController.clear();
                        setState(() {
                          _showSearch = false;
                        });
                      },
                    ),
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10.r),
                      borderSide: BorderSide(
                        width: 1.r,
                        style: BorderStyle.solid,
                      ),
                    ),
                    filled: true,
                    contentPadding: EdgeInsets.all(16.r),
                    fillColor: Colors.white.withOpacity(0.1),
                  ),
                  style: const TextStyle(
                      color: blackColor, fontWeight: FontWeight.bold),
                ))
            : null,
        floatingActionButtonLocation: FloatingActionButtonLocation.centerTop,
      ),
    );
  }
}

class BaaniSearchDelegate extends SearchDelegate<BaaniLineModel> {
  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(
        icon: const Icon(Icons.clear),
        onPressed: () {
          query = '';
        },
      ),
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      icon: const Icon(Icons.arrow_back),
      onPressed: () => Navigator.of(context).pop(),
    );
  }

  @override
  Widget buildResults(BuildContext context) {
    return Container(
      child: const Center(
        child: Text('Search Results'),
      ),
    );
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return Container(
      child: const Center(
        child: Text('Enter Page no to show'),
      ),
    );
  }
}


/*
BaaniPageView(
                    baaniLines: _loadSourceLines || _isNitnem ?
                                currentAng.baaniLines.length ~/ (_linesPerPage*(_pageNo)) ==0 ?
                                  DBResult(baaniLines: List.empty(), count: 0) :
                                  currentAng.baaniLines.length < _pageNo*_linesPerPage ?
                                    DBResult(baaniLines: List.empty(), count: 0) :
                                    DBResult(
                                      baaniLines: currentAng.baaniLines.sublist(
                                          _pageNo*_linesPerPage ,
                                          (_pageNo+1)*_linesPerPage >= currentAng.baaniLines.length ?
                                            currentAng.baaniLines.length :
                                            (_pageNo+1)*_linesPerPage),
                                      count: _linesPerPage) :
                                nextAng,
                    pageNo: (currentAng.count/_linesPerPage).ceil() == _pageNo ? 1 : _pageNo+1,
                    totalPages: _loadSourceLines || _isNitnem ? (currentAng.count/_linesPerPage).ceil() : (nextAng.count/_linesPerPage).ceil(),
                    linesPerPage: _linesPerPage,
                    showEnglishTransliteration: _showEnglishTransliteration,
                    showEnglishTranslation: _showEnglishTranslation,
                    showPunjabiTranslation: _bookNo == 1 ? _showPunjabiTranslation : false,
                    showPunjabiTeekaTranslation: _showPunjabiTeekaTranslation,
                    showFaridkotTeekaTranslation: _showFaridkotTeekaTranslation,
                    showHindiTranslation: _showHindiTranslation,
                    showHindiTeekaTranslation: _showHindiTeekaTranslation,
                    language: _language,
                  isLoading: _isLoading,
                  showAng: _isNitnem ? false : true,
                ),





                ========================================================



                BaaniPageView(
                          baaniLines: _loadSourceLines || _isNitnem ?
                                      currentAng.baaniLines.length < _pageNo*_linesPerPage ?
                                          _pageNo >1 ?
                                            DBResult(
                                                baaniLines: currentAng.baaniLines.sublist(
                                                    (_pageNo-1)*_linesPerPage,
                                                    (_pageNo)*_linesPerPage >= currentAng.baaniLines.length ?
                                                      currentAng.baaniLines.length :
                                                      _pageNo*_linesPerPage
                                                ),
                                                count: _linesPerPage
                                            ) :
                                            DBResult(baaniLines: List.empty(), count: 0) :
                                      DBResult(
                                          baaniLines: currentAng.baaniLines.sublist(
                                              (_pageNo-1)*_linesPerPage,
                                              (_pageNo)*_linesPerPage >= currentAng.baaniLines.length ?
                                                currentAng.baaniLines.length :
                                                _pageNo*_linesPerPage
                                          ),
                                          count: _linesPerPage
                                      ) : currentAng,
                          pageNo: _pageNo,
                          totalPages: (currentAng.count/_linesPerPage).ceil(),
                          linesPerPage: _linesPerPage,
                          showEnglishTransliteration: _showEnglishTransliteration,
                          showEnglishTranslation: _showEnglishTranslation,
                          showPunjabiTranslation: _bookNo == 1 ? _showPunjabiTranslation : false,
                          showPunjabiTeekaTranslation: _showPunjabiTeekaTranslation,
                          showFaridkotTeekaTranslation: _showFaridkotTeekaTranslation,
                          showHindiTranslation: _showHindiTranslation,
                          showHindiTeekaTranslation: _showHindiTeekaTranslation,
                          language: _language,
                        isLoading: _isLoading,
                        showAng: _isNitnem ? false : true,
                      ),

 */
Leave a Comment