From f2620ed22bd5979ad42bcf54da403cdd5ecc074b Mon Sep 17 00:00:00 2001 From: PH2 <1293456824@qq.com> Date: Fri, 25 Sep 2020 20:27:09 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=B7=BB=E5=8A=A0=E9=AA=A8=E6=9E=B6?= =?UTF-8?q?=E5=9B=BE=E5=B7=A5=E5=85=B7=E7=B1=BB=202=E3=80=81=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0sliver=20delegate=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/models/base/widget_model.dart | 2 +- ...tom_sliver_persistent_header_delegate.dart | 32 +++++++++++++++++++ lib/util/shimmer_util.dart | 16 ++++++++++ lib/util/sliver_tab_bar_delegate.dart | 23 +++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 lib/util/custom_sliver_persistent_header_delegate.dart create mode 100644 lib/util/shimmer_util.dart create mode 100644 lib/util/sliver_tab_bar_delegate.dart diff --git a/lib/models/base/widget_model.dart b/lib/models/base/widget_model.dart index 6f806dc..8b12f3a 100644 --- a/lib/models/base/widget_model.dart +++ b/lib/models/base/widget_model.dart @@ -45,7 +45,7 @@ class WidgetModel { this.isGlobal}); WidgetModel.fromJson(Map json) { - modId = json['mod_id']; + modId = json['mod_id'] is int ? json['mod_id'] as int : int.parse(json['mod_id']?.toString()); modPid = json['mod_pid']; modName = json['mod_name']; position = json['position']; diff --git a/lib/util/custom_sliver_persistent_header_delegate.dart b/lib/util/custom_sliver_persistent_header_delegate.dart new file mode 100644 index 0000000..8aaebf3 --- /dev/null +++ b/lib/util/custom_sliver_persistent_header_delegate.dart @@ -0,0 +1,32 @@ +// 自定义 SliverPersistentHeaderDelegate +import 'package:flutter/material.dart'; + +class CustomSliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate { + final double max; // 最大高度 + final double min; // 最小高度 + final Widget child; // 需要展示的内容 + + CustomSliverPersistentHeaderDelegate({@required this.max, @required this.min, @required this.child}) + // 如果 assert 内部条件不成立,会报错 + : assert(max != null), + assert(min != null), + assert(child != null), + assert(min <= max), + super(); + + // 返回展示的内容,如果内容固定可以直接在这定义,如果需要可扩展,这边通过传入值来定义 + @override + Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) => child; + + @override + double get maxExtent => max; // 返回最大高度 + + @override + double get minExtent => min; // 返回最小高度 + + @override + bool shouldRebuild(CustomSliverPersistentHeaderDelegate oldDelegate) { + // 是否需要更新,这里我们定义当高度范围和展示内容被替换的时候进行刷新界面 + return max != oldDelegate.max || min != oldDelegate.min || child != oldDelegate.child; + } +} diff --git a/lib/util/shimmer_util.dart b/lib/util/shimmer_util.dart new file mode 100644 index 0000000..fe548c2 --- /dev/null +++ b/lib/util/shimmer_util.dart @@ -0,0 +1,16 @@ +import 'package:flutter/material.dart'; +import 'package:shimmer/shimmer.dart'; + +class ShimmerUtil { + static Widget getShimmerWidget({double width, double height, double radius = 0}) { + return Shimmer.fromColors( + baseColor: Colors.grey[300], + highlightColor: Colors.grey[100], + child: Container( + width: width, + height: height, + decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(radius)), + ), + ); + } +} diff --git a/lib/util/sliver_tab_bar_delegate.dart b/lib/util/sliver_tab_bar_delegate.dart new file mode 100644 index 0000000..874e76d --- /dev/null +++ b/lib/util/sliver_tab_bar_delegate.dart @@ -0,0 +1,23 @@ +import 'package:flutter/material.dart'; + +class SliverTabBarDelegate extends SliverPersistentHeaderDelegate { + final TabBar widget; + + const SliverTabBarDelegate(this.widget) : assert(widget != null); + + @override + Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) { + return this.widget; + } + + @override + bool shouldRebuild(SliverTabBarDelegate oldDelegate) { + return false; + } + + @override + double get maxExtent => widget.preferredSize.height; + + @override + double get minExtent => widget.preferredSize.height; +}