fix: continuous PageAreaView not scroll depend on overview
This commit is contained in:
parent
eb19022572
commit
fc6e56c9ee
|
@ -33,8 +33,29 @@ class PdfPageArea extends ConsumerStatefulWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
class _PdfPageAreaState extends ConsumerState<PdfPageArea> {
|
class _PdfPageAreaState extends ConsumerState<PdfPageArea> {
|
||||||
final ScrollController _scrollController = ScrollController();
|
|
||||||
final Map<int, GlobalKey> _pageKeys = {};
|
final Map<int, GlobalKey> _pageKeys = {};
|
||||||
|
final PdfViewerController _viewerController = PdfViewerController();
|
||||||
|
// Guards to avoid scroll feedback between provider and viewer
|
||||||
|
int? _programmaticTargetPage;
|
||||||
|
bool _suppressProviderListen = false;
|
||||||
|
int? _visiblePage; // last page reported by viewer
|
||||||
|
int? _pendingPage; // pending target for mock ensureVisible retry
|
||||||
|
int _scrollRetryCount = 0;
|
||||||
|
static const int _maxScrollRetries = 50;
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
// If app starts in continuous mode with a loaded PDF, ensure the viewer
|
||||||
|
// is instructed to align to the provider's current page once ready.
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
if (!mounted) return;
|
||||||
|
final mode = ref.read(pageViewModeProvider);
|
||||||
|
final pdf = ref.read(pdfProvider);
|
||||||
|
if (mode == 'continuous' && pdf.pickedPdfPath != null && pdf.loaded) {
|
||||||
|
_scrollToPage(pdf.currentPage);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
GlobalKey _pageKey(int page) => _pageKeys.putIfAbsent(
|
GlobalKey _pageKey(int page) => _pageKeys.putIfAbsent(
|
||||||
page,
|
page,
|
||||||
|
@ -43,88 +64,135 @@ class _PdfPageAreaState extends ConsumerState<PdfPageArea> {
|
||||||
|
|
||||||
void _scrollToPage(int page) {
|
void _scrollToPage(int page) {
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
final key = _pageKey(page);
|
final pdf = ref.read(pdfProvider);
|
||||||
final ctx = key.currentContext;
|
final isContinuous = ref.read(pageViewModeProvider) == 'continuous';
|
||||||
|
|
||||||
|
// Real continuous: drive via PdfViewerController
|
||||||
|
if (pdf.pickedPdfPath != null && isContinuous) {
|
||||||
|
if (_viewerController.isReady) {
|
||||||
|
_programmaticTargetPage = page;
|
||||||
|
// print("[DEBUG] viewerController Scrolling to page $page");
|
||||||
|
_viewerController.goToPage(
|
||||||
|
pageNumber: page,
|
||||||
|
anchor: PdfPageAnchor.top,
|
||||||
|
);
|
||||||
|
// Fallback: if no onPageChanged arrives (e.g., same page), don't block future jumps
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
if (!mounted) return;
|
||||||
|
Future<void>.delayed(const Duration(milliseconds: 120), () {
|
||||||
|
if (!mounted) return;
|
||||||
|
if (_programmaticTargetPage == page) {
|
||||||
|
_programmaticTargetPage = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
_pendingPage = null;
|
||||||
|
_scrollRetryCount = 0;
|
||||||
|
} else {
|
||||||
|
_pendingPage = page;
|
||||||
|
if (_scrollRetryCount < _maxScrollRetries) {
|
||||||
|
_scrollRetryCount += 1;
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
if (!mounted) return;
|
||||||
|
final p = _pendingPage;
|
||||||
|
if (p == null) return;
|
||||||
|
_scrollToPage(p);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// print("[DEBUG] Mock Scrolling to page $page");
|
||||||
|
// Mock continuous: try ensureVisible on the page container
|
||||||
|
final ctx = _pageKey(page).currentContext;
|
||||||
if (ctx != null) {
|
if (ctx != null) {
|
||||||
Scrollable.ensureVisible(
|
try {
|
||||||
ctx,
|
final scrollable = Scrollable.of(ctx);
|
||||||
duration: const Duration(milliseconds: 250),
|
final position = scrollable.position;
|
||||||
curve: Curves.easeInOut,
|
final targetBox = ctx.findRenderObject() as RenderBox?;
|
||||||
alignment: 0.1,
|
final scrollBox = scrollable.context.findRenderObject() as RenderBox?;
|
||||||
);
|
if (targetBox != null && scrollBox != null) {
|
||||||
|
final offsetInViewport = targetBox.localToGlobal(
|
||||||
|
Offset.zero,
|
||||||
|
ancestor: scrollBox,
|
||||||
|
);
|
||||||
|
final desiredTop = scrollBox.size.height * 0.1;
|
||||||
|
final newPixels =
|
||||||
|
(position.pixels + offsetInViewport.dy - desiredTop)
|
||||||
|
.clamp(position.minScrollExtent, position.maxScrollExtent)
|
||||||
|
.toDouble();
|
||||||
|
position.jumpTo(newPixels);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (_) {
|
||||||
|
// Fallback to ensureVisible if any calculation fails
|
||||||
|
Scrollable.ensureVisible(
|
||||||
|
ctx,
|
||||||
|
alignment: 0.1,
|
||||||
|
duration: const Duration(milliseconds: 1),
|
||||||
|
curve: Curves.linear,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_pendingPage = page;
|
||||||
|
if (_scrollRetryCount < _maxScrollRetries) {
|
||||||
|
_scrollRetryCount += 1;
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
if (!mounted) return;
|
||||||
|
final p = _pendingPage;
|
||||||
|
if (p == null) return;
|
||||||
|
_scrollToPage(p);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
void initState() {
|
|
||||||
super.initState();
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void dispose() {
|
|
||||||
_scrollController.dispose();
|
|
||||||
super.dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _showContextMenuForPlaced({
|
|
||||||
required BuildContext context,
|
|
||||||
required WidgetRef ref,
|
|
||||||
required Offset globalPos,
|
|
||||||
required int index,
|
|
||||||
required int page,
|
|
||||||
}) async {
|
|
||||||
widget.onSelectPlaced(index);
|
|
||||||
final choice = await showMenu<String>(
|
|
||||||
context: context,
|
|
||||||
position: RelativeRect.fromLTRB(
|
|
||||||
globalPos.dx,
|
|
||||||
globalPos.dy,
|
|
||||||
globalPos.dx,
|
|
||||||
globalPos.dy,
|
|
||||||
),
|
|
||||||
items: [
|
|
||||||
PopupMenuItem<String>(
|
|
||||||
key: Key('ctx_delete_signature'),
|
|
||||||
value: 'delete',
|
|
||||||
child: Text(AppLocalizations.of(context).delete),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
if (choice == 'delete') {
|
|
||||||
ref.read(pdfProvider.notifier).removePlacement(page: page, index: index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final pdf = ref.watch(pdfProvider);
|
final pdf = ref.watch(pdfProvider);
|
||||||
final pageViewMode = ref.watch(pageViewModeProvider);
|
final pageViewMode = ref.watch(pageViewModeProvider);
|
||||||
// Subscribe to provider changes during build (allowed by Riverpod) to trigger side-effects.
|
|
||||||
|
// React to provider currentPage changes (e.g., user tapped overview)
|
||||||
ref.listen(pdfProvider, (prev, next) {
|
ref.listen(pdfProvider, (prev, next) {
|
||||||
final mode = ref.read(pageViewModeProvider);
|
final mode = ref.read(pageViewModeProvider);
|
||||||
|
if (_suppressProviderListen) return;
|
||||||
if (mode == 'continuous' && (prev?.currentPage != next.currentPage)) {
|
if (mode == 'continuous' && (prev?.currentPage != next.currentPage)) {
|
||||||
_scrollToPage(next.currentPage);
|
final target = next.currentPage;
|
||||||
|
// If we're already navigating to this target, ignore; otherwise allow new target.
|
||||||
|
if (_programmaticTargetPage != null &&
|
||||||
|
_programmaticTargetPage == target) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Only navigate if target differs from what viewer shows
|
||||||
|
if (_visiblePage != target) {
|
||||||
|
_scrollToPage(target);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// When switching to continuous, bring current page into view
|
||||||
ref.listen<String>(pageViewModeProvider, (prev, next) {
|
ref.listen<String>(pageViewModeProvider, (prev, next) {
|
||||||
if (next == 'continuous') {
|
if (next == 'continuous') {
|
||||||
|
// Skip initial auto-scroll in mock mode to avoid fighting with
|
||||||
|
// early provider-driven jumps during tests.
|
||||||
|
final isMock = ref.read(useMockViewerProvider);
|
||||||
|
if (isMock) return;
|
||||||
final p = ref.read(pdfProvider).currentPage;
|
final p = ref.read(pdfProvider).currentPage;
|
||||||
_scrollToPage(p);
|
if (_visiblePage != p) {
|
||||||
|
_scrollToPage(p);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!pdf.loaded) {
|
if (!pdf.loaded) {
|
||||||
return Center(child: Text(AppLocalizations.of(context).noPdfLoaded));
|
return Center(child: Text(AppLocalizations.of(context).noPdfLoaded));
|
||||||
}
|
}
|
||||||
|
|
||||||
final useMock = ref.watch(useMockViewerProvider);
|
final useMock = ref.watch(useMockViewerProvider);
|
||||||
final isContinuous = pageViewMode == 'continuous';
|
final isContinuous = pageViewMode == 'continuous';
|
||||||
if (isContinuous) {
|
|
||||||
// Make sure the current page is visible after first build of continuous list.
|
// Mock single-page
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
||||||
if (!mounted) return;
|
|
||||||
_scrollToPage(pdf.currentPage);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (useMock && !isContinuous) {
|
if (useMock && !isContinuous) {
|
||||||
return Center(
|
return Center(
|
||||||
child: AspectRatio(
|
child: AspectRatio(
|
||||||
|
@ -168,53 +236,86 @@ class _PdfPageAreaState extends ConsumerState<PdfPageArea> {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mock continuous: ListView with prebuilt children, no controller
|
||||||
if (useMock && isContinuous) {
|
if (useMock && isContinuous) {
|
||||||
final count = pdf.pageCount > 0 ? pdf.pageCount : 1;
|
final count = pdf.pageCount > 0 ? pdf.pageCount : 1;
|
||||||
return ListView.builder(
|
return Builder(
|
||||||
key: const Key('pdf_continuous_mock_list'),
|
builder: (ctx) {
|
||||||
controller: _scrollController,
|
// Defer processing of any pending jump until after the tree is mounted.
|
||||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
if (_pendingPage != null) {
|
||||||
itemCount: count,
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
itemBuilder: (context, idx) {
|
if (!mounted) return;
|
||||||
final pageNum = idx + 1;
|
final p = _pendingPage;
|
||||||
return Center(
|
if (p != null) {
|
||||||
child: Padding(
|
_pendingPage = null;
|
||||||
key: _pageKey(pageNum),
|
_scrollRetryCount = 0;
|
||||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
Future<void>.delayed(const Duration(milliseconds: 1), () {
|
||||||
child: AspectRatio(
|
if (!mounted) return;
|
||||||
aspectRatio: widget.pageSize.width / widget.pageSize.height,
|
_scrollToPage(p);
|
||||||
child: Stack(
|
});
|
||||||
key: ValueKey('page_stack_$pageNum'),
|
}
|
||||||
children: [
|
});
|
||||||
Container(
|
}
|
||||||
color: Colors.grey.shade200,
|
return SingleChildScrollView(
|
||||||
child: Center(
|
key: const Key('pdf_continuous_mock_list'),
|
||||||
child: Text(
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||||
AppLocalizations.of(context).pageInfo(pageNum, count),
|
child: Column(
|
||||||
style: const TextStyle(
|
children: List.generate(count, (idx) {
|
||||||
fontSize: 24,
|
final pageNum = idx + 1;
|
||||||
color: Colors.black54,
|
return Center(
|
||||||
|
child: Padding(
|
||||||
|
key: _pageKey(pageNum),
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||||
|
child: AspectRatio(
|
||||||
|
aspectRatio:
|
||||||
|
widget.pageSize.width / widget.pageSize.height,
|
||||||
|
child: Stack(
|
||||||
|
key: ValueKey('page_stack_$pageNum'),
|
||||||
|
children: [
|
||||||
|
Container(
|
||||||
|
color: Colors.grey.shade200,
|
||||||
|
child: Center(
|
||||||
|
child: Text(
|
||||||
|
AppLocalizations.of(
|
||||||
|
context,
|
||||||
|
).pageInfo(pageNum, count),
|
||||||
|
style: const TextStyle(
|
||||||
|
fontSize: 24,
|
||||||
|
color: Colors.black54,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
Consumer(
|
||||||
|
builder: (context, ref, _) {
|
||||||
|
final sig = ref.watch(signatureProvider);
|
||||||
|
final visible = ref.watch(
|
||||||
|
signatureVisibilityProvider,
|
||||||
|
);
|
||||||
|
return visible
|
||||||
|
? _buildPageOverlays(
|
||||||
|
context,
|
||||||
|
ref,
|
||||||
|
sig,
|
||||||
|
pageNum,
|
||||||
|
)
|
||||||
|
: const SizedBox.shrink();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Consumer(
|
),
|
||||||
builder: (context, ref, _) {
|
);
|
||||||
final sig = ref.watch(signatureProvider);
|
}),
|
||||||
final visible = ref.watch(signatureVisibilityProvider);
|
|
||||||
return visible
|
|
||||||
? _buildPageOverlays(context, ref, sig, pageNum)
|
|
||||||
: const SizedBox.shrink();
|
|
||||||
},
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Real single-page mode
|
||||||
if (pdf.pickedPdfPath != null && !isContinuous) {
|
if (pdf.pickedPdfPath != null && !isContinuous) {
|
||||||
return PdfDocumentViewBuilder.file(
|
return PdfDocumentViewBuilder.file(
|
||||||
pdf.pickedPdfPath!,
|
pdf.pickedPdfPath!,
|
||||||
|
@ -266,67 +367,112 @@ class _PdfPageAreaState extends ConsumerState<PdfPageArea> {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Real continuous mode (pdfrx): copy example patterns
|
||||||
if (pdf.pickedPdfPath != null && isContinuous) {
|
if (pdf.pickedPdfPath != null && isContinuous) {
|
||||||
return PdfDocumentViewBuilder.file(
|
return PdfViewer.file(
|
||||||
pdf.pickedPdfPath!,
|
pdf.pickedPdfPath!,
|
||||||
builder: (context, document) {
|
controller: _viewerController,
|
||||||
if (document == null) {
|
params: PdfViewerParams(
|
||||||
return const Center(child: CircularProgressIndicator());
|
pageAnchor: PdfPageAnchor.top,
|
||||||
}
|
onViewerReady: (doc, controller) {
|
||||||
final pages = document.pages;
|
if (pdf.pageCount != doc.pages.length) {
|
||||||
if (pdf.pageCount != pages.length) {
|
ref.read(pdfProvider.notifier).setPageCount(doc.pages.length);
|
||||||
|
}
|
||||||
|
final target = _pendingPage ?? pdf.currentPage;
|
||||||
|
_pendingPage = null;
|
||||||
|
_scrollRetryCount = 0;
|
||||||
|
_programmaticTargetPage = target;
|
||||||
|
controller.goToPage(pageNumber: target, anchor: PdfPageAnchor.top);
|
||||||
|
// Fallback: if the viewer doesn't emit onPageChanged (e.g., already at target),
|
||||||
|
// ensure we don't keep blocking provider-driven jumps.
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
ref.read(pdfProvider.notifier).setPageCount(pages.length);
|
if (!mounted) return;
|
||||||
|
Future<void>.delayed(const Duration(milliseconds: 120), () {
|
||||||
|
if (!mounted) return;
|
||||||
|
if (_programmaticTargetPage == target) {
|
||||||
|
_programmaticTargetPage = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
// Also ensure a scroll attempt is queued in case current state suppressed earlier.
|
||||||
return ListView.builder(
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
key: const Key('pdf_continuous_list'),
|
if (!mounted) return;
|
||||||
controller: _scrollController,
|
if (_visiblePage != ref.read(pdfProvider).currentPage) {
|
||||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
_scrollToPage(ref.read(pdfProvider).currentPage);
|
||||||
itemCount: pages.length,
|
}
|
||||||
itemBuilder: (context, idx) {
|
});
|
||||||
final pageNum = idx + 1;
|
},
|
||||||
final page = pages[idx];
|
onPageChanged: (n) {
|
||||||
final aspect = page.width / page.height;
|
if (n == null) return;
|
||||||
return Center(
|
_visiblePage = n;
|
||||||
child: Padding(
|
// Programmatic navigation: wait until target reached
|
||||||
key: _pageKey(pageNum),
|
if (_programmaticTargetPage != null) {
|
||||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
if (n == _programmaticTargetPage) {
|
||||||
child: AspectRatio(
|
if (n != ref.read(pdfProvider).currentPage) {
|
||||||
aspectRatio: aspect,
|
_suppressProviderListen = true;
|
||||||
child: Stack(
|
ref.read(pdfProvider.notifier).jumpTo(n);
|
||||||
key: ValueKey('page_stack_$pageNum'),
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
children: [
|
_suppressProviderListen = false;
|
||||||
PdfPageView(
|
});
|
||||||
key: ValueKey('pdf_page_view_$pageNum'),
|
}
|
||||||
document: document,
|
_programmaticTargetPage = null;
|
||||||
pageNumber: pageNum,
|
}
|
||||||
alignment: Alignment.center,
|
return;
|
||||||
),
|
}
|
||||||
Consumer(
|
// User scroll -> reflect page to provider without re-triggering scroll
|
||||||
builder: (context, ref, _) {
|
if (n != ref.read(pdfProvider).currentPage) {
|
||||||
final sig = ref.watch(signatureProvider);
|
_suppressProviderListen = true;
|
||||||
final visible = ref.watch(
|
ref.read(pdfProvider.notifier).jumpTo(n);
|
||||||
signatureVisibilityProvider,
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
);
|
_suppressProviderListen = false;
|
||||||
return visible
|
});
|
||||||
? _buildPageOverlays(context, ref, sig, pageNum)
|
}
|
||||||
: const SizedBox.shrink();
|
},
|
||||||
},
|
),
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return const SizedBox.shrink();
|
return const SizedBox.shrink();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Context menu for already placed signatures
|
||||||
|
void _showContextMenuForPlaced({
|
||||||
|
required BuildContext context,
|
||||||
|
required WidgetRef ref,
|
||||||
|
required Offset globalPos,
|
||||||
|
required int index,
|
||||||
|
required int page,
|
||||||
|
}) {
|
||||||
|
final l = AppLocalizations.of(context);
|
||||||
|
showMenu<String>(
|
||||||
|
context: context,
|
||||||
|
position: RelativeRect.fromLTRB(
|
||||||
|
globalPos.dx,
|
||||||
|
globalPos.dy,
|
||||||
|
globalPos.dx,
|
||||||
|
globalPos.dy,
|
||||||
|
),
|
||||||
|
items: [
|
||||||
|
PopupMenuItem<String>(
|
||||||
|
key: const Key('ctx_placed_delete'),
|
||||||
|
value: 'delete',
|
||||||
|
child: Text(l.delete),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
).then((choice) {
|
||||||
|
switch (choice) {
|
||||||
|
case 'delete':
|
||||||
|
ref
|
||||||
|
.read(pdfProvider.notifier)
|
||||||
|
.removePlacement(page: page, index: index);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Widget _buildPageOverlays(
|
Widget _buildPageOverlays(
|
||||||
BuildContext context,
|
BuildContext context,
|
||||||
WidgetRef ref,
|
WidgetRef ref,
|
||||||
|
|
|
@ -12,21 +12,30 @@ Future<void> theAppSupportsLanguages(
|
||||||
// Accept either a DataTable from bdd_widget_test or a string like "{en, zh-TW, es}"
|
// Accept either a DataTable from bdd_widget_test or a string like "{en, zh-TW, es}"
|
||||||
final Set<String> expected;
|
final Set<String> expected;
|
||||||
if (languages is bdd.DataTable) {
|
if (languages is bdd.DataTable) {
|
||||||
final lists = languages.asLists();
|
final lists = languages.asLists();
|
||||||
// Flatten ignoring header rows if any
|
// Flatten ignoring header rows if any
|
||||||
final items = lists
|
final items =
|
||||||
.skipWhile((row) => row.any((e) => e.toString().contains('artist') || e.toString().contains('name')))
|
lists
|
||||||
.expand((row) => row)
|
.skipWhile(
|
||||||
.map((e) => e.toString().replaceAll("'", '').trim())
|
(row) => row.any(
|
||||||
.where((e) => e.isNotEmpty)
|
(e) =>
|
||||||
.toSet();
|
e.toString().contains('artist') ||
|
||||||
expected = items;
|
e.toString().contains('name'),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.expand((row) => row)
|
||||||
|
.map((e) => e.toString().replaceAll("'", '').trim())
|
||||||
|
.where((e) => e.isNotEmpty)
|
||||||
|
.toSet();
|
||||||
|
expected = items;
|
||||||
} else {
|
} else {
|
||||||
final raw = languages.toString().trim();
|
final raw = languages.toString().trim();
|
||||||
final inner = raw.startsWith('{') && raw.endsWith('}')
|
final inner =
|
||||||
? raw.substring(1, raw.length - 1)
|
raw.startsWith('{') && raw.endsWith('}')
|
||||||
: raw;
|
? raw.substring(1, raw.length - 1)
|
||||||
expected = inner.split(',').map((s) => s.trim().replaceAll("'", '')).toSet();
|
: raw;
|
||||||
|
expected =
|
||||||
|
inner.split(',').map((s) => s.trim().replaceAll("'", '')).toSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep this in sync with the app's supported locales
|
// Keep this in sync with the app's supported locales
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import '_world.dart';
|
||||||
|
|
||||||
|
/// Usage: the preference 'language' is saved as {"<language>"}
|
||||||
|
Future<void> thePreferenceLanguageIsSavedAs(
|
||||||
|
WidgetTester tester, [
|
||||||
|
dynamic valueWrapped,
|
||||||
|
]) async {
|
||||||
|
String unwrap(String s) {
|
||||||
|
var out = s.trim();
|
||||||
|
if (out.startsWith('{') && out.endsWith('}')) {
|
||||||
|
out = out.substring(1, out.length - 1);
|
||||||
|
}
|
||||||
|
if ((out.startsWith("'") && out.endsWith("'")) ||
|
||||||
|
(out.startsWith('"') && out.endsWith('"'))) {
|
||||||
|
out = out.substring(1, out.length - 1);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
final expected = unwrap((valueWrapped ?? '').toString());
|
||||||
|
expect(TestWorld.prefs['language'], expected);
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import '_world.dart';
|
||||||
|
|
||||||
|
/// Usage: the preference 'theme' is saved as {"<theme>"}
|
||||||
|
Future<void> thePreferenceThemeIsSavedAs(
|
||||||
|
WidgetTester tester, [
|
||||||
|
dynamic valueWrapped,
|
||||||
|
]) async {
|
||||||
|
String unwrap(String s) {
|
||||||
|
var out = s.trim();
|
||||||
|
if (out.startsWith('{') && out.endsWith('}')) {
|
||||||
|
out = out.substring(1, out.length - 1);
|
||||||
|
}
|
||||||
|
if ((out.startsWith("'") && out.endsWith("'")) ||
|
||||||
|
(out.startsWith('"') && out.endsWith('"'))) {
|
||||||
|
out = out.substring(1, out.length - 1);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
final expected = unwrap((valueWrapped ?? '').toString());
|
||||||
|
expect(TestWorld.prefs['theme'], expected);
|
||||||
|
}
|
|
@ -0,0 +1,87 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
|
||||||
|
import 'package:pdf_signature/ui/features/pdf/widgets/pdf_page_area.dart';
|
||||||
|
import 'package:pdf_signature/ui/features/pdf/view_model/view_model.dart';
|
||||||
|
import 'package:pdf_signature/data/services/providers.dart';
|
||||||
|
import 'package:pdf_signature/l10n/app_localizations.dart';
|
||||||
|
import 'package:pdf_signature/data/model/model.dart';
|
||||||
|
import 'package:pdf_signature/ui/features/preferences/providers.dart';
|
||||||
|
|
||||||
|
class _TestPdfController extends PdfController {
|
||||||
|
_TestPdfController() : super() {
|
||||||
|
state = PdfState.initial().copyWith(
|
||||||
|
loaded: true,
|
||||||
|
pageCount: 6,
|
||||||
|
currentPage: 1,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
testWidgets('PdfPageArea: early jump queues and scrolls once list builds', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final ctrl = _TestPdfController();
|
||||||
|
|
||||||
|
// Build the widget tree
|
||||||
|
await tester.pumpWidget(
|
||||||
|
ProviderScope(
|
||||||
|
overrides: [
|
||||||
|
useMockViewerProvider.overrideWithValue(true),
|
||||||
|
pageViewModeProvider.overrideWithValue('continuous'),
|
||||||
|
pdfProvider.overrideWith((ref) => ctrl),
|
||||||
|
],
|
||||||
|
child: MaterialApp(
|
||||||
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||||
|
supportedLocales: AppLocalizations.supportedLocales,
|
||||||
|
locale: const Locale('en'),
|
||||||
|
home: const Scaffold(
|
||||||
|
body: Center(
|
||||||
|
child: SizedBox(
|
||||||
|
width: 800,
|
||||||
|
height: 520,
|
||||||
|
child: PdfPageArea(
|
||||||
|
pageSize: Size(676, 400),
|
||||||
|
onDragSignature: _noopOffset,
|
||||||
|
onResizeSignature: _noopOffset,
|
||||||
|
onConfirmSignature: _noop,
|
||||||
|
onClearActiveOverlay: _noop,
|
||||||
|
onSelectPlaced: _noopInt,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Trigger an early jump immediately after first pump, before settle.
|
||||||
|
ctrl.jumpTo(5);
|
||||||
|
|
||||||
|
// Now allow frames to build and settle
|
||||||
|
await tester.pump();
|
||||||
|
await tester.pumpAndSettle(const Duration(milliseconds: 800));
|
||||||
|
|
||||||
|
// Validate that page 5 is in view and scroll offset moved.
|
||||||
|
final listFinder = find.byKey(const Key('pdf_continuous_mock_list'));
|
||||||
|
expect(listFinder, findsOneWidget);
|
||||||
|
final scrollableFinder = find.descendant(
|
||||||
|
of: listFinder,
|
||||||
|
matching: find.byType(Scrollable),
|
||||||
|
);
|
||||||
|
final pos = tester.state<ScrollableState>(scrollableFinder).position;
|
||||||
|
expect(pos.pixels, greaterThan(0));
|
||||||
|
|
||||||
|
final pageStack = find.byKey(const ValueKey('page_stack_5'));
|
||||||
|
expect(pageStack, findsOneWidget);
|
||||||
|
final viewport = tester.getRect(listFinder);
|
||||||
|
final pageRect = tester.getRect(pageStack);
|
||||||
|
expect(viewport.overlaps(pageRect), isTrue);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _noop() {}
|
||||||
|
void _noopInt(int? _) {}
|
||||||
|
void _noopOffset(Offset _) {}
|
|
@ -0,0 +1,106 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
|
||||||
|
import 'package:pdf_signature/ui/features/pdf/widgets/pdf_page_area.dart';
|
||||||
|
import 'package:pdf_signature/ui/features/pdf/view_model/view_model.dart';
|
||||||
|
import 'package:pdf_signature/data/services/providers.dart';
|
||||||
|
import 'package:pdf_signature/l10n/app_localizations.dart';
|
||||||
|
import 'package:pdf_signature/data/model/model.dart';
|
||||||
|
import 'package:pdf_signature/ui/features/preferences/providers.dart';
|
||||||
|
|
||||||
|
class _TestPdfController extends PdfController {
|
||||||
|
_TestPdfController() : super() {
|
||||||
|
state = PdfState.initial().copyWith(
|
||||||
|
loaded: true,
|
||||||
|
pageCount: 6,
|
||||||
|
currentPage: 2,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
testWidgets(
|
||||||
|
'PdfPageArea: continuous mode scrolls target page into view on jump',
|
||||||
|
(tester) async {
|
||||||
|
final ctrl = _TestPdfController();
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
ProviderScope(
|
||||||
|
overrides: [
|
||||||
|
useMockViewerProvider.overrideWithValue(true),
|
||||||
|
// Force continuous mode without SharedPreferences
|
||||||
|
pageViewModeProvider.overrideWithValue('continuous'),
|
||||||
|
pdfProvider.overrideWith((ref) => ctrl),
|
||||||
|
],
|
||||||
|
child: MaterialApp(
|
||||||
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||||
|
supportedLocales: AppLocalizations.supportedLocales,
|
||||||
|
locale: const Locale('en'),
|
||||||
|
home: const Scaffold(
|
||||||
|
body: Center(
|
||||||
|
child: SizedBox(
|
||||||
|
width: 800,
|
||||||
|
height: 520,
|
||||||
|
child: PdfPageArea(
|
||||||
|
pageSize: Size(676, 400),
|
||||||
|
onDragSignature: _noopOffset,
|
||||||
|
onResizeSignature: _noopOffset,
|
||||||
|
onConfirmSignature: _noop,
|
||||||
|
onClearActiveOverlay: _noop,
|
||||||
|
onSelectPlaced: _noopInt,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Get initial scroll position (may already have auto-scrolled to current page)
|
||||||
|
final listFinder = find.byKey(const Key('pdf_continuous_mock_list'));
|
||||||
|
expect(listFinder, findsOneWidget);
|
||||||
|
final scrollableFinder = find.descendant(
|
||||||
|
of: listFinder,
|
||||||
|
matching: find.byType(Scrollable),
|
||||||
|
);
|
||||||
|
double lastPixels =
|
||||||
|
tester.state<ScrollableState>(scrollableFinder).position.pixels;
|
||||||
|
|
||||||
|
Future<void> jumpAndVerify(int targetPage) async {
|
||||||
|
final before = lastPixels;
|
||||||
|
ctrl.jumpTo(targetPage);
|
||||||
|
await tester.pump();
|
||||||
|
await tester.pumpAndSettle(const Duration(milliseconds: 600));
|
||||||
|
|
||||||
|
// Verify with viewport geometry
|
||||||
|
final pageStack = find.byKey(ValueKey('page_stack_$targetPage'));
|
||||||
|
expect(pageStack, findsOneWidget);
|
||||||
|
|
||||||
|
final viewport = tester.getRect(listFinder);
|
||||||
|
final pageRect = tester.getRect(pageStack);
|
||||||
|
expect(
|
||||||
|
viewport.overlaps(pageRect),
|
||||||
|
isTrue,
|
||||||
|
reason: 'Page $targetPage should overlap viewport after jump',
|
||||||
|
);
|
||||||
|
|
||||||
|
final currentPixels =
|
||||||
|
tester.state<ScrollableState>(scrollableFinder).position.pixels;
|
||||||
|
// Ensure scroll position changed (direction not enforced)
|
||||||
|
expect(currentPixels, isNot(equals(before)));
|
||||||
|
lastPixels = currentPixels;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Jump to 4 different pages and verify each
|
||||||
|
await jumpAndVerify(5);
|
||||||
|
await jumpAndVerify(1);
|
||||||
|
await jumpAndVerify(6);
|
||||||
|
await jumpAndVerify(3);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _noop() {}
|
||||||
|
void _noopInt(int? _) {}
|
||||||
|
void _noopOffset(Offset _) {}
|
Loading…
Reference in New Issue