• Flutter SliverAppBar 吸顶效果


    吸顶是常见的布局,主要使用的是CustomScrollView 和SliverApp组件实现的

    页面布局

    1. @override
    2. Widget build(BuildContext context) {
    3. return CustomScrollView(
    4. controller: controller.scrollController!,
    5. physics: const BouncingScrollPhysics(),
    6. slivers: [
    7. SliverAppBar(
    8. backgroundColor: Colors.blue,
    9. iconTheme: const IconThemeData(color: Colors.white),
    10. expandedHeight: controller.kExpandedHeight,
    11. floating: false,
    12. pinned: true,
    13. stretch: false,
    14. snap: false,
    15. stretchTriggerOffset: ScreenHelper.height(100),
    16. onStretchTrigger: () async {
    17. return;
    18. },
    19. flexibleSpace: FlexibleSpaceBar(
    20. centerTitle: true,
    21. title: Obx(() => Text(
    22. controller.isAppBArPinned.value ? "体重检测" : "",
    23. style: TextStyle(
    24. color: Colors.white, fontSize: ScreenHelper.sp(16)),
    25. )),
    26. collapseMode: CollapseMode.parallax,
    27. stretchModes: [
    28. StretchMode.zoomBackground,
    29. StretchMode.fadeTitle,
    30. StretchMode.blurBackground,
    31. ],
    32. background: Container(
    33. color: Colors.blue,
    34. child: Column(
    35. mainAxisAlignment: MainAxisAlignment.start,
    36. children: [
    37. SizedBox(height: ScreenHelper.topSafeHeight + 18),
    38. Text(
    39. "2021年12月30日 13:00",
    40. style: TextStyle(color: Colors.white.withOpacity(.8)),
    41. ),
    42. SizedBox(height: ScreenHelper.height(30)),
    43. Row(
    44. mainAxisAlignment: MainAxisAlignment.center,
    45. crossAxisAlignment: CrossAxisAlignment.end,
    46. children: [
    47. Text(
    48. "52.0",
    49. style: TextStyle(
    50. color: Colors.white,
    51. fontSize: ScreenHelper.sp(40),
    52. fontWeight: FontWeight.bold),
    53. ),
    54. Text(
    55. "kg",
    56. style: TextStyle(
    57. color: Colors.white, fontSize: ScreenHelper.sp(13)),
    58. )
    59. ],
    60. ),
    61. SizedBox(
    62. height: ScreenHelper.height(5),
    63. ),
    64. Text(
    65. "BMI 20.7 标准",
    66. style: TextStyle(
    67. color: Colors.white.withOpacity(.8),
    68. fontSize: ScreenHelper.sp(14)),
    69. ),
    70. SizedBox(
    71. height: ScreenHelper.height(15),
    72. ),
    73. Container(
    74. padding: EdgeInsets.symmetric(horizontal: 6, vertical: 4),
    75. decoration: BoxDecoration(
    76. color: Colors.yellow,
    77. borderRadius: BorderRadius.circular(50)),
    78. child: Text(
    79. "记录体重",
    80. style: TextStyle(
    81. color: Colors.white, fontSize: ScreenHelper.sp(14)),
    82. ),
    83. )
    84. ],
    85. ),
    86. ),
    87. ),
    88. ),
    89. SliverFixedExtentList(
    90. delegate: SliverChildBuilderDelegate(
    91. (context, index) => ListTile(
    92. title: Text(
    93. "测试",
    94. style: TextStyle(color: AppTheme.dartTextColor),
    95. ),
    96. ),
    97. childCount: 30),
    98. itemExtent: 50.0,
    99. ),
    100. ],
    101. );
    102. }

    因为使用的getx状态管理,所以数据的操作都在getx里

    GetxController 

    1. ScrollController? scrollController;
    2. final isAppBArPinned = false.obs; //判断APPbar是否吸顶
    3. double kExpandedHeight = ScreenHelper.width(240);
    4. void onInit() {
    5. // TODO: implement onInit
    6. scrollController = ScrollController()..addListener(_onScroll);
    7. super.onInit();
    8. }
    9. void _onScroll() {
    10. if (scrollController!.hasClients &&
    11. scrollController!.offset > kExpandedHeight - kToolbarHeight) {
    12. isAppBArPinned.value = true;
    13. } else {
    14. isAppBArPinned.value = false;
    15. }
    16. }
    17. @override
    18. void onClose() {
    19. // TODO: implement onClose
    20. super.onClose();
    21. scrollController!.dispose();
    22. }

    最后呈现的效果

    吸顶

  • 相关阅读:
    vue 改变路由(URL)参数不刷新页面
    某平台简单尝试一次密码逆向
    【实训项目】“优品果园”-线上水果商城小程序
    少年,你可听说过MVCC?
    单例设计模式
    「Python条件结构」if…elif…else结构根据输入x的值求y的值(1)
    HarmonyOS(鸿蒙)不再适合JS语言开发
    新手编码指北(持续更新...)
    STM32 GPIO模拟UART串口:最简延时方式
    Spring Data Commons远程命令执行漏洞复现(CVE-2018-1273)
  • 原文地址:https://blog.csdn.net/zww986736788/article/details/133745130