Skip to content

Commit 1185529

Browse files
authored
Add cupertino examples to the Floating App Bar recipe. (#11821)
_Description of what this PR is changing or adding, and why:_ - Added code sample for a Cupertino floating navigation bar (thanks @loic-sharma!) - Added Cupertino code excerpts. - Updated the Material code example to pin the app bar, but in a minimized style (matches Cupertino example and looks nice, imo) - Added tabs & moved Material and Cupertino specific content into tabs. - In some cases, I reduced text in the intro section to be style agnostic. - Moved some content into an Overview so that we can easily get to that content through the right-pane TOC. Note: It's not easy to tell when content belongs to a tab. We might consider adding some shading back to our tabs. Just a thought for the future. _Issues fixed by this PR (if any):_ _PRs or commits this PR depends on (if any):_ ## Presubmit checklist - [x] This PR is marked as draft with an explanation if not meant to land until a future stable release. - [x] This PR doesn’t contain automatically generated corrections (Grammarly or similar). - [x] This PR follows the [Google Developer Documentation Style Guidelines](https://developers.google.com/style) — for example, it doesn’t use _i.e._ or _e.g._, and it avoids _I_ and _we_ (first person). - [x] This PR uses [semantic line breaks](https://github.com/dart-lang/site-shared/blob/main/doc/writing-for-dart-and-flutter-websites.md#semantic-line-breaks) of 80 characters or fewer.
1 parent 4a41fdc commit 1185529

File tree

8 files changed

+364
-140
lines changed

8 files changed

+364
-140
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'package:flutter/cupertino.dart';
2+
3+
void main() => runApp(const MyApp());
4+
5+
class MyApp extends StatelessWidget {
6+
const MyApp({super.key});
7+
8+
@override
9+
Widget build(BuildContext context) {
10+
const title = 'Floating Navigation Bar';
11+
12+
return CupertinoApp(
13+
title: title,
14+
home: CupertinoPageScaffold(
15+
// No navigation bar provided to CupertinoPageScaffold,
16+
// only a body with a CustomScrollView.
17+
child: CustomScrollView(
18+
slivers: [
19+
// Add the navigation bar to the CustomScrollView.
20+
const CupertinoSliverNavigationBar(
21+
// Provide a standard title.
22+
largeTitle: Text(title),
23+
),
24+
// #docregion SliverList
25+
// Next, create a SliverList
26+
SliverList.builder(
27+
// The builder function returns a CupertinoListTile with a title
28+
// that displays the index of the current item.
29+
itemBuilder:
30+
(context, index) =>
31+
CupertinoListTile(title: Text('Item #$index')),
32+
// Builds 50 CupertinoListTile
33+
itemCount: 50,
34+
),
35+
// #enddocregion SliverList
36+
],
37+
),
38+
),
39+
);
40+
}
41+
}

examples/cookbook/lists/floating_app_bar/lib/main.dart examples/cookbook/lists/floating_app_bar/lib/main_material.dart

+10-13
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,30 @@ class MyApp extends StatelessWidget {
1212
return MaterialApp(
1313
title: title,
1414
home: Scaffold(
15-
// No appbar provided to the Scaffold, only a body with a
15+
// No app bar provided to Scaffold, only a body with a
1616
// CustomScrollView.
1717
body: CustomScrollView(
1818
slivers: [
1919
// Add the app bar to the CustomScrollView.
2020
const SliverAppBar(
2121
// Provide a standard title.
2222
title: Text(title),
23-
// Allows the user to reveal the app bar if they begin scrolling
24-
// back up the list of items.
25-
floating: true,
23+
// Pin the app bar when scrolling
24+
pinned: true,
2625
// Display a placeholder widget to visualize the shrinking size.
2726
flexibleSpace: Placeholder(),
2827
// Make the initial height of the SliverAppBar larger than normal.
2928
expandedHeight: 200,
3029
),
3130
// #docregion SliverList
3231
// Next, create a SliverList
33-
SliverList(
34-
// Use a delegate to build items as they're scrolled on screen.
35-
delegate: SliverChildBuilderDelegate(
36-
// The builder function returns a ListTile with a title that
37-
// displays the index of the current item.
38-
(context, index) => ListTile(title: Text('Item #$index')),
39-
// Builds 1000 ListTiles
40-
childCount: 1000,
41-
),
32+
SliverList.builder(
33+
// The builder function returns a ListTile with a title that
34+
// displays the index of the current item.
35+
itemBuilder:
36+
(context, index) => ListTile(title: Text('Item #$index')),
37+
// Builds 50 ListTiles
38+
itemCount: 50,
4239
),
4340
// #enddocregion SliverList
4441
],

examples/cookbook/lists/floating_app_bar/lib/starter.dart

-18
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'package:flutter/cupertino.dart';
2+
3+
class MyApp extends StatelessWidget {
4+
const MyApp({super.key});
5+
6+
@override
7+
Widget build(BuildContext context) {
8+
// #docregion CustomScrollView
9+
return const CupertinoApp(
10+
title: 'Floating Navigation Bar',
11+
home: CupertinoPageScaffold(
12+
// No navigation bar property provided yet.
13+
child: CustomScrollView(
14+
// Add the navigation bar and list of items as slivers in the next steps.
15+
slivers: <Widget>[],
16+
),
17+
),
18+
);
19+
// #enddocregion CustomScrollView
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'package:flutter/material.dart';
2+
3+
class MyApp extends StatelessWidget {
4+
const MyApp({super.key});
5+
6+
@override
7+
Widget build(BuildContext context) {
8+
// #docregion CustomScrollView
9+
return const MaterialApp(
10+
title: 'Floating App Bar',
11+
home: Scaffold(
12+
// No app bar property provided yet.
13+
body: CustomScrollView(
14+
// Add the app bar and list of items as slivers in the next steps.
15+
slivers: <Widget>[],
16+
),
17+
),
18+
);
19+
// #enddocregion CustomScrollView
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import 'package:flutter/cupertino.dart';
2+
3+
void main() => runApp(const MyApp());
4+
5+
class MyApp extends StatelessWidget {
6+
const MyApp({super.key});
7+
8+
@override
9+
Widget build(BuildContext context) {
10+
return const CupertinoApp(
11+
title: 'Floating App Bar',
12+
home: CupertinoPageScaffold(
13+
// No navigation bar provided to CupertinoPageScaffold,
14+
// only a body with a CustomScrollView.
15+
child: CustomScrollView(
16+
// #docregion SliverAppBar
17+
slivers: [
18+
// Add the navigation bar to the CustomScrollView.
19+
CupertinoSliverNavigationBar(
20+
// Provide a standard title.
21+
largeTitle: Text('Floating App Bar'),
22+
),
23+
],
24+
// #enddocregion SliverAppBar
25+
),
26+
),
27+
);
28+
}
29+
}

examples/cookbook/lists/floating_app_bar/lib/step2.dart examples/cookbook/lists/floating_app_bar/lib/step2_material.dart

+6-9
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,28 @@ class MyApp extends StatelessWidget {
77

88
@override
99
Widget build(BuildContext context) {
10-
const title = 'Floating App Bar';
11-
1210
return const MaterialApp(
13-
title: title,
11+
title: 'Floating App Bar',
1412
home: Scaffold(
1513
// No appbar provided to the Scaffold, only a body with a
1614
// CustomScrollView.
17-
// #docregion SliverAppBar
1815
body: CustomScrollView(
16+
// #docregion SliverAppBar
1917
slivers: [
2018
// Add the app bar to the CustomScrollView.
2119
SliverAppBar(
2220
// Provide a standard title.
23-
title: Text(title),
24-
// Allows the user to reveal the app bar if they begin scrolling
25-
// back up the list of items.
26-
floating: true,
21+
title: Text('Floating App Bar'),
22+
// Pin the app bar when scrolling.
23+
pinned: true,
2724
// Display a placeholder widget to visualize the shrinking size.
2825
flexibleSpace: Placeholder(),
2926
// Make the initial height of the SliverAppBar larger than normal.
3027
expandedHeight: 200,
3128
),
3229
],
30+
// #enddocregion SliverAppBar
3331
),
34-
// #enddocregion SliverAppBar
3532
),
3633
);
3734
}

0 commit comments

Comments
 (0)