Add smoke tests for all the examples, fix 17 broken examples. (#89021)

This adds a smoke test for every single API example. It also fixes 17 tests that had bugs in them, or were otherwise broken, and even fixes one actual bug in the framework, and one limitation in the framework.

The bug in the framework is that NetworkImage's _loadAsync method had await response.drain<List<int>>();, but if the response is null, it will throw a cryptic exception saying that Null can't be assigned to List<int>. The fix was just to use await response.drain<void>(); instead.

The limitation is that RelativePositionedTransition takes an Animation<Rect> rect parameter, and if you want to use a RectTween with it, the value emitted there is Rect?, and one of the examples was just casting from Animation<Rect> to Animation<Rect?>, which is invalid, so I modified RelativePositionedTransition to take a Rect? and just use Rect.zero if the rect is null.
This commit is contained in:
Greg Spencer
2021-09-28 09:32:06 -07:00
committed by GitHub
parent d508a247be
commit ab2b0851a2
113 changed files with 908 additions and 343 deletions

View File

@@ -98,9 +98,9 @@ class _FollowCurve2DState extends State<FollowCurve2D>
@override
void dispose() {
super.dispose();
// Always have to dispose of animation controllers when done.
controller.dispose();
super.dispose();
}
@override

View File

@@ -64,32 +64,39 @@ class _MyStatefulWidgetState extends State<MyStatefulWidget> {
@override
Widget build(BuildContext context) {
return CupertinoApp(
home: CupertinoPageScaffold(
child: CustomScrollView(
physics:
const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
slivers: <Widget>[
const CupertinoSliverNavigationBar(largeTitle: Text('Scroll down')),
CupertinoSliverRefreshControl(
refreshTriggerPullDistance: 100.0,
refreshIndicatorExtent: 60.0,
onRefresh: () async {
await Future<void>.delayed(const Duration(milliseconds: 1000));
setState(() {
items.insert(
0, Container(color: colors[items.length % 3], height: 100.0));
});
},
return CupertinoPageScaffold(
child: CustomScrollView(
physics: const BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics(),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) => items[index],
childCount: items.length,
slivers: <Widget>[
const CupertinoSliverNavigationBar(
largeTitle: Text('Scroll down'),
),
),
],
)));
CupertinoSliverRefreshControl(
refreshTriggerPullDistance: 100.0,
refreshIndicatorExtent: 60.0,
onRefresh: () async {
await Future<void>.delayed(
const Duration(milliseconds: 1000),
);
setState(() {
items.insert(
0,
Container(color: colors[items.length % 3], height: 100.0),
);
});
},
),
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) => items[index],
childCount: items.length,
),
),
],
),
);
}
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)

View File

@@ -2,7 +2,7 @@
// 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
// Template: dev/snippets/config/templates/stateful_widget_scaffold_center.tmpl
//
// Comment lines marked with "▼▼▼" and "▲▲▲" are used for authoring
// of samples, and may be ignored if you are just exploring the sample.
@@ -30,9 +30,14 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
return MaterialApp(
title: _title,
home: MyStatefulWidget(),
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const Center(
child: MyStatefulWidget(),
),
),
);
}
}

View File

@@ -2,7 +2,7 @@
// 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
// 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.
@@ -12,12 +12,12 @@
//***************************************************************************
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
// This example shows how to use this animation to create a
// [FloatingActionButton] that animates itself between the normal and
// extended states of the [NavigationRail].
// This example shows how to use this animation to create a [FloatingActionButton]
// that animates itself between the normal and extended states of the
// [NavigationRail].
//
// An instance of `ExtendableFab` would be created for
// [NavigationRail.leading].
// An instance of `MyNavigationRailFab` is created for [NavigationRail.leading].
// Pressing the FAB button toggles the "extended" state of the [NavigationRail].
//* ▲▲▲▲▲▲▲▲ description ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
//***************************************************************************
@@ -32,34 +32,107 @@ import 'dart:ui';
import 'package:flutter/material.dart';
//********************************************************************************
//* ▼▼▼▼▼▼▼▼ code-main ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
void main() => runApp(const MyApp());
/// This is the main application widget.
//* ▲▲▲▲▲▲▲▲ code-main ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
//********************************************************************************
//********************************************************************************
//* ▼▼▼▼▼▼▼▼ code-preamble ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
static const String _title = 'NavigationRail.extendedAnimation Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
return MaterialApp(
title: _title,
home: MyStatelessWidget(),
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const MyNavigationRail(),
),
);
}
}
/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
const MyStatelessWidget({Key? key}) : super(key: key);
class MyNavigationRail extends StatefulWidget {
const MyNavigationRail({Key? key}) : super(key: key);
@override
State<MyNavigationRail> createState() => _MyNavigationRailState();
}
class _MyNavigationRailState extends State<MyNavigationRail> {
int _selectedIndex = 0;
bool _extended = false;
@override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
NavigationRail(
selectedIndex: _selectedIndex,
extended: _extended,
leading: MyNavigationRailFab(onPressed: () {
setState(() {
_extended = !_extended;
});
}),
onDestinationSelected: (int index) {
setState(() {
_selectedIndex = index;
});
},
labelType: NavigationRailLabelType.none,
destinations: const <NavigationRailDestination>[
NavigationRailDestination(
icon: Icon(Icons.favorite_border),
selectedIcon: Icon(Icons.favorite),
label: Text('First'),
),
NavigationRailDestination(
icon: Icon(Icons.bookmark_border),
selectedIcon: Icon(Icons.book),
label: Text('Second'),
),
NavigationRailDestination(
icon: Icon(Icons.star_border),
selectedIcon: Icon(Icons.star),
label: Text('Third'),
),
],
),
const VerticalDivider(thickness: 1, width: 1),
// This is the main content.
Expanded(
child: Center(
child: Text('selectedIndex: $_selectedIndex'),
),
)
],
);
}
}
//* ▲▲▲▲▲▲▲▲ code-preamble ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
//********************************************************************************
//********************************************************************
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
class MyNavigationRailFab extends StatelessWidget {
const MyNavigationRailFab({Key? key, this.onPressed}) : super(key: key);
final VoidCallback? onPressed;
@override
Widget build(BuildContext context) {
final Animation<double> animation =
NavigationRail.extendedAnimation(context);
final Animation<double> animation = NavigationRail.extendedAnimation(context);
return AnimatedBuilder(
animation: animation,
builder: (BuildContext context, Widget? child) {
@@ -72,7 +145,7 @@ class MyStatelessWidget extends StatelessWidget {
child: animation.value == 0
? FloatingActionButton(
child: const Icon(Icons.add),
onPressed: () {},
onPressed: onPressed,
)
: Align(
alignment: AlignmentDirectional.centerStart,
@@ -82,7 +155,7 @@ class MyStatelessWidget extends StatelessWidget {
child: FloatingActionButton.extended(
icon: const Icon(Icons.add),
label: const Text('CREATE'),
onPressed: () {},
onPressed: onPressed,
),
),
),
@@ -90,8 +163,7 @@ class MyStatelessWidget extends StatelessWidget {
},
);
}
}
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
//********************************************************************
}

View File

@@ -2,7 +2,7 @@
// 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
// Template: dev/snippets/config/templates/stateful_widget_scaffold_center.tmpl
//
// Comment lines marked with "▼▼▼" and "▲▲▲" are used for authoring
// of samples, and may be ignored if you are just exploring the sample.
@@ -29,9 +29,14 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
return MaterialApp(
title: _title,
home: MyStatefulWidget(),
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const Center(
child: MyStatefulWidget(),
),
),
);
}
}

View File

@@ -22,6 +22,7 @@
//****************************************************************************
//* ▼▼▼▼▼▼▼▼ code-imports ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
@@ -33,15 +34,29 @@ import 'package:flutter/widgets.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
print(measureWidget(const SizedBox(width: 640, height: 480)));
final Size size = measureWidget(const SizedBox(width: 640, height: 480));
// Just displays the size calculated above.
runApp(
WidgetsApp(
title: 'BuildOwner Sample',
color: const Color(0xff000000),
builder: (BuildContext context, Widget? child) {
return Scaffold(
body: Center(
child: Text(size.toString()),
),
);
},
),
);
}
Size measureWidget(Widget widget) {
final PipelineOwner pipelineOwner = PipelineOwner();
final MeasurementView rootView = pipelineOwner.rootNode = MeasurementView();
final BuildOwner buildOwner = BuildOwner(focusManager: FocusManager());
final RenderObjectToWidgetElement<RenderBox> element =
RenderObjectToWidgetAdapter<RenderBox>(
final RenderObjectToWidgetElement<RenderBox> element = RenderObjectToWidgetAdapter<RenderBox>(
container: rootView,
debugShortDescription: '[root]',
child: widget,
@@ -57,8 +72,7 @@ Size measureWidget(Widget widget) {
}
}
class MeasurementView extends RenderBox
with RenderObjectWithChildMixin<RenderBox> {
class MeasurementView extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
@override
void performLayout() {
assert(child != null);

View File

@@ -12,45 +12,94 @@
//***************************************************************************
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
// This example shows how to override the standard error widget builder in release
// mode, but use the standard one in debug mode.
//
// The error occurs when you click the "Error Prone" button.
//* ▲▲▲▲▲▲▲▲ description ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
//***************************************************************************
//********************************************************************
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
//*************************************************************************
//* ▼▼▼▼▼▼▼▼ code-imports ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
import 'package:flutter/material.dart';
//* ▲▲▲▲▲▲▲▲ code-imports ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
//*************************************************************************
//********************************************************************
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
void main() {
// Set the ErrorWidget's builder before the app is started.
ErrorWidget.builder = (FlutterErrorDetails details) {
// This is how to tell if you're in debug mode: Assertions are only executed in
// debug mode.
bool inDebug = false;
assert(() {
inDebug = true;
return true;
}());
// In debug mode, use the normal error widget which shows
// the error message:
// If we're in debug mode, use the normal error widget which shows the error
// message:
if (inDebug) {
return ErrorWidget(details.exception);
}
// In release builds, show a yellow-on-blue message instead:
return Container(
alignment: Alignment.center,
child: const Text(
'Error!',
style: TextStyle(color: Colors.yellow),
child: Text(
'Error!\n${details.exception}',
style: const TextStyle(color: Colors.yellow),
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
),
);
};
// Here we would normally runApp() the root widget, but to demonstrate
// the error handling we artificially fail:
return runApp(Builder(
builder: (BuildContext context) {
throw 'oh no, an error';
},
));
// Start the app.
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'ErrorWidget Sample';
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool throwError = false;
@override
Widget build(BuildContext context) {
if (throwError) {
// Since the error widget is only used during a build, in this contrived example,
// we purposely throw an exception in a build function.
return Builder(
builder: (BuildContext context) {
throw Exception('oh no, an error');
},
);
} else {
return MaterialApp(
title: MyApp._title,
home: Scaffold(
appBar: AppBar(title: const Text(MyApp._title)),
body: Center(
child: TextButton(
onPressed: () {
setState(() { throwError = true; });
},
child: const Text('Error Prone')),
),
),
);
}
}
}
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)

View File

@@ -2,7 +2,7 @@
// 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
// Template: dev/snippets/config/templates/stateless_widget_scaffold_center.tmpl
//
// Comment lines marked with "▼▼▼" and "▲▲▲" are used for authoring
// of samples, and may be ignored if you are just exploring the sample.
@@ -38,9 +38,14 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
return MaterialApp(
title: _title,
home: MyStatelessWidget(),
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const Center(
child: MyStatelessWidget(),
),
),
);
}
}

View File

@@ -2,7 +2,7 @@
// 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
// Template: dev/snippets/config/templates/stateless_widget_scaffold_center.tmpl
//
// Comment lines marked with "▼▼▼" and "▲▲▲" are used for authoring
// of samples, and may be ignored if you are just exploring the sample.
@@ -32,7 +32,11 @@ class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatelessWidget(),
home: Scaffold(
body: Center(
child: MyStatelessWidget(),
),
),
);
}
}

View File

@@ -53,7 +53,7 @@ class MyStatelessWidget extends StatelessWidget {
borderRadius: BorderRadius.circular(20),
),
child: Image.network(
'https://example.com/image.jpg',
'https://flutter.github.io/assets-for-api-docs/assets/widgets/falcon.jpg',
loadingBuilder: (BuildContext context, Widget child,
ImageChunkEvent? loadingProgress) {
if (loadingProgress == null) {

View File

@@ -46,7 +46,7 @@ class IVBuilderExampleApp extends StatelessWidget {
class _IVBuilderExample extends StatefulWidget {
@override
_IVBuilderExampleState createState() => _IVBuilderExampleState();
State<_IVBuilderExample> createState() => _IVBuilderExampleState();
}
class _IVBuilderExampleState extends State<_IVBuilderExample> {
@@ -58,7 +58,7 @@ class _IVBuilderExampleState extends State<_IVBuilderExample> {
// Returns true iff the given cell is currently visible. Caches viewport
// calculations.
late Quad _cachedViewport;
Quad? _cachedViewport;
late int _firstVisibleRow;
late int _firstVisibleColumn;
late int _lastVisibleRow;

View File

@@ -1,69 +0,0 @@
// 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 TransitionDelegate
//
//***************************************************************************
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
// The following example demonstrates how to implement a subclass that always
// removes or adds routes without animated transitions and puts the removed
// routes at the top of the list.
//* ▲▲▲▲▲▲▲▲ description ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
//***************************************************************************
//****************************************************************************
//* ▼▼▼▼▼▼▼▼ code-imports ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
import 'package:flutter/widgets.dart';
//* ▲▲▲▲▲▲▲▲ code-imports ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
//****************************************************************************
//********************************************************************
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
class NoAnimationTransitionDelegate extends TransitionDelegate<void> {
@override
Iterable<RouteTransitionRecord> resolve({
required List<RouteTransitionRecord> newPageRouteHistory,
required Map<RouteTransitionRecord?, RouteTransitionRecord>
locationToExitingPageRoute,
required Map<RouteTransitionRecord?, List<RouteTransitionRecord>>
pageRouteToPagelessRoutes,
}) {
final List<RouteTransitionRecord> results = <RouteTransitionRecord>[];
for (final RouteTransitionRecord pageRoute in newPageRouteHistory) {
if (pageRoute.isWaitingForEnteringDecision) {
pageRoute.markForAdd();
}
results.add(pageRoute);
}
for (final RouteTransitionRecord exitingPageRoute
in locationToExitingPageRoute.values) {
if (exitingPageRoute.isWaitingForExitingDecision) {
exitingPageRoute.markForRemove();
final List<RouteTransitionRecord>? pagelessRoutes =
pageRouteToPagelessRoutes[exitingPageRoute];
if (pagelessRoutes != null) {
for (final RouteTransitionRecord pagelessRoute in pagelessRoutes) {
pagelessRoute.markForRemove();
}
}
}
results.add(exitingPageRoute);
}
return results;
}
}
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
//********************************************************************

View File

@@ -29,10 +29,12 @@ class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return WidgetsApp(
title: 'Flutter Code Sample',
home: const Center(
child: MyStatefulWidget(restorationId: 'main'),
),
color: const Color(0xffffffff),
builder: (BuildContext context, Widget? child) {
return const Center(
child: MyStatefulWidget(restorationId: 'main'),
);
},
);
}
}

View File

@@ -82,7 +82,7 @@ class _MyStatefulWidgetState extends State<MyStatefulWidget>
).animate(CurvedAnimation(
parent: _controller,
curve: Curves.elasticInOut,
)) as Animation<Rect>,
)),
child: const Padding(
padding: EdgeInsets.all(8), child: FlutterLogo()),
),

View File

@@ -63,8 +63,8 @@ class _MyStatefulWidgetState extends State<MyStatefulWidget>
@override
void dispose() {
super.dispose();
_controller.dispose();
super.dispose();
}
@override

View File

@@ -64,8 +64,8 @@ class _MyStatefulWidgetState extends State<MyStatefulWidget>
@override
void dispose() {
super.dispose();
_controller.dispose();
super.dispose();
}
@override

View File

@@ -70,8 +70,8 @@ class _MyStatefulWidgetState extends State<MyStatefulWidget>
@override
void dispose() {
super.dispose();
_controller.dispose();
super.dispose();
}
@override

View File

@@ -78,6 +78,12 @@ class _MyStatefulWidgetState extends State<MyStatefulWidget>
controller.forward();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return CustomScrollView(slivers: <Widget>[