Skip to content

Commit

Permalink
feat: NestedScrollView related optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
xuelongqy committed Jul 31, 2022
1 parent 85af22f commit 1478c2c
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 136 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Next
## V 3.0.3
> fix: processedDuration == Duration.zero, can't rebound [#572](https://github.com/xuelongqy/flutter_easy_refresh/pull/572).
> fix: [clamping] may not have rebound animation.
> fix: Indicator overflow [#575](https://github.com/xuelongqy/flutter_easy_refresh/pull/575).
Expand Down
1 change: 1 addition & 0 deletions example/lib/l10n/translations/en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ const en = {
'Dogecoin donation': 'Dogecoin donation',
'%s copied!': '%s copied!',
'Trigger immediately': 'Trigger immediately',
'TabBarView example': 'NestedScrollView + TabBarView example',
};
1 change: 1 addition & 0 deletions example/lib/l10n/translations/zh_cn.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ const zhCN = {
'Dogecoin donation': '狗狗币捐赠',
'%s copied!': '%s 已复制!',
'Trigger immediately': '立即触发',
'TabBarView example': 'NestedScrollView + TabBarView示例',
};
38 changes: 15 additions & 23 deletions example/lib/page/sample/nested_scroll_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,23 @@ class NestedScrollViewPageState extends State<NestedScrollViewPage>
},
body: Column(
children: <Widget>[
PreferredSize(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 8,
TabBar(
controller: _tabController,
labelColor: themeData.colorScheme.primary,
indicatorColor: themeData.colorScheme.primary,
onTap: (index) {
setState(() {
_tabIndex = index;
});
},
tabs: const <Widget>[
Tab(
text: 'List',
),
child: TabBar(
controller: _tabController,
labelColor: themeData.colorScheme.primary,
indicatorColor: themeData.colorScheme.primary,
onTap: (index) {
setState(() {
_tabIndex = index;
});
},
tabs: const <Widget>[
Tab(
text: 'List',
),
Tab(
text: 'Grid',
),
],
Tab(
text: 'Grid',
),
),
preferredSize: const Size(double.infinity, 46.0),
],
),
Expanded(
child: IndexedStack(
Expand Down
6 changes: 3 additions & 3 deletions example/lib/page/sample/sample_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class _SamplePageState extends State<SamplePage> {
onTap: () => Get.toNamed(Routes.nestedScrollViewSample),
),
ListItem(
title: 'TabView',
subtitle: 'NestedScrollView example'.tr,
icon: Icons.line_style,
title: 'TabBarView',
subtitle: 'TabBarView example'.tr,
icon: Icons.tab_rounded,
onTap: () => Get.toNamed(Routes.tabBarViewSample),
),
ListItem(
Expand Down
184 changes: 107 additions & 77 deletions example/lib/page/sample/tab_bar_view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class TabBarViewPageState extends State<TabBarViewPage>
header: ClassicHeader(
clamping: true,
position: IndicatorPosition.locator,
mainAxisAlignment: MainAxisAlignment.end,
dragText: 'Pull to refresh'.tr,
armedText: 'Release ready'.tr,
readyText: 'Refreshing...'.tr,
Expand Down Expand Up @@ -88,94 +89,123 @@ class TabBarViewPageState extends State<TabBarViewPage>
});
},
childBuilder: (context, physics) {
return ExtendedNestedScrollView(
physics: physics,
onlyOneScrollInBody: true,
pinnedHeaderSliverHeightBuilder: () {
return MediaQuery.of(context).padding.top + kToolbarHeight;
},
headerSliverBuilder: (context, innerBoxIsScrolled) {
return <Widget>[
const HeaderLocator.sliver(clearExtent: false),
SliverAppBar(
expandedHeight: 166,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text(
'TabBarView',
style: TextStyle(
color: Theme.of(context).textTheme.titleLarge?.color),
return ScrollConfiguration(
behavior: const ERScrollBehavior(),
child: ExtendedNestedScrollView(
physics: physics,
onlyOneScrollInBody: true,
pinnedHeaderSliverHeightBuilder: () {
return MediaQuery.of(context).padding.top + kToolbarHeight;
},
headerSliverBuilder: (context, innerBoxIsScrolled) {
return <Widget>[
const HeaderLocator.sliver(clearExtent: false),
SliverAppBar(
expandedHeight: 120,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text(
'TabBarView',
style: TextStyle(
color:
Theme.of(context).textTheme.titleLarge?.color),
),
centerTitle: false,
),
titlePadding: const EdgeInsets.only(left: 64, bottom: 60),
centerTitle: false,
),
bottom: PreferredSize(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 8,
];
},
body: Column(
children: [
TabBar(
controller: _tabController,
labelColor: themeData.colorScheme.primary,
indicatorColor: themeData.colorScheme.primary,
tabs: const <Widget>[
Tab(
text: 'List',
),
child: TabBar(
controller: _tabController,
labelColor: themeData.colorScheme.primary,
indicatorColor: themeData.colorScheme.primary,
tabs: const <Widget>[
Tab(
text: 'List',
),
Tab(
text: 'Grid',
),
],
Tab(
text: 'Grid',
),
),
preferredSize: const Size(double.infinity, 46),
),
),
];
},
body: TabBarView(
controller: _tabController,
children: <Widget>[
ExtendedVisibilityDetector(
uniqueKey: const Key('Tab0'),
child: CustomScrollView(
physics: physics,
slivers: [
SliverList(
delegate:
SliverChildBuilderDelegate((context, index) {
return const SkeletonItem();
}, childCount: _listCount)),
const FooterLocator.sliver(),
],
),
),
ExtendedVisibilityDetector(
uniqueKey: const Key('Tab1'),
child: CustomScrollView(
physics: physics,
slivers: [
SliverGrid(
delegate:
SliverChildBuilderDelegate((context, index) {
return const SkeletonItem(
direction: Axis.horizontal,
);
}, childCount: _gridCount),
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 6 / 7,
)),
const FooterLocator.sliver(),
],
Expanded(
child: TabBarView(
controller: _tabController,
children: <Widget>[
ExtendedVisibilityDetector(
uniqueKey: const Key('Tab0'),
child: _AutomaticKeepAlive(
child: CustomScrollView(
physics: physics,
slivers: [
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
return const SkeletonItem();
}, childCount: _listCount)),
const FooterLocator.sliver(),
],
),
),
),
ExtendedVisibilityDetector(
uniqueKey: const Key('Tab1'),
child: _AutomaticKeepAlive(
child: CustomScrollView(
physics: physics,
slivers: [
SliverGrid(
delegate: SliverChildBuilderDelegate(
(context, index) {
return const SkeletonItem(
direction: Axis.horizontal,
);
}, childCount: _gridCount),
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 6 / 7,
)),
const FooterLocator.sliver(),
],
),
),
),
],
),
),
),
],
],
),
),
);
},
),
);
}
}

class _AutomaticKeepAlive extends StatefulWidget {
final Widget child;

const _AutomaticKeepAlive({
Key? key,
required this.child,
}) : super(key: key);

@override
State<_AutomaticKeepAlive> createState() => _AutomaticKeepAliveState();
}

class _AutomaticKeepAliveState extends State<_AutomaticKeepAlive>
with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
super.build(context);
return widget.child;
}

@override
bool get wantKeepAlive => true;
}
10 changes: 5 additions & 5 deletions example/lib/widget/skeleton_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class SkeletonItem extends StatelessWidget {
}
return Card(
elevation: 0,
color: Theme.of(context).colorScheme.surfaceVariant,
color: backgroundColor,
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
Expand All @@ -75,7 +75,7 @@ class SkeletonItem extends StatelessWidget {
margin: const EdgeInsets.only(bottom: 16),
height: 80,
width: 80,
color: Theme.of(context).splashColor,
color: foregroundColor,
),
Expanded(
child: Row(
Expand All @@ -88,19 +88,19 @@ class SkeletonItem extends StatelessWidget {
constraints: const BoxConstraints(
maxHeight: 200,
),
color: Theme.of(context).splashColor,
color: foregroundColor,
),
Container(
margin: const EdgeInsets.only(left: 16),
width: 12,
height: 80,
color: Theme.of(context).splashColor,
color: foregroundColor,
),
Container(
margin: const EdgeInsets.only(left: 8),
width: 12,
height: 80,
color: Theme.of(context).splashColor,
color: foregroundColor,
),
],
),
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: EasyRefresh example.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# Read more about versioning at semver.org.
version: 3.0.2+47
version: 3.0.3+48

environment:
sdk: ">=2.13.0 <3.0.0"
Expand All @@ -31,7 +31,7 @@ dependencies:
rive: ^0.9.0
qr_flutter: ^4.0.0
flutter_svg: ^1.1.1+1
flutter_sticky_header: ^0.6.3
flutter_sticky_header: ^0.6.4
easy_refresh:
path: ../
easy_refresh_space:
Expand Down
6 changes: 3 additions & 3 deletions example/windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "generated_plugin_registrant.h"

#include <url_launcher_windows/url_launcher_windows.h>
#include <url_launcher_windows/url_launcher_plugin.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
UrlLauncherWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("UrlLauncherWindows"));
UrlLauncherPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("UrlLauncherPlugin"));
}
5 changes: 3 additions & 2 deletions lib/src/behavior/scroll_behavior.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ part of easy_refresh;

/// Define [ScrollBehavior] in the scope of EasyRefresh.
/// Add support for web and PC.
class _ERScrollBehavior extends ScrollBehavior {
class ERScrollBehavior extends ScrollBehavior {
final ScrollPhysics? _physics;

const _ERScrollBehavior([this._physics]);
const ERScrollBehavior([this._physics]);

@override
ScrollPhysics getScrollPhysics(BuildContext context) {
Expand All @@ -24,5 +24,6 @@ class _ERScrollBehavior extends ScrollBehavior {
PointerDeviceKind.stylus,
PointerDeviceKind.invertedStylus,
PointerDeviceKind.mouse,
PointerDeviceKind.unknown,
};
}
4 changes: 2 additions & 2 deletions lib/src/easy_refresh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,12 @@ class _EasyRefreshState extends State<EasyRefresh>
Widget child;
if (widget.childBuilder != null) {
child = ScrollConfiguration(
behavior: const _ERScrollBehavior(),
behavior: const ERScrollBehavior(),
child: widget.childBuilder!(context, _physics),
);
} else {
child = ScrollConfiguration(
behavior: _ERScrollBehavior(_physics),
behavior: ERScrollBehavior(_physics),
child: widget.child!,
);
}
Expand Down
Loading

0 comments on commit 1478c2c

Please sign in to comment.