tab_bar_theme.dart 4.39 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// 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';
6
import 'package:flutter/widgets.dart';
7 8

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

/// 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
17 18
/// `TabBarTheme.of(context)`. Instances of [TabBarTheme] can be customized with
/// [TabBarTheme.copyWith].
19 20 21 22 23 24
///
/// See also:
///
///  * [TabBar], a widget that displays a horizontal row of tabs.
///  * [ThemeData], which describes the overall theme information for the
///    application.
25
@immutable
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
  });

  /// Default value for [TabBar.indicator].
39
  final Decoration? indicator;
40 41

  /// Default value for [TabBar.indicatorSize].
42
  final TabBarIndicatorSize? indicatorSize;
43 44

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

47
  /// Default value for [TabBar.labelPadding].
48 49 50 51
  ///
  /// If there are few tabs with both icon and text and few
  /// tabs with only icon or text, this padding is vertically
  /// adjusted to provide uniform padding to all tabs.
52
  final EdgeInsetsGeometry? labelPadding;
53

54
  /// Default value for [TabBar.labelStyle].
55
  final TextStyle? labelStyle;
56

57
  /// Default value for [TabBar.unselectedLabelColor].
58
  final Color? unselectedLabelColor;
59

60
  /// Default value for [TabBar.unselectedLabelStyle].
61
  final TextStyle? unselectedLabelStyle;
62

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

85 86
  /// The data from the closest [TabBarTheme] instance given the build context.
  static TabBarTheme of(BuildContext context) {
87
    return Theme.of(context).tabBarTheme;
88 89
  }

90 91
  /// Linearly interpolate between two tab bar themes.
  ///
92 93
  /// The arguments must not be null.
  ///
94
  /// {@macro dart.ui.shadow.lerp}
95 96 97 98 99 100 101 102
  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),
103
      labelPadding: EdgeInsetsGeometry.lerp(a.labelPadding, b.labelPadding, t),
104 105 106
      labelStyle: TextStyle.lerp(a.labelStyle, b.labelStyle, t),
      unselectedLabelColor: Color.lerp(a.unselectedLabelColor, b.unselectedLabelColor, t),
      unselectedLabelStyle: TextStyle.lerp(a.unselectedLabelStyle, b.unselectedLabelStyle, t),
107 108 109 110 111
    );
  }

  @override
  int get hashCode {
112 113 114 115
    return hashValues(
      indicator,
      indicatorSize,
      labelColor,
116
      labelPadding,
117 118 119 120
      labelStyle,
      unselectedLabelColor,
      unselectedLabelStyle,
    );
121 122 123
  }

  @override
124
  bool operator ==(Object other) {
125 126 127 128
    if (identical(this, other))
      return true;
    if (other.runtimeType != runtimeType)
      return false;
129 130 131 132 133 134 135 136
    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;
137 138
  }
}