Added SharedAppData to the widgets library (#93175)
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
// 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.
|
||||
|
||||
// Flutter code sample for SharedAppData
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class ShowSharedValue extends StatelessWidget {
|
||||
const ShowSharedValue({ Key? key, required this.appDataKey }) : super(key: key);
|
||||
|
||||
final String appDataKey;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// The SharedAppData.getValue() call here 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');
|
||||
}
|
||||
}
|
||||
|
||||
// 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({ Key? key }) : super(key: key);
|
||||
|
||||
@override
|
||||
State<Home> createState() => _HomeState();
|
||||
}
|
||||
|
||||
class _HomeState extends State<Home> {
|
||||
int _fooVersion = 0;
|
||||
int _barVersion = 0;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
const ShowSharedValue(appDataKey: 'foo'),
|
||||
const SizedBox(height: 16),
|
||||
const ShowSharedValue(appDataKey: 'bar'),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
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.
|
||||
SharedAppData.setValue<String, String?>(context, 'foo', 'FOO $_fooVersion'); // note: no setState()
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
child: const Text('change bar'),
|
||||
onPressed: () {
|
||||
_barVersion += 1;
|
||||
SharedAppData.setValue<String, String?>(context, 'bar', 'BAR $_barVersion'); // note: no setState()
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
runApp(const MaterialApp(home: Home()));
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// 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.
|
||||
|
||||
// Flutter code sample for SharedAppData
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
// 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
|
||||
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._());
|
||||
}
|
||||
}
|
||||
|
||||
// An example of a widget which depends on the SharedObject's value,
|
||||
// which might be provided - along with SharedObject - in a Dart package.
|
||||
class CustomWidget extends StatelessWidget {
|
||||
const CustomWidget({ Key? key }) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Will be rebuilt if the shared object's value is changed.
|
||||
return ElevatedButton(
|
||||
child: Text('Replace ${SharedObject.of(context)}'),
|
||||
onPressed: () {
|
||||
SharedObject.reset(context);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Home extends StatelessWidget {
|
||||
const Home({ Key? key }) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const Scaffold(
|
||||
body: Center(
|
||||
child: CustomWidget()
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
runApp(const MaterialApp(home: Home()));
|
||||
}
|
||||
Reference in New Issue
Block a user