nested_scroll_view.2.dart 1.97 KB
Newer Older
1 2 3 4 5 6
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

7
/// Flutter code sample for [NestedScrollView].
8

9
void main() => runApp(const NestedScrollViewExampleApp());
10

11 12
class NestedScrollViewExampleApp extends StatelessWidget {
  const NestedScrollViewExampleApp({super.key});
13 14 15 16

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
17
      home: NestedScrollViewExample(),
18 19 20 21
    );
  }
}

22 23
class NestedScrollViewExample extends StatelessWidget {
  const NestedScrollViewExample({super.key});
24 25 26 27

  @override
  Widget build(BuildContext context) {
    return Scaffold(
28
        body: NestedScrollView(headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
29 30 31 32 33 34 35 36 37 38
      return <Widget>[
        SliverOverlapAbsorber(
          handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
          sliver: SliverAppBar(
            title: const Text('Snapping Nested SliverAppBar'),
            floating: true,
            snap: true,
            expandedHeight: 200.0,
            forceElevated: innerBoxIsScrolled,
          ),
39
        ),
40 41 42
      ];
    }, body: Builder(builder: (BuildContext context) {
      return CustomScrollView(
43 44 45 46
        // The "controller" and "primary" members should be left unset, so that
        // the NestedScrollView can control this inner scroll view.
        // If the "controller" property is set, then this scroll view will not
        // be associated with the NestedScrollView.
47
        slivers: <Widget>[
48
          SliverOverlapInjector(handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context)),
49 50 51
          SliverFixedExtentList(
            itemExtent: 48.0,
            delegate: SliverChildBuilderDelegate(
52
              (BuildContext context, int index) => ListTile(title: Text('Item $index')),
53 54 55 56 57 58 59 60
              childCount: 30,
            ),
          ),
        ],
      );
    })));
  }
}