-
Notifications
You must be signed in to change notification settings - Fork 5
Add support for variable page sizes. (Resolves #59) #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
angelosilvestre
wants to merge
6
commits into
main
Choose a base branch
from
59_variable_page_size
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bf4abbe
Add support for variable page sizes. (Resolves #59)
angelosilvestre a68c458
Fix zoom out bug
angelosilvestre 35b6acc
Create dedicated widgets for fixed and variable page size
angelosilvestre 9c74611
Add docs
angelosilvestre 2163bcf
Make _constrainOriginToViewportBounds use the width of the page at th…
angelosilvestre 43ccd06
Add tests
angelosilvestre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
include ':app' | ||
pluginManagement { | ||
def flutterSdkPath = { | ||
def properties = new Properties() | ||
file("local.properties").withInputStream { properties.load(it) } | ||
def flutterSdkPath = properties.getProperty("flutter.sdk") | ||
assert flutterSdkPath != null, "flutter.sdk not set in local.properties" | ||
return flutterSdkPath | ||
}() | ||
|
||
def localPropertiesFile = new File(rootProject.projectDir, "local.properties") | ||
def properties = new Properties() | ||
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle") | ||
|
||
assert localPropertiesFile.exists() | ||
localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) } | ||
repositories { | ||
google() | ||
mavenCentral() | ||
gradlePluginPortal() | ||
} | ||
} | ||
|
||
def flutterSdkPath = properties.getProperty("flutter.sdk") | ||
assert flutterSdkPath != null, "flutter.sdk not set in local.properties" | ||
apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle" | ||
plugins { | ||
id "dev.flutter.flutter-plugin-loader" version "1.0.0" // apply true | ||
id "com.android.application" version "8.3.0" apply false | ||
id "org.jetbrains.kotlin.android" version "1.8.10" apply false | ||
} | ||
|
||
include ":app" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:logging/logging.dart'; | ||
import 'package:page_list_viewport/page_list_viewport.dart'; | ||
|
||
void main() { | ||
PageListViewportLogs.initLoggers(Level.ALL, {}); | ||
|
||
runApp(const MyApp()); | ||
} | ||
|
||
class MyApp extends StatelessWidget { | ||
const MyApp({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const MaterialApp( | ||
title: 'Page List Viewport Variable Size Demo', | ||
home: MyHomePage(), | ||
); | ||
} | ||
} | ||
|
||
class MyHomePage extends StatefulWidget { | ||
const MyHomePage({ | ||
super.key, | ||
}); | ||
|
||
@override | ||
State<MyHomePage> createState() => _MyHomePageState(); | ||
} | ||
|
||
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin { | ||
static const _pageCount = 200; | ||
|
||
static const _pageSizes = [ | ||
Size(8.5, 11), // Letter | ||
Size(11, 8.5), // Letter (inverse) | ||
]; | ||
|
||
late final PageListViewportWithVariableSizeController _controller; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_controller = PageListViewportWithVariableSizeController.startAtPage(pageIndex: 5, vsync: this); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_controller.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
body: Row( | ||
crossAxisAlignment: CrossAxisAlignment.stretch, | ||
children: [ | ||
SizedBox( | ||
width: 125, | ||
child: _buildThumbnailList(), | ||
), | ||
Expanded( | ||
child: Center( | ||
child: SizedBox( | ||
height: _pageSizes[0].height * 72 * MediaQuery.of(context).devicePixelRatio, | ||
width: _pageSizes[0].width * 72 * MediaQuery.of(context).devicePixelRatio, | ||
child: _buildViewport(), | ||
), | ||
), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
|
||
Widget _buildViewport() { | ||
return Stack( | ||
children: [ | ||
PageListViewportGestures( | ||
controller: _controller, | ||
lockPanAxis: true, | ||
child: PageListViewport.variedPageSized( | ||
controller: _controller, | ||
pageCount: _pageCount, | ||
onGetNaturalPageSize: (pageIndex) => | ||
_pageSizes[pageIndex % _pageSizes.length] * 72 * MediaQuery.of(context).devicePixelRatio, | ||
pageLayoutCacheCount: 3, | ||
pagePaintCacheCount: 3, | ||
builder: (BuildContext context, int pageIndex) { | ||
return Stack( | ||
children: [ | ||
Positioned.fill( | ||
child: GestureDetector( | ||
onTap: () => print('tapped at pageIndex: $pageIndex'), | ||
child: _buildPage(pageIndex), | ||
), | ||
), | ||
], | ||
); | ||
}, | ||
), | ||
), | ||
], | ||
); | ||
} | ||
|
||
Widget _buildThumbnailList() { | ||
return RepaintBoundary( | ||
child: ColoredBox( | ||
color: Colors.grey.shade800, | ||
child: ListView.builder( | ||
padding: const EdgeInsets.all(16), | ||
itemCount: _pageCount, | ||
itemBuilder: (context, index) { | ||
return Padding( | ||
padding: const EdgeInsets.symmetric(vertical: 8), | ||
child: GestureDetector( | ||
onTap: () { | ||
_controller.animateToPage(index, const Duration(milliseconds: 250)); | ||
}, | ||
child: AspectRatio( | ||
aspectRatio: _pageSizes[index % _pageSizes.length].aspectRatio, | ||
child: _buildPage(index), | ||
), | ||
), | ||
); | ||
}, | ||
), | ||
), | ||
); | ||
} | ||
|
||
Widget _buildPage(int pageIndex) { | ||
final pageSize = Size( | ||
_pageSizes[pageIndex % _pageSizes.length].width * 72, | ||
_pageSizes[pageIndex % _pageSizes.length].height * 72, | ||
); | ||
|
||
return Container( | ||
color: Colors.white, | ||
child: Container( | ||
color: Colors.primaries[pageIndex % Colors.primaries.length], | ||
width: pageSize.width, | ||
height: pageSize.height, | ||
child: Center( | ||
child: LayoutBuilder(builder: (context, constraints) { | ||
return Container( | ||
width: 300 * (constraints.maxWidth / pageSize.width), | ||
height: 300 * (constraints.maxHeight / pageSize.height), | ||
decoration: const BoxDecoration( | ||
color: Colors.white, | ||
shape: BoxShape.circle, | ||
), | ||
alignment: Alignment.center, | ||
child: Padding( | ||
padding: const EdgeInsets.all(4.0), | ||
child: FittedBox( | ||
child: Text( | ||
'Page $pageIndex', | ||
style: const TextStyle(fontSize: 24), | ||
), | ||
), | ||
), | ||
); | ||
}), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dodgog I'm reviewing this PR with @angelosilvestre - what do you think about this widget API? Do you see anything missing in terms of what you need for varied page sizes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks fine, yes. I can see the alternative being: passing a list of page sizes and also a function onGetNaturalSizeForPage() [doesn't have arguments]. Or is there a design constraint requiring onGetNaturalPageSize to be a different function for each page?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dodgog
A list of page sizes sounds good to me, but I didn't understand why we would have an onGetNaturalSizeForPage() without arguments if the list of page sizes is provided.
Could you clarify that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The api looks good. It's either a list of sizes, or a PageSizeResolver with a cache.
It may not always be cheap to obtain a list of sizes in advance, that's why onGetSize can be helpful to dynamically build the viewport.
Just to confirm my understanding: does the total length of the viewport need to be known in advance in order to render the widget?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, only a
PageSizeResolver
is allowed. Do you want another option to give a list of sizes instead?The
pageCount
is required in order to render the widget.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's no need for a list of sizes. The consumer of the api could probably implement a convenience resolver which would have cached values, for example a getSize(i) function which would map to sizesList[i] as you do in the example app roughly. This is good for now. Can proceed with final reviews and merging probably.
Let's not act on this now, but I'm wondering if it will be easy/worth it to make the calculation and per-page layout lazy? Would have to involve some complex scrolling approximations, but how hard is it?