可容纳一个Sliver家族的子组件,添加自身内边距来限制孩子组件的占位,核心属性为padding。

    相关组件

    Padding

    SliverPadding基本使用

    【sliver】 : 子组件 【Widget】
    【padding】 : 内边距 【EdgeInsetsGeometry】
    209.gif

    1. import 'package:flutter/material.dart';
    2. class SliverPaddingDemo extends StatelessWidget {
    3. final data = List.generate(128, (i) => Color(0xFF6600FF - 2 * i));
    4. @override
    5. Widget build(BuildContext context) {
    6. return Container(
    7. height: 300,
    8. child: CustomScrollView(
    9. slivers: <Widget>[_buildSliverAppBar(), SliverPadding(
    10. padding: EdgeInsets.only(top: 10),
    11. sliver
    12. : _buildSliverGrid())],
    13. ),
    14. );
    15. }
    16. Widget _buildSliverGrid() => SliverGrid.extent(
    17. childAspectRatio: 1 / 0.618,
    18. maxCrossAxisExtent: 180,
    19. crossAxisSpacing: 5,
    20. mainAxisSpacing: 5,
    21. children: data
    22. .map((e) => Container(
    23. alignment: Alignment.center,
    24. width: 100,
    25. height: 60,
    26. color: e,
    27. child: Text(
    28. colorString(e),
    29. style: TextStyle(color: Colors.white, shadows: [
    30. Shadow(
    31. color: Colors.black,
    32. offset: Offset(.5, .5),
    33. blurRadius: 2)
    34. ]),
    35. ),
    36. ))
    37. .toList(),
    38. );
    39. Widget _buildSliverAppBar() {
    40. return SliverAppBar(
    41. expandedHeight: 190.0,
    42. leading: _buildLeading(),
    43. title: Text('张风捷特烈'),
    44. actions: _buildActions(),
    45. elevation: 5,
    46. pinned: true,
    47. backgroundColor: Colors.orange,
    48. flexibleSpace: FlexibleSpaceBar(
    49. //伸展处布局
    50. titlePadding: EdgeInsets.only(left: 55, bottom: 15), //标题边距
    51. collapseMode: CollapseMode.parallax, //视差效果
    52. background: Image.asset(
    53. "assets/images/caver.jpeg",
    54. fit: BoxFit.cover,
    55. ),
    56. ),
    57. );
    58. }
    59. Widget _buildLeading() => Container(
    60. margin: EdgeInsets.all(10),
    61. child: Image.asset('assets/images/icon_head.png'));
    62. List<Widget> _buildActions() => <Widget>[
    63. IconButton(
    64. onPressed: () {},
    65. icon: Icon(
    66. Icons.star_border,
    67. color: Colors.white,
    68. ),
    69. )
    70. ];
    71. String colorString(Color color) =>
    72. "#${color.value.toRadixString(16).padLeft(8, '0').toUpperCase()}";
    73. }