Extract Sample code into examples/api (#87280)
This extracts the sample code out from the API doc comments, and places them in separate files on disk, allowing running of the examples locally, testing them, and building of slightly larger examples.
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
// 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.
|
||||
|
||||
// Template: dev/snippets/config/templates/stateless_widget_material.tmpl
|
||||
//
|
||||
// Comment lines marked with "▼▼▼" and "▲▲▲" are used for authoring
|
||||
// of samples, and may be ignored if you are just exploring the sample.
|
||||
|
||||
// Flutter code sample for NestedScrollView
|
||||
//
|
||||
//***************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
// This example shows a [NestedScrollView] whose header is the combination of a
|
||||
// [TabBar] in a [SliverAppBar] and whose body is a [TabBarView]. It uses a
|
||||
// [SliverOverlapAbsorber]/[SliverOverlapInjector] pair to make the inner lists
|
||||
// align correctly, and it uses [SafeArea] to avoid any horizontal disturbances
|
||||
// (e.g. the "notch" on iOS when the phone is horizontal). In addition,
|
||||
// [PageStorageKey]s are used to remember the scroll position of each tab's
|
||||
// list.
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ description ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//***************************************************************************
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const MyApp());
|
||||
|
||||
/// This is the main application widget.
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
static const String _title = 'Flutter Code Sample';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
title: _title,
|
||||
home: MyStatelessWidget(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// This is the stateless widget that the main application instantiates.
|
||||
class MyStatelessWidget extends StatelessWidget {
|
||||
const MyStatelessWidget({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
//********************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
final List<String> _tabs = <String>['Tab 1', 'Tab 2'];
|
||||
return DefaultTabController(
|
||||
length: _tabs.length, // This is the number of tabs.
|
||||
child: Scaffold(
|
||||
body: NestedScrollView(
|
||||
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
|
||||
// These are the slivers that show up in the "outer" scroll view.
|
||||
return <Widget>[
|
||||
SliverOverlapAbsorber(
|
||||
// This widget takes the overlapping behavior of the SliverAppBar,
|
||||
// and redirects it to the SliverOverlapInjector below. If it is
|
||||
// missing, then it is possible for the nested "inner" scroll view
|
||||
// below to end up under the SliverAppBar even when the inner
|
||||
// scroll view thinks it has not been scrolled.
|
||||
// This is not necessary if the "headerSliverBuilder" only builds
|
||||
// widgets that do not overlap the next sliver.
|
||||
handle:
|
||||
NestedScrollView.sliverOverlapAbsorberHandleFor(context),
|
||||
sliver: SliverAppBar(
|
||||
title:
|
||||
const Text('Books'), // This is the title in the app bar.
|
||||
pinned: true,
|
||||
expandedHeight: 150.0,
|
||||
// The "forceElevated" property causes the SliverAppBar to show
|
||||
// a shadow. The "innerBoxIsScrolled" parameter is true when the
|
||||
// inner scroll view is scrolled beyond its "zero" point, i.e.
|
||||
// when it appears to be scrolled below the SliverAppBar.
|
||||
// Without this, there are cases where the shadow would appear
|
||||
// or not appear inappropriately, because the SliverAppBar is
|
||||
// not actually aware of the precise position of the inner
|
||||
// scroll views.
|
||||
forceElevated: innerBoxIsScrolled,
|
||||
bottom: TabBar(
|
||||
// These are the widgets to put in each tab in the tab bar.
|
||||
tabs: _tabs.map((String name) => Tab(text: name)).toList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
];
|
||||
},
|
||||
body: TabBarView(
|
||||
// These are the contents of the tab views, below the tabs.
|
||||
children: _tabs.map((String name) {
|
||||
return SafeArea(
|
||||
top: false,
|
||||
bottom: false,
|
||||
child: Builder(
|
||||
// This Builder is needed to provide a BuildContext that is
|
||||
// "inside" the NestedScrollView, so that
|
||||
// sliverOverlapAbsorberHandleFor() can find the
|
||||
// NestedScrollView.
|
||||
builder: (BuildContext context) {
|
||||
return CustomScrollView(
|
||||
// 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.
|
||||
// The PageStorageKey should be unique to this ScrollView;
|
||||
// it allows the list to remember its scroll position when
|
||||
// the tab view is not on the screen.
|
||||
key: PageStorageKey<String>(name),
|
||||
slivers: <Widget>[
|
||||
SliverOverlapInjector(
|
||||
// This is the flip side of the SliverOverlapAbsorber
|
||||
// above.
|
||||
handle:
|
||||
NestedScrollView.sliverOverlapAbsorberHandleFor(
|
||||
context),
|
||||
),
|
||||
SliverPadding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
// In this example, the inner scroll view has
|
||||
// fixed-height list items, hence the use of
|
||||
// SliverFixedExtentList. However, one could use any
|
||||
// sliver widget here, e.g. SliverList or SliverGrid.
|
||||
sliver: SliverFixedExtentList(
|
||||
// The items in this example are fixed to 48 pixels
|
||||
// high. This matches the Material Design spec for
|
||||
// ListTile widgets.
|
||||
itemExtent: 48.0,
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(BuildContext context, int index) {
|
||||
// This builder is called for each child.
|
||||
// In this example, we just number each list item.
|
||||
return ListTile(
|
||||
title: Text('Item $index'),
|
||||
);
|
||||
},
|
||||
// The childCount of the SliverChildBuilderDelegate
|
||||
// specifies how many children this inner list
|
||||
// has. In this example, each tab has a list of
|
||||
// exactly 30 items, but this is arbitrary.
|
||||
childCount: 30,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//********************************************************************
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
// 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.
|
||||
|
||||
// Template: dev/snippets/config/templates/stateless_widget_material.tmpl
|
||||
//
|
||||
// Comment lines marked with "▼▼▼" and "▲▲▲" are used for authoring
|
||||
// of samples, and may be ignored if you are just exploring the sample.
|
||||
|
||||
// Flutter code sample for NestedScrollView
|
||||
//
|
||||
//***************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
// This simple example shows a [NestedScrollView] whose header contains a
|
||||
// floating [SliverAppBar]. By using the [floatHeaderSlivers] property, the
|
||||
// floating behavior is coordinated between the outer and inner [Scrollable]s,
|
||||
// so it behaves as it would in a single scrollable.
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ description ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//***************************************************************************
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const MyApp());
|
||||
|
||||
/// This is the main application widget.
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
static const String _title = 'Flutter Code Sample';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
title: _title,
|
||||
home: MyStatelessWidget(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// This is the stateless widget that the main application instantiates.
|
||||
class MyStatelessWidget extends StatelessWidget {
|
||||
const MyStatelessWidget({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
//********************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: NestedScrollView(
|
||||
// Setting floatHeaderSlivers to true is required in order to float
|
||||
// the outer slivers over the inner scrollable.
|
||||
floatHeaderSlivers: true,
|
||||
headerSliverBuilder:
|
||||
(BuildContext context, bool innerBoxIsScrolled) {
|
||||
return <Widget>[
|
||||
SliverAppBar(
|
||||
title: const Text('Floating Nested SliverAppBar'),
|
||||
floating: true,
|
||||
expandedHeight: 200.0,
|
||||
forceElevated: innerBoxIsScrolled,
|
||||
),
|
||||
];
|
||||
},
|
||||
body: ListView.builder(
|
||||
padding: const EdgeInsets.all(8),
|
||||
itemCount: 30,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
return SizedBox(
|
||||
height: 50,
|
||||
child: Center(child: Text('Item $index')),
|
||||
);
|
||||
})));
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//********************************************************************
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
// 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.
|
||||
|
||||
// Template: dev/snippets/config/templates/stateless_widget_material.tmpl
|
||||
//
|
||||
// Comment lines marked with "▼▼▼" and "▲▲▲" are used for authoring
|
||||
// of samples, and may be ignored if you are just exploring the sample.
|
||||
|
||||
// Flutter code sample for NestedScrollView
|
||||
//
|
||||
//***************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
// This simple example shows a [NestedScrollView] whose header contains a
|
||||
// snapping, floating [SliverAppBar]. _Without_ setting any additional flags,
|
||||
// e.g [NestedScrollView.floatHeaderSlivers], the [SliverAppBar] will animate
|
||||
// in and out without floating. The [SliverOverlapAbsorber] and
|
||||
// [SliverOverlapInjector] maintain the proper alignment between the two
|
||||
// separate scroll views.
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ description ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//***************************************************************************
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const MyApp());
|
||||
|
||||
/// This is the main application widget.
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
static const String _title = 'Flutter Code Sample';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
title: _title,
|
||||
home: MyStatelessWidget(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// This is the stateless widget that the main application instantiates.
|
||||
class MyStatelessWidget extends StatelessWidget {
|
||||
const MyStatelessWidget({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
//********************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: NestedScrollView(headerSliverBuilder:
|
||||
(BuildContext context, bool innerBoxIsScrolled) {
|
||||
return <Widget>[
|
||||
SliverOverlapAbsorber(
|
||||
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
|
||||
sliver: SliverAppBar(
|
||||
title: const Text('Snapping Nested SliverAppBar'),
|
||||
floating: true,
|
||||
snap: true,
|
||||
expandedHeight: 200.0,
|
||||
forceElevated: innerBoxIsScrolled,
|
||||
),
|
||||
)
|
||||
];
|
||||
}, body: Builder(builder: (BuildContext context) {
|
||||
return CustomScrollView(
|
||||
// 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.
|
||||
slivers: <Widget>[
|
||||
SliverOverlapInjector(
|
||||
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context)),
|
||||
SliverFixedExtentList(
|
||||
itemExtent: 48.0,
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(BuildContext context, int index) =>
|
||||
ListTile(title: Text('Item $index')),
|
||||
childCount: 30,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
})));
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//********************************************************************
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
// 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.
|
||||
|
||||
// Template: dev/snippets/config/templates/stateless_widget_material.tmpl
|
||||
//
|
||||
// Comment lines marked with "▼▼▼" and "▲▲▲" are used for authoring
|
||||
// of samples, and may be ignored if you are just exploring the sample.
|
||||
|
||||
// Flutter code sample for NestedScrollViewState
|
||||
//
|
||||
//***************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
// [NestedScrollViewState] can be obtained using a [GlobalKey].
|
||||
// Using the following setup, you can access the inner scroll controller
|
||||
// using `globalKey.currentState.innerController`.
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ description ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//***************************************************************************
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const MyApp());
|
||||
|
||||
/// This is the main application widget.
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
static const String _title = 'Flutter Code Sample';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
title: _title,
|
||||
home: MyStatelessWidget(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ code-preamble ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
final GlobalKey<NestedScrollViewState> globalKey = GlobalKey();
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code-preamble ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//*****************************************************************************
|
||||
|
||||
/// This is the stateless widget that the main application instantiates.
|
||||
class MyStatelessWidget extends StatelessWidget {
|
||||
const MyStatelessWidget({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
//********************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return NestedScrollView(
|
||||
key: globalKey,
|
||||
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
|
||||
return const <Widget>[
|
||||
SliverAppBar(
|
||||
title: Text('NestedScrollViewState Demo!'),
|
||||
),
|
||||
];
|
||||
},
|
||||
body: const CustomScrollView(
|
||||
// Body slivers go here!
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
ScrollController get innerController {
|
||||
return globalKey.currentState!.innerController;
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//********************************************************************
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user