Add tests for shared_app_data.#.dart API examples. (#147830)
This PR contributes to https://github.com/flutter/flutter/issues/130459 ### Description - Updates `examples/api/lib/widgets/shared_app_data/shared_app_data.0.dart` to meet latest API examples structure - Updates `examples/api/lib/widgets/shared_app_data/shared_app_data.1.dart` to meet latest API examples structure - Adds tests for `examples/api/lib/widgets/shared_app_data/shared_app_data.0.dart` - Adds tests for `examples/api/lib/widgets/shared_app_data/shared_app_data.1.dart`
This commit is contained in:
committed by
GitHub
parent
6cb9119148
commit
d9cf066191
@@ -6,38 +6,41 @@ import 'package:flutter/material.dart';
|
||||
|
||||
/// Flutter code sample for [SharedAppData].
|
||||
|
||||
class ShowSharedValue extends StatelessWidget {
|
||||
const ShowSharedValue({super.key, required this.appDataKey});
|
||||
void main() {
|
||||
runApp(const SharedAppDataExampleApp());
|
||||
}
|
||||
|
||||
final String appDataKey;
|
||||
class SharedAppDataExampleApp extends StatelessWidget {
|
||||
const SharedAppDataExampleApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// The SharedAppData.getValue() call causes this widget to depend on the
|
||||
// value of the SharedAppData's 'foo' key. If it's changed, with
|
||||
// SharedAppData.setValue(), then this widget will be rebuilt.
|
||||
final String value = SharedAppData.getValue<String, String>(context, appDataKey, () => 'initial');
|
||||
return Text('$appDataKey: $value');
|
||||
return const MaterialApp(
|
||||
home: SharedAppDataExample(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Demonstrates that changes to the SharedAppData _only_ cause the dependent
|
||||
// widgets to be rebuilt. In this case that's the ShowSharedValue widget that's
|
||||
// displaying the value of a key whose value has been updated.
|
||||
class Home extends StatefulWidget {
|
||||
const Home({super.key});
|
||||
class SharedAppDataExample extends StatefulWidget {
|
||||
const SharedAppDataExample({super.key});
|
||||
|
||||
@override
|
||||
State<Home> createState() => _HomeState();
|
||||
State<SharedAppDataExample> createState() => _SharedAppDataExampleState();
|
||||
}
|
||||
|
||||
class _HomeState extends State<Home> {
|
||||
class _SharedAppDataExampleState extends State<SharedAppDataExample> {
|
||||
int _fooVersion = 0;
|
||||
int _barVersion = 0;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('SharedAppData Sample'),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
@@ -50,10 +53,13 @@ class _HomeState extends State<Home> {
|
||||
child: const Text('change foo'),
|
||||
onPressed: () {
|
||||
_fooVersion += 1;
|
||||
// Changing the SharedAppData's value for 'foo' causes the
|
||||
// widgets that depend on 'foo' to be rebuilt.
|
||||
// Changing the SharedAppData's value for 'foo' key causes the
|
||||
// widgets that depend on 'foo' key to be rebuilt.
|
||||
SharedAppData.setValue<String, String?>(
|
||||
context, 'foo', 'FOO $_fooVersion'); // no need to call setState()
|
||||
context,
|
||||
'foo',
|
||||
'FOO $_fooVersion',
|
||||
); // No need to call setState().
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
@@ -61,8 +67,13 @@ class _HomeState extends State<Home> {
|
||||
child: const Text('change bar'),
|
||||
onPressed: () {
|
||||
_barVersion += 1;
|
||||
// Changing the SharedAppData's value for 'bar' key causes the
|
||||
// widgets that depend on 'bar' key to be rebuilt.
|
||||
SharedAppData.setValue<String, String?>(
|
||||
context, 'bar', 'BAR $_barVersion'); // no need to call setState()
|
||||
context,
|
||||
'bar',
|
||||
'BAR $_barVersion',
|
||||
); // No need to call setState().
|
||||
},
|
||||
),
|
||||
],
|
||||
@@ -72,6 +83,22 @@ class _HomeState extends State<Home> {
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
runApp(const MaterialApp(home: Home()));
|
||||
class ShowSharedValue extends StatelessWidget {
|
||||
const ShowSharedValue({super.key, required this.appDataKey});
|
||||
|
||||
final String appDataKey;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// The SharedAppData.getValue() call causes this widget to depend on the
|
||||
// value of the SharedAppData's key. If it's changed, with
|
||||
// SharedAppData.setValue(), then this widget will be rebuilt.
|
||||
final String value = SharedAppData.getValue<String, String>(
|
||||
context,
|
||||
appDataKey,
|
||||
() => 'initial',
|
||||
);
|
||||
|
||||
return Text('$appDataKey: $value');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,27 +7,34 @@ import 'package:flutter/material.dart';
|
||||
|
||||
/// Flutter code sample for [SharedAppData].
|
||||
|
||||
// A single lazily-constructed object that's shared with the entire application
|
||||
// via `SharedObject.of(context)`. The value of the object can be changed with
|
||||
// `SharedObject.reset(context)`. Resetting the value will cause all of the
|
||||
// widgets that depend on it to be rebuilt.
|
||||
class SharedObject {
|
||||
SharedObject._();
|
||||
void main() {
|
||||
runApp(const SharedAppDataExampleApp());
|
||||
}
|
||||
|
||||
static final Object _sharedObjectKey = Object();
|
||||
class SharedAppDataExampleApp extends StatelessWidget {
|
||||
const SharedAppDataExampleApp({super.key});
|
||||
|
||||
@override
|
||||
String toString() => describeIdentity(this);
|
||||
|
||||
static void reset(BuildContext context) {
|
||||
// Calling SharedAppData.setValue() causes dependent widgets to be rebuilt.
|
||||
SharedAppData.setValue<Object, SharedObject>(context, _sharedObjectKey, SharedObject._());
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
home: SharedAppDataExample(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static SharedObject of(BuildContext context) {
|
||||
// If a value for _sharedObjectKey has never been set then the third
|
||||
// callback parameter is used to generate an initial value.
|
||||
return SharedAppData.getValue<Object, SharedObject>(context, _sharedObjectKey, () => SharedObject._());
|
||||
class SharedAppDataExample extends StatelessWidget {
|
||||
const SharedAppDataExample({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('SharedAppData Sample'),
|
||||
),
|
||||
body: const Center(
|
||||
child: CustomWidget(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,17 +55,34 @@ class CustomWidget extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
class Home extends StatelessWidget {
|
||||
const Home({super.key});
|
||||
// A single lazily-constructed object that's shared with the entire application
|
||||
// via `SharedObject.of(context)`. The value of the object can be changed with
|
||||
// `SharedObject.reset(context)`. Resetting the value will cause all of the
|
||||
// widgets that depend on it to be rebuilt.
|
||||
class SharedObject {
|
||||
SharedObject._();
|
||||
|
||||
static final Object _sharedObjectKey = Object();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const Scaffold(
|
||||
body: Center(child: CustomWidget()),
|
||||
String toString() => describeIdentity(this);
|
||||
|
||||
static void reset(BuildContext context) {
|
||||
// Calling SharedAppData.setValue() causes dependent widgets to be rebuilt.
|
||||
SharedAppData.setValue<Object, SharedObject>(
|
||||
context,
|
||||
_sharedObjectKey,
|
||||
SharedObject._(),
|
||||
);
|
||||
}
|
||||
|
||||
static SharedObject of(BuildContext context) {
|
||||
// If a value for _sharedObjectKey has never been set then the third
|
||||
// callback parameter is used to generate an initial value.
|
||||
return SharedAppData.getValue<Object, SharedObject>(
|
||||
context,
|
||||
_sharedObjectKey,
|
||||
() => SharedObject._(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
runApp(const MaterialApp(home: Home()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user