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:
122
examples/api/lib/widgets/async/future_builder.0.dart
Normal file
122
examples/api/lib/widgets/async/future_builder.0.dart
Normal file
@@ -0,0 +1,122 @@
|
||||
// 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_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 FutureBuilder
|
||||
//
|
||||
//***************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
// This sample shows a [FutureBuilder] that displays a loading spinner while it
|
||||
// loads data. It displays a success icon and text if the [Future] completes
|
||||
// with a result, or an error icon and text if the [Future] completes with an
|
||||
// error. Assume the `_calculation` field is set by pressing a button elsewhere
|
||||
// in the UI.
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ 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: 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)
|
||||
|
||||
final Future<String> _calculation = Future<String>.delayed(
|
||||
const Duration(seconds: 2),
|
||||
() => 'Data Loaded',
|
||||
);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return DefaultTextStyle(
|
||||
style: Theme.of(context).textTheme.headline2!,
|
||||
textAlign: TextAlign.center,
|
||||
child: FutureBuilder<String>(
|
||||
future: _calculation, // a previously-obtained Future<String> or null
|
||||
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
|
||||
List<Widget> children;
|
||||
if (snapshot.hasData) {
|
||||
children = <Widget>[
|
||||
const Icon(
|
||||
Icons.check_circle_outline,
|
||||
color: Colors.green,
|
||||
size: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: Text('Result: ${snapshot.data}'),
|
||||
)
|
||||
];
|
||||
} else if (snapshot.hasError) {
|
||||
children = <Widget>[
|
||||
const Icon(
|
||||
Icons.error_outline,
|
||||
color: Colors.red,
|
||||
size: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: Text('Error: ${snapshot.error}'),
|
||||
)
|
||||
];
|
||||
} else {
|
||||
children = const <Widget>[
|
||||
SizedBox(
|
||||
child: CircularProgressIndicator(),
|
||||
width: 60,
|
||||
height: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(top: 16),
|
||||
child: Text('Awaiting result...'),
|
||||
)
|
||||
];
|
||||
}
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: children,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//********************************************************************
|
||||
|
||||
}
|
||||
161
examples/api/lib/widgets/async/stream_builder.0.dart
Normal file
161
examples/api/lib/widgets/async/stream_builder.0.dart
Normal file
@@ -0,0 +1,161 @@
|
||||
// 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_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 StreamBuilder
|
||||
//
|
||||
//***************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
// This sample shows a [StreamBuilder] that listens to a Stream that emits bids
|
||||
// for an auction. Every time the StreamBuilder receives a bid from the Stream,
|
||||
// it will display the price of the bid below an icon. If the Stream emits an
|
||||
// error, the error is displayed below an error icon. When the Stream finishes
|
||||
// emitting bids, the final price is displayed.
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ 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: 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)
|
||||
|
||||
final Stream<int> _bids = (() async* {
|
||||
await Future<void>.delayed(const Duration(seconds: 1));
|
||||
yield 1;
|
||||
await Future<void>.delayed(const Duration(seconds: 1));
|
||||
})();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return DefaultTextStyle(
|
||||
style: Theme.of(context).textTheme.headline2!,
|
||||
textAlign: TextAlign.center,
|
||||
child: Container(
|
||||
alignment: FractionalOffset.center,
|
||||
color: Colors.white,
|
||||
child: StreamBuilder<int>(
|
||||
stream: _bids,
|
||||
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
|
||||
List<Widget> children;
|
||||
if (snapshot.hasError) {
|
||||
children = <Widget>[
|
||||
const Icon(
|
||||
Icons.error_outline,
|
||||
color: Colors.red,
|
||||
size: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: Text('Error: ${snapshot.error}'),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text('Stack trace: ${snapshot.stackTrace}'),
|
||||
),
|
||||
];
|
||||
} else {
|
||||
switch (snapshot.connectionState) {
|
||||
case ConnectionState.none:
|
||||
children = const <Widget>[
|
||||
Icon(
|
||||
Icons.info,
|
||||
color: Colors.blue,
|
||||
size: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(top: 16),
|
||||
child: Text('Select a lot'),
|
||||
)
|
||||
];
|
||||
break;
|
||||
case ConnectionState.waiting:
|
||||
children = const <Widget>[
|
||||
SizedBox(
|
||||
child: CircularProgressIndicator(),
|
||||
width: 60,
|
||||
height: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(top: 16),
|
||||
child: Text('Awaiting bids...'),
|
||||
)
|
||||
];
|
||||
break;
|
||||
case ConnectionState.active:
|
||||
children = <Widget>[
|
||||
const Icon(
|
||||
Icons.check_circle_outline,
|
||||
color: Colors.green,
|
||||
size: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: Text('\$${snapshot.data}'),
|
||||
)
|
||||
];
|
||||
break;
|
||||
case ConnectionState.done:
|
||||
children = <Widget>[
|
||||
const Icon(
|
||||
Icons.info,
|
||||
color: Colors.blue,
|
||||
size: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: Text('\$${snapshot.data} (closed)'),
|
||||
)
|
||||
];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: children,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//********************************************************************
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user