flutter3.0学习笔记

SliverPadding

Preview

SliverPadding的工作方式与普通Padding小部件的工作方式相同。唯一的区别是它需要sliver而不是child。总的来说SliverPadding 组件是sliver系列的Padding组件。 构造函数:

 const SliverPadding({
    super.key,
    required this.padding,
    Widget? sliver,
  }) 

例子:

import 'package:flutter/material.dart';

class SliverPaddingPage extends StatelessWidget {
  const SliverPaddingPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('SliverPadding'),),
      body: CustomScrollView(
        slivers: [
          SliverPadding(
            padding: const EdgeInsets.all(20),
            sliver: SliverList(
              delegate: SliverChildBuilderDelegate(
                (context,index){
                  return Container(
                    height: 50,
                    alignment: Alignment.center,
                    color: Colors.primaries[index % Colors.primaries.length],
                    child: Text('List item : $index'),
                  );
                },
                childCount: 10
              ),
            ),
          )
        ],
      ),
    );
  }
}

Simulator Screen Shot - iPhone 14 Pro Max - 2022-11-08 at 15.24.19.png