tab_bar_theme.dart 4.23 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
7
import 'package:flutter/widgets.dart';
8 9

import 'tabs.dart';
10
import 'theme.dart';
11 12 13 14 15 16 17

/// Defines a theme for [TabBar] widgets.
///
/// A tab bar theme describes the color of the tab label and the size/shape of
/// the [TabBar.indicator].
///
/// Descendant widgets obtain the current theme's [TabBarTheme] object using
18 19
/// `TabBarTheme.of(context)`. Instances of [TabBarTheme] can be customized with
/// [TabBarTheme.copyWith].
20 21 22 23 24 25
///
/// See also:
///
///  * [TabBar], a widget that displays a horizontal row of tabs.
///  * [ThemeData], which describes the overall theme information for the
///    application.
26
class TabBarTheme with Diagnosticable {
27 28 29 30 31
  /// Creates a tab bar theme that can be used with [ThemeData.tabBarTheme].
  const TabBarTheme({
    this.indicator,
    this.indicatorSize,
    this.labelColor,
32
    this.labelPadding,
33
    this.labelStyle,
34
    this.unselectedLabelColor,
35
    this.unselectedLabelStyle,
36 37 38 39 40 41 42 43 44 45 46
  });

  /// Default value for [TabBar.indicator].
  final Decoration indicator;

  /// Default value for [TabBar.indicatorSize].
  final TabBarIndicatorSize indicatorSize;

  /// Default value for [TabBar.labelColor].
  final Color labelColor;

47 48 49
  /// Default value for [TabBar.labelPadding].
  final EdgeInsetsGeometry labelPadding;

50 51 52
  /// Default value for [TabBar.labelStyle].
  final TextStyle labelStyle;

53 54 55
  /// Default value for [TabBar.unselectedLabelColor].
  final Color unselectedLabelColor;

56 57 58
  /// Default value for [TabBar.unselectedLabelStyle].
  final TextStyle unselectedLabelStyle;

59 60 61 62 63 64
  /// Creates a copy of this object but with the given fields replaced with the
  /// new values.
  TabBarTheme copyWith({
    Decoration indicator,
    TabBarIndicatorSize indicatorSize,
    Color labelColor,
65
    EdgeInsetsGeometry labelPadding,
66
    TextStyle labelStyle,
67
    Color unselectedLabelColor,
68
    TextStyle unselectedLabelStyle,
69 70
  }) {
    return TabBarTheme(
71 72 73
      indicator: indicator ?? this.indicator,
      indicatorSize: indicatorSize ?? this.indicatorSize,
      labelColor: labelColor ?? this.labelColor,
74
      labelPadding: labelPadding ?? this.labelPadding,
75 76 77
      labelStyle: labelStyle ?? this.labelStyle,
      unselectedLabelColor: unselectedLabelColor ?? this.unselectedLabelColor,
      unselectedLabelStyle: unselectedLabelStyle ?? this.unselectedLabelStyle,
78 79 80
    );
  }

81 82 83 84 85
  /// The data from the closest [TabBarTheme] instance given the build context.
  static TabBarTheme of(BuildContext context) {
    return Theme.of(context).tabBarTheme;
  }

86 87
  /// Linearly interpolate between two tab bar themes.
  ///
88 89
  /// The arguments must not be null.
  ///
90
  /// {@macro dart.ui.shadow.lerp}
91 92 93 94 95 96 97 98
  static TabBarTheme lerp(TabBarTheme a, TabBarTheme b, double t) {
    assert(a != null);
    assert(b != null);
    assert(t != null);
    return TabBarTheme(
      indicator: Decoration.lerp(a.indicator, b.indicator, t),
      indicatorSize: t < 0.5 ? a.indicatorSize : b.indicatorSize,
      labelColor: Color.lerp(a.labelColor, b.labelColor, t),
99
      labelPadding: EdgeInsetsGeometry.lerp(a.labelPadding, b.labelPadding, t),
100 101 102
      labelStyle: TextStyle.lerp(a.labelStyle, b.labelStyle, t),
      unselectedLabelColor: Color.lerp(a.unselectedLabelColor, b.unselectedLabelColor, t),
      unselectedLabelStyle: TextStyle.lerp(a.unselectedLabelStyle, b.unselectedLabelStyle, t),
103 104 105 106 107
    );
  }

  @override
  int get hashCode {
108 109 110 111
    return hashValues(
      indicator,
      indicatorSize,
      labelColor,
112
      labelPadding,
113 114 115 116
      labelStyle,
      unselectedLabelColor,
      unselectedLabelStyle,
    );
117 118 119
  }

  @override
120
  bool operator ==(Object other) {
121 122 123 124
    if (identical(this, other))
      return true;
    if (other.runtimeType != runtimeType)
      return false;
125 126 127 128 129 130 131 132
    return other is TabBarTheme
        && other.indicator == indicator
        && other.indicatorSize == indicatorSize
        && other.labelColor == labelColor
        && other.labelPadding == labelPadding
        && other.labelStyle == labelStyle
        && other.unselectedLabelColor == unselectedLabelColor
        && other.unselectedLabelStyle == unselectedLabelStyle;
133 134
  }
}