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,73 @@
|
||||
// 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 InteractiveViewer
|
||||
//
|
||||
//***************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
// This example shows a simple Container that can be panned and zoomed.
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ 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(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 Center(
|
||||
child: InteractiveViewer(
|
||||
boundaryMargin: const EdgeInsets.all(20.0),
|
||||
minScale: 0.1,
|
||||
maxScale: 1.6,
|
||||
child: Container(
|
||||
decoration: const BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: <Color>[Colors.orange, Colors.red],
|
||||
stops: <double>[0.0, 1.0],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//********************************************************************
|
||||
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
// 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/freeform.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 InteractiveViewer.builder
|
||||
//
|
||||
//***************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
// This example shows how to use builder to create a [Table] whose cell
|
||||
// contents are only built when they are visible. Built and remove cells are
|
||||
// logged in the console for illustration.
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ description ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//***************************************************************************
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
//*************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ code-main ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
import 'package:vector_math/vector_math_64.dart' show Quad, Vector3;
|
||||
|
||||
void main() => runApp(const IVBuilderExampleApp());
|
||||
|
||||
class IVBuilderExampleApp extends StatelessWidget {
|
||||
const IVBuilderExampleApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('IV Builder Example'),
|
||||
),
|
||||
body: _IVBuilderExample(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _IVBuilderExample extends StatefulWidget {
|
||||
@override
|
||||
_IVBuilderExampleState createState() => _IVBuilderExampleState();
|
||||
}
|
||||
|
||||
class _IVBuilderExampleState extends State<_IVBuilderExample> {
|
||||
final TransformationController _transformationController =
|
||||
TransformationController();
|
||||
|
||||
static const double _cellWidth = 200.0;
|
||||
static const double _cellHeight = 26.0;
|
||||
|
||||
// Returns true iff the given cell is currently visible. Caches viewport
|
||||
// calculations.
|
||||
late Quad _cachedViewport;
|
||||
late int _firstVisibleRow;
|
||||
late int _firstVisibleColumn;
|
||||
late int _lastVisibleRow;
|
||||
late int _lastVisibleColumn;
|
||||
bool _isCellVisible(int row, int column, Quad viewport) {
|
||||
if (viewport != _cachedViewport) {
|
||||
final Rect aabb = _axisAlignedBoundingBox(viewport);
|
||||
_cachedViewport = viewport;
|
||||
_firstVisibleRow = (aabb.top / _cellHeight).floor();
|
||||
_firstVisibleColumn = (aabb.left / _cellWidth).floor();
|
||||
_lastVisibleRow = (aabb.bottom / _cellHeight).floor();
|
||||
_lastVisibleColumn = (aabb.right / _cellWidth).floor();
|
||||
}
|
||||
return row >= _firstVisibleRow &&
|
||||
row <= _lastVisibleRow &&
|
||||
column >= _firstVisibleColumn &&
|
||||
column <= _lastVisibleColumn;
|
||||
}
|
||||
|
||||
// Returns the axis aligned bounding box for the given Quad, which might not
|
||||
// be axis aligned.
|
||||
Rect _axisAlignedBoundingBox(Quad quad) {
|
||||
double? xMin;
|
||||
double? xMax;
|
||||
double? yMin;
|
||||
double? yMax;
|
||||
for (final Vector3 point in <Vector3>[
|
||||
quad.point0,
|
||||
quad.point1,
|
||||
quad.point2,
|
||||
quad.point3
|
||||
]) {
|
||||
if (xMin == null || point.x < xMin) {
|
||||
xMin = point.x;
|
||||
}
|
||||
if (xMax == null || point.x > xMax) {
|
||||
xMax = point.x;
|
||||
}
|
||||
if (yMin == null || point.y < yMin) {
|
||||
yMin = point.y;
|
||||
}
|
||||
if (yMax == null || point.y > yMax) {
|
||||
yMax = point.y;
|
||||
}
|
||||
}
|
||||
return Rect.fromLTRB(xMin!, yMin!, xMax!, yMax!);
|
||||
}
|
||||
|
||||
void _onChangeTransformation() {
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_transformationController.addListener(_onChangeTransformation);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_transformationController.removeListener(_onChangeTransformation);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: LayoutBuilder(
|
||||
builder: (BuildContext context, BoxConstraints constraints) {
|
||||
return InteractiveViewer.builder(
|
||||
alignPanAxis: true,
|
||||
scaleEnabled: false,
|
||||
transformationController: _transformationController,
|
||||
builder: (BuildContext context, Quad viewport) {
|
||||
// A simple extension of Table that builds cells.
|
||||
return _TableBuilder(
|
||||
rowCount: 60,
|
||||
columnCount: 6,
|
||||
cellWidth: _cellWidth,
|
||||
builder: (BuildContext context, int row, int column) {
|
||||
if (!_isCellVisible(row, column, viewport)) {
|
||||
print('removing cell ($row, $column)');
|
||||
return Container(height: _cellHeight);
|
||||
}
|
||||
print('building cell ($row, $column)');
|
||||
return Container(
|
||||
height: _cellHeight,
|
||||
color: row % 2 + column % 2 == 1
|
||||
? Colors.white
|
||||
: Colors.grey.withOpacity(0.1),
|
||||
child: Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: Text('$row x $column'),
|
||||
),
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef _CellBuilder = Widget Function(
|
||||
BuildContext context, int row, int column);
|
||||
|
||||
class _TableBuilder extends StatelessWidget {
|
||||
const _TableBuilder({
|
||||
required this.rowCount,
|
||||
required this.columnCount,
|
||||
required this.cellWidth,
|
||||
required this.builder,
|
||||
}) : assert(rowCount > 0),
|
||||
assert(columnCount > 0);
|
||||
|
||||
final int rowCount;
|
||||
final int columnCount;
|
||||
final double cellWidth;
|
||||
final _CellBuilder builder;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Table(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
columnWidths: <int, TableColumnWidth>{
|
||||
for (int column = 0; column < columnCount; column++)
|
||||
column: FixedColumnWidth(cellWidth),
|
||||
},
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: <TableRow>[
|
||||
for (int row = 0; row < rowCount; row++)
|
||||
// ignore: prefer_const_constructors
|
||||
TableRow(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: <Widget>[
|
||||
for (int column = 0; column < columnCount; column++)
|
||||
builder(context, row, column),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code-main ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//*************************************************************************
|
||||
@@ -0,0 +1,91 @@
|
||||
// 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 InteractiveViewer.constrained
|
||||
//
|
||||
//***************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
// This example shows how to create a pannable table. Because the table is
|
||||
// larger than the entire screen, setting `constrained` to false is necessary
|
||||
// to allow it to be drawn to its full size. The parts of the table that
|
||||
// exceed the screen size can then be panned into view.
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ 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(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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) {
|
||||
const int _rowCount = 48;
|
||||
const int _columnCount = 6;
|
||||
|
||||
return InteractiveViewer(
|
||||
alignPanAxis: true,
|
||||
constrained: false,
|
||||
scaleEnabled: false,
|
||||
child: Table(
|
||||
columnWidths: <int, TableColumnWidth>{
|
||||
for (int column = 0; column < _columnCount; column += 1)
|
||||
column: const FixedColumnWidth(200.0),
|
||||
},
|
||||
children: <TableRow>[
|
||||
for (int row = 0; row < _rowCount; row += 1)
|
||||
TableRow(
|
||||
children: <Widget>[
|
||||
for (int column = 0; column < _columnCount; column += 1)
|
||||
Container(
|
||||
height: 26,
|
||||
color: row % 2 + column % 2 == 1
|
||||
? Colors.white
|
||||
: Colors.grey.withOpacity(0.1),
|
||||
child: Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: Text('$row x $column'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//********************************************************************
|
||||
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
// 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_ticker.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 InteractiveViewer.transformationController
|
||||
//
|
||||
//***************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
// This example shows how transformationController can be used to animate the
|
||||
// transformation back to its starting position.
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ 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.
|
||||
/// AnimationControllers can be created with `vsync: this` because of TickerProviderStateMixin.
|
||||
class _MyStatefulWidgetState extends State<MyStatefulWidget>
|
||||
with TickerProviderStateMixin {
|
||||
//********************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
final TransformationController _transformationController =
|
||||
TransformationController();
|
||||
Animation<Matrix4>? _animationReset;
|
||||
late final AnimationController _controllerReset;
|
||||
|
||||
void _onAnimateReset() {
|
||||
_transformationController.value = _animationReset!.value;
|
||||
if (!_controllerReset.isAnimating) {
|
||||
_animationReset!.removeListener(_onAnimateReset);
|
||||
_animationReset = null;
|
||||
_controllerReset.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void _animateResetInitialize() {
|
||||
_controllerReset.reset();
|
||||
_animationReset = Matrix4Tween(
|
||||
begin: _transformationController.value,
|
||||
end: Matrix4.identity(),
|
||||
).animate(_controllerReset);
|
||||
_animationReset!.addListener(_onAnimateReset);
|
||||
_controllerReset.forward();
|
||||
}
|
||||
|
||||
// Stop a running reset to home transform animation.
|
||||
void _animateResetStop() {
|
||||
_controllerReset.stop();
|
||||
_animationReset?.removeListener(_onAnimateReset);
|
||||
_animationReset = null;
|
||||
_controllerReset.reset();
|
||||
}
|
||||
|
||||
void _onInteractionStart(ScaleStartDetails details) {
|
||||
// If the user tries to cause a transformation while the reset animation is
|
||||
// running, cancel the reset animation.
|
||||
if (_controllerReset.status == AnimationStatus.forward) {
|
||||
_animateResetStop();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controllerReset = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(milliseconds: 400),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controllerReset.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
appBar: AppBar(
|
||||
automaticallyImplyLeading: false,
|
||||
title: const Text('Controller demo'),
|
||||
),
|
||||
body: Center(
|
||||
child: InteractiveViewer(
|
||||
boundaryMargin: const EdgeInsets.all(double.infinity),
|
||||
transformationController: _transformationController,
|
||||
minScale: 0.1,
|
||||
maxScale: 1.0,
|
||||
onInteractionStart: _onInteractionStart,
|
||||
child: Container(
|
||||
decoration: const BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: <Color>[Colors.orange, Colors.red],
|
||||
stops: <double>[0.0, 1.0],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
persistentFooterButtons: <Widget>[
|
||||
IconButton(
|
||||
onPressed: _animateResetInitialize,
|
||||
tooltip: 'Reset',
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
icon: const Icon(Icons.replay),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//********************************************************************
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user