tab_bar_theme.dart 2.98 KB
Newer Older
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
// Copyright 2018 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.

import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';

import 'tabs.dart';

/// 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
/// `Theme.of(context).tabBarTheme`.
/// [ThemeData.tabBarTheme] can be customized by copying it (using
/// [TabBarTheme.copyWith]).
///
/// See also:
///
///  * [TabBar], a widget that displays a horizontal row of tabs.
///  * [ThemeData], which describes the overall theme information for the
///    application.
class TabBarTheme extends Diagnosticable {
  /// Creates a tab bar theme that can be used with [ThemeData.tabBarTheme].
  const TabBarTheme({
    this.indicator,
    this.indicatorSize,
    this.labelColor,
    this.unselectedLabelColor,
  });

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

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

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

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

  /// Creates a copy of this object but with the given fields replaced with the
  /// new values.
  TabBarTheme copyWith({
    Decoration indicator,
    TabBarIndicatorSize indicatorSize,
    Color labelColor,
    Color unselectedLabelColor,
  }) {
    return TabBarTheme(
        indicator: indicator ?? this.indicator,
        indicatorSize: indicatorSize ?? this.indicatorSize,
        labelColor: labelColor ?? this.labelColor,
        unselectedLabelColor: unselectedLabelColor ?? this.unselectedLabelColor
    );
  }

  /// Linearly interpolate between two tab bar themes.
  ///
64 65
  /// The arguments must not be null.
  ///
66
  /// {@macro dart.ui.shadow.lerp}
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  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),
      unselectedLabelColor: Color.lerp(a.unselectedLabelColor, b.unselectedLabelColor, t)
    );
  }

  @override
  int get hashCode {
    return hashValues(indicator, indicatorSize, labelColor, unselectedLabelColor);
  }

  @override
  bool operator ==(dynamic other) {
    if (identical(this, other))
      return true;
    if (other.runtimeType != runtimeType)
      return false;
    final TabBarTheme typedOther = other;
    return typedOther.indicator == indicator
        && typedOther.indicatorSize == indicatorSize
        && typedOther.labelColor == labelColor
        && typedOther.unselectedLabelColor == unselectedLabelColor;
  }
}