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:
172
examples/api/lib/material/list_tile/list_tile.4.dart
Normal file
172
examples/api/lib/material/list_tile/list_tile.4.dart
Normal file
@@ -0,0 +1,172 @@
|
||||
// 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_scaffold.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 ListTile
|
||||
//
|
||||
//***************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
// Here is an example of a custom list item that resembles a YouTube-related
|
||||
// video list item created with [Expanded] and [Container] widgets.
|
||||
//
|
||||
// 
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ 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 MaterialApp(
|
||||
title: _title,
|
||||
home: Scaffold(
|
||||
appBar: AppBar(title: const Text(_title)),
|
||||
body: const MyStatelessWidget(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ code-preamble ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
class CustomListItem extends StatelessWidget {
|
||||
const CustomListItem({
|
||||
Key? key,
|
||||
required this.thumbnail,
|
||||
required this.title,
|
||||
required this.user,
|
||||
required this.viewCount,
|
||||
}) : super(key: key);
|
||||
|
||||
final Widget thumbnail;
|
||||
final String title;
|
||||
final String user;
|
||||
final int viewCount;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 5.0),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: thumbnail,
|
||||
),
|
||||
Expanded(
|
||||
flex: 3,
|
||||
child: _VideoDescription(
|
||||
title: title,
|
||||
user: user,
|
||||
viewCount: viewCount,
|
||||
),
|
||||
),
|
||||
const Icon(
|
||||
Icons.more_vert,
|
||||
size: 16.0,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _VideoDescription extends StatelessWidget {
|
||||
const _VideoDescription({
|
||||
Key? key,
|
||||
required this.title,
|
||||
required this.user,
|
||||
required this.viewCount,
|
||||
}) : super(key: key);
|
||||
|
||||
final String title;
|
||||
final String user;
|
||||
final int viewCount;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(5.0, 0.0, 0.0, 0.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Text(
|
||||
title,
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.w500,
|
||||
fontSize: 14.0,
|
||||
),
|
||||
),
|
||||
const Padding(padding: EdgeInsets.symmetric(vertical: 2.0)),
|
||||
Text(
|
||||
user,
|
||||
style: const TextStyle(fontSize: 10.0),
|
||||
),
|
||||
const Padding(padding: EdgeInsets.symmetric(vertical: 1.0)),
|
||||
Text(
|
||||
'$viewCount views',
|
||||
style: const TextStyle(fontSize: 10.0),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ 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)
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return ListView(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
itemExtent: 106.0,
|
||||
children: <CustomListItem>[
|
||||
CustomListItem(
|
||||
user: 'Flutter',
|
||||
viewCount: 999000,
|
||||
thumbnail: Container(
|
||||
decoration: const BoxDecoration(color: Colors.blue),
|
||||
),
|
||||
title: 'The Flutter YouTube Channel',
|
||||
),
|
||||
CustomListItem(
|
||||
user: 'Dash',
|
||||
viewCount: 884000,
|
||||
thumbnail: Container(
|
||||
decoration: const BoxDecoration(color: Colors.yellow),
|
||||
),
|
||||
title: 'Announcing Flutter 1.0',
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//********************************************************************
|
||||
|
||||
}
|
||||
217
examples/api/lib/material/list_tile/list_tile.5.dart
Normal file
217
examples/api/lib/material/list_tile/list_tile.5.dart
Normal file
@@ -0,0 +1,217 @@
|
||||
// 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_scaffold.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 ListTile
|
||||
//
|
||||
//***************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
// Here is an example of an article list item with multiline titles and
|
||||
// subtitles. It utilizes [Row]s and [Column]s, as well as [Expanded] and
|
||||
// [AspectRatio] widgets to organize its layout.
|
||||
//
|
||||
// 
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ 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 MaterialApp(
|
||||
title: _title,
|
||||
home: Scaffold(
|
||||
appBar: AppBar(title: const Text(_title)),
|
||||
body: const MyStatelessWidget(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ code-preamble ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
class _ArticleDescription extends StatelessWidget {
|
||||
const _ArticleDescription({
|
||||
Key? key,
|
||||
required this.title,
|
||||
required this.subtitle,
|
||||
required this.author,
|
||||
required this.publishDate,
|
||||
required this.readDuration,
|
||||
}) : super(key: key);
|
||||
|
||||
final String title;
|
||||
final String subtitle;
|
||||
final String author;
|
||||
final String publishDate;
|
||||
final String readDuration;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Text(
|
||||
title,
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const Padding(padding: EdgeInsets.only(bottom: 2.0)),
|
||||
Text(
|
||||
subtitle,
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontSize: 12.0,
|
||||
color: Colors.black54,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: <Widget>[
|
||||
Text(
|
||||
author,
|
||||
style: const TextStyle(
|
||||
fontSize: 12.0,
|
||||
color: Colors.black87,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'$publishDate - $readDuration',
|
||||
style: const TextStyle(
|
||||
fontSize: 12.0,
|
||||
color: Colors.black54,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CustomListItemTwo extends StatelessWidget {
|
||||
const CustomListItemTwo({
|
||||
Key? key,
|
||||
required this.thumbnail,
|
||||
required this.title,
|
||||
required this.subtitle,
|
||||
required this.author,
|
||||
required this.publishDate,
|
||||
required this.readDuration,
|
||||
}) : super(key: key);
|
||||
|
||||
final Widget thumbnail;
|
||||
final String title;
|
||||
final String subtitle;
|
||||
final String author;
|
||||
final String publishDate;
|
||||
final String readDuration;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 10.0),
|
||||
child: SizedBox(
|
||||
height: 100,
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
AspectRatio(
|
||||
aspectRatio: 1.0,
|
||||
child: thumbnail,
|
||||
),
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(20.0, 0.0, 2.0, 0.0),
|
||||
child: _ArticleDescription(
|
||||
title: title,
|
||||
subtitle: subtitle,
|
||||
author: author,
|
||||
publishDate: publishDate,
|
||||
readDuration: readDuration,
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ 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)
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return ListView(
|
||||
padding: const EdgeInsets.all(10.0),
|
||||
children: <Widget>[
|
||||
CustomListItemTwo(
|
||||
thumbnail: Container(
|
||||
decoration: const BoxDecoration(color: Colors.pink),
|
||||
),
|
||||
title: 'Flutter 1.0 Launch',
|
||||
subtitle: 'Flutter continues to improve and expand its horizons. '
|
||||
'This text should max out at two lines and clip',
|
||||
author: 'Dash',
|
||||
publishDate: 'Dec 28',
|
||||
readDuration: '5 mins',
|
||||
),
|
||||
CustomListItemTwo(
|
||||
thumbnail: Container(
|
||||
decoration: const BoxDecoration(color: Colors.blue),
|
||||
),
|
||||
title: 'Flutter 1.2 Release - Continual updates to the framework',
|
||||
subtitle: 'Flutter once again improves and makes updates.',
|
||||
author: 'Flutter',
|
||||
publishDate: 'Feb 26',
|
||||
readDuration: '12 mins',
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//********************************************************************
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
// 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/stateful_widget_scaffold.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 ListTile.selected
|
||||
//
|
||||
//***************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
// Here is an example of using a [StatefulWidget] to keep track of the
|
||||
// selected index, and using that to set the `selected` property on the
|
||||
// corresponding [ListTile].
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ 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 MaterialApp(
|
||||
title: _title,
|
||||
home: Scaffold(
|
||||
appBar: AppBar(title: const Text(_title)),
|
||||
body: const MyStatefulWidget(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// This is the stateful widget that the main application instantiates.
|
||||
class MyStatefulWidget extends StatefulWidget {
|
||||
const MyStatefulWidget({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
|
||||
}
|
||||
|
||||
/// This is the private State class that goes with MyStatefulWidget.
|
||||
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
|
||||
//********************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
int _selectedIndex = 0;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView.builder(
|
||||
itemCount: 10,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
return ListTile(
|
||||
title: Text('Item $index'),
|
||||
selected: index == _selectedIndex,
|
||||
onTap: () {
|
||||
setState(() {
|
||||
_selectedIndex = index;
|
||||
});
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//********************************************************************
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user