tool_bar.dart 3.44 KB
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
import 'package:flutter/widgets.dart';
6

7
import 'constants.dart';
Adam Barth's avatar
Adam Barth committed
8 9
import 'icon_theme.dart';
import 'icon_theme_data.dart';
10
import 'material.dart';
11
import 'theme.dart';
12
import 'typography.dart';
13

14
class ToolBar extends StatelessComponent {
15
  ToolBar({
16
    Key key,
17 18 19
    this.left,
    this.center,
    this.right,
Hans Muller's avatar
Hans Muller committed
20
    this.bottom,
21
    this.tabBar,
Hans Muller's avatar
Hans Muller committed
22
    this.elevation: 4,
Adam Barth's avatar
Adam Barth committed
23
    this.backgroundColor,
24
    this.textTheme,
25
    this.padding: EdgeDims.zero
26 27 28 29 30
  }) : super(key: key);

  final Widget left;
  final Widget center;
  final List<Widget> right;
Hans Muller's avatar
Hans Muller committed
31
  final Widget bottom;
32
  final Widget tabBar;
Hans Muller's avatar
Hans Muller committed
33
  final int elevation;
34
  final Color backgroundColor;
Adam Barth's avatar
Adam Barth committed
35
  final TextTheme textTheme;
36
  final EdgeDims padding;
37

38 39 40 41 42 43 44 45 46 47 48
  ToolBar copyWith({
    Key key,
    Widget left,
    Widget center,
    List<Widget> right,
    Widget bottom,
    int elevation,
    Color backgroundColor,
    TextTheme textTheme,
    EdgeDims padding
  }) {
49
    return new ToolBar(
50 51 52 53 54 55 56 57 58 59
      key: key ?? this.key,
      left: left ?? this.left,
      center: center ?? this.center,
      right: right ?? this.right,
      bottom: bottom ?? this.bottom,
      tabBar: tabBar ?? this.tabBar,
      elevation: elevation ?? this.elevation,
      backgroundColor: backgroundColor ?? this.backgroundColor,
      textTheme: textTheme ?? this.textTheme,
      padding: padding ?? this.padding
60 61
    );
  }
62

63
  Widget build(BuildContext context) {
64
    Color color = backgroundColor;
65
    IconThemeData iconThemeData;
Adam Barth's avatar
Adam Barth committed
66 67 68 69
    TextStyle centerStyle = textTheme?.title;
    TextStyle sideStyle = textTheme?.body1;

    if (color == null || iconThemeData == null || textTheme == null) {
70
      ThemeData themeData = Theme.of(context);
Adam Barth's avatar
Adam Barth committed
71 72 73 74 75 76
      color ??= themeData.primaryColor;
      iconThemeData ??= themeData.primaryIconTheme;

      TextTheme primaryTextTheme = themeData.primaryTextTheme;
      centerStyle ??= primaryTextTheme.title;
      sideStyle ??= primaryTextTheme.body2;
77 78
    }

79
    final List<Widget> firstRow = <Widget>[];
80
    if (left != null)
81 82
      firstRow.add(left);
    firstRow.add(
83 84
      new Flexible(
        child: new Padding(
85 86
          padding: new EdgeDims.only(left: 24.0),
          child: center != null ? new DefaultTextStyle(style: centerStyle, child: center) : null
87 88 89 90
        )
      )
    );
    if (right != null)
91 92 93 94 95 96 97 98 99 100
      firstRow.addAll(right);

    final List<Widget> rows = <Widget>[
      new Container(
        height: kToolBarHeight,
        child: new DefaultTextStyle(
          style: sideStyle,
          child: new Row(firstRow)
        )
      )
Hans Muller's avatar
Hans Muller committed
101
    ];
102 103 104 105 106 107 108 109 110 111 112
    if (bottom != null) {
      rows.add(
        new DefaultTextStyle(
          style: centerStyle,
          child: new Container(
            height: kExtendedToolBarHeight - kToolBarHeight,
            child: bottom
          )
        )
      );
    }
113
    if (tabBar != null)
114 115 116 117 118
      rows.add(tabBar);

    EdgeDims combinedPadding = new EdgeDims.symmetric(horizontal: 8.0);
    if (padding != null)
      combinedPadding += padding;
119

120 121 122 123 124 125 126 127 128
    Widget contents = new Material(
      color: color,
      elevation: elevation,
      child: new Container(
        padding: combinedPadding,
        child: new Column(
          rows,
          justifyContent: FlexJustifyContent.collapse
        )
129 130 131 132
      )
    );

    if (iconThemeData != null)
133 134 135
      contents = new IconTheme(data: iconThemeData, child: contents);

    return contents;
136 137 138
  }

}