unique_widget.dart 999 Bytes
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'package:meta/meta.dart';

7
import 'framework.dart';
8

9
/// A widget that has exactly one inflated instance in the tree.
10
abstract class UniqueWidget<T extends State> extends StatefulWidget {
11 12 13 14 15 16 17
  /// Creates a widget that has exactly one inflated instance in the tree.
  ///
  /// The [key] argument cannot be null because it identifies the unique
  /// inflated instance of this widget.
  UniqueWidget({
    @required GlobalKey key
  }) : super(key: key) {
18 19
    assert(key != null);
  }
20

21
  @override
Adam Barth's avatar
Adam Barth committed
22
  T createState();
23

24
  /// The state for the unique inflated instance of this widget.
25 26
  ///
  /// Might be null if the widget is not currently in the tree.
Adam Barth's avatar
Adam Barth committed
27 28
  T get currentState {
    GlobalKey globalKey = key;
29
    return globalKey.currentState; // ignore: return_of_invalid_type, https://github.com/flutter/flutter/issues/5771
Adam Barth's avatar
Adam Barth committed
30
  }
31
}