-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique_widget.dart
40 lines (36 loc) · 1.46 KB
/
unique_widget.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// 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.
import 'framework.dart';
/// Base class for stateful widgets that have exactly one inflated instance in
/// the tree.
///
/// Such widgets must be given a [GlobalKey]. This key can be generated by the
/// subclass from its [Type] object, e.g. by calling `super(key: new
/// GlobalObjectKey(MyWidget))` where `MyWidget` is the name of the subclass.
///
/// Since only one instance can be inflated at a time, there is only ever one
/// corresponding [State] object. That object is exposed, for convenience, via
/// the [currentState] property.
///
/// When subclassing [UniqueWidget], provide the corresponding [State] subclass
/// as the type argument.
abstract class UniqueWidget<T extends State<StatefulWidget>> extends StatefulWidget {
/// Creates a widget that has exactly one inflated instance in the tree.
///
/// The [key] argument must not be null because it identifies the unique
/// inflated instance of this widget.
const UniqueWidget({
@required GlobalKey<T> key,
}) : assert(key != null),
super(key: key);
@override
T createState();
/// The state for the unique inflated instance of this widget.
///
/// Might be null if the widget is not currently in the tree.
T get currentState {
final GlobalKey<T> globalKey = key as GlobalKey<T>;
return globalKey.currentState;
}
}