| 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import 'package:flutter/material.dart'; |
| 6 | |
| 7 | /// Flutter code sample for [CustomScrollView]. |
| 8 | |
| 9 | void main() => runApp(const CustomScrollViewExampleApp()); |
| 10 | |
| 11 | class CustomScrollViewExampleApp extends StatelessWidget { |
| 12 | const CustomScrollViewExampleApp({super.key}); |
| 13 | |
| 14 | @override |
| 15 | Widget build(BuildContext context) { |
| 16 | return const MaterialApp(home: CustomScrollViewExample()); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | class CustomScrollViewExample extends StatefulWidget { |
| 21 | const CustomScrollViewExample({super.key}); |
| 22 | |
| 23 | @override |
| 24 | State<CustomScrollViewExample> createState() => _CustomScrollViewExampleState(); |
| 25 | } |
| 26 | |
| 27 | class _CustomScrollViewExampleState extends State<CustomScrollViewExample> { |
| 28 | List<int> top = <int>[]; |
| 29 | List<int> bottom = <int>[0]; |
| 30 | |
| 31 | @override |
| 32 | Widget build(BuildContext context) { |
| 33 | const Key centerKey = ValueKey<String>('bottom-sliver-list' ); |
| 34 | return Scaffold( |
| 35 | appBar: AppBar( |
| 36 | title: const Text('Press on the plus to add items above and below' ), |
| 37 | leading: IconButton( |
| 38 | icon: const Icon(Icons.add), |
| 39 | onPressed: () { |
| 40 | setState(() { |
| 41 | top.add(-top.length - 1); |
| 42 | bottom.add(bottom.length); |
| 43 | }); |
| 44 | }, |
| 45 | ), |
| 46 | ), |
| 47 | body: CustomScrollView( |
| 48 | center: centerKey, |
| 49 | slivers: <Widget>[ |
| 50 | SliverList( |
| 51 | delegate: SliverChildBuilderDelegate((BuildContext context, int index) { |
| 52 | return Container( |
| 53 | alignment: Alignment.center, |
| 54 | color: Colors.blue[200 + top[index] % 4 * 100], |
| 55 | height: 100 + top[index] % 4 * 20.0, |
| 56 | child: Text('Item: ${top[index]}' ), |
| 57 | ); |
| 58 | }, childCount: top.length), |
| 59 | ), |
| 60 | SliverList( |
| 61 | key: centerKey, |
| 62 | delegate: SliverChildBuilderDelegate((BuildContext context, int index) { |
| 63 | return Container( |
| 64 | alignment: Alignment.center, |
| 65 | color: Colors.blue[200 + bottom[index] % 4 * 100], |
| 66 | height: 100 + bottom[index] % 4 * 20.0, |
| 67 | child: Text('Item: ${bottom[index]}' ), |
| 68 | ); |
| 69 | }, childCount: bottom.length), |
| 70 | ), |
| 71 | ], |
| 72 | ), |
| 73 | ); |
| 74 | } |
| 75 | } |
| 76 | |