divider.dart 9.65 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hans Muller's avatar
Hans Muller committed
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/widgets.dart';

7
import 'divider_theme.dart';
Hans Muller's avatar
Hans Muller committed
8 9
import 'theme.dart';

10
// Examples can assume:
11
// late BuildContext context;
12

13
/// A thin horizontal line, with padding on either side.
14
///
15
/// In the Material Design language, this represents a divider. Dividers can be
16
/// used in lists, [Drawer]s, and elsewhere to separate content.
17
///
18
/// To create a divider between [ListTile] items, consider using
jslavitz's avatar
jslavitz committed
19
/// [ListTile.divideTiles], which is optimized for this case.
20
///
21 22
/// {@youtube 560 315 https://www.youtube.com/watch?v=_liUC641Nmk}
///
jslavitz's avatar
jslavitz committed
23
/// The box's total height is controlled by [height]. The appropriate
24
/// padding is automatically computed from the height.
25
///
26
/// {@tool dartpad}
27 28 29 30 31 32 33
/// This sample shows how to display a Divider between an orange and blue box
/// inside a column. The Divider is 20 logical pixels in height and contains a
/// vertically centered black line that is 5 logical pixels thick. The black
/// line is indented by 20 logical pixels.
///
/// ![](https://flutter.github.io/assets-for-api-docs/assets/material/divider.png)
///
34
/// ** See code in examples/api/lib/material/divider/divider.0.dart **
35
/// {@end-tool}
36
///
37
/// See also:
38
///
39
///  * [PopupMenuDivider], which is the equivalent but for popup menus.
40
///  * [ListTile.divideTiles], another approach to dividing widgets in a list.
41
///  * [VerticalDivider], which is the vertical analog of this widget.
42
///  * <https://material.io/design/components/dividers.html>
43
class Divider extends StatelessWidget {
44
  /// Creates a Material Design divider.
45
  ///
46 47
  /// The [height], [thickness], [indent], and [endIndent] must be null or
  /// non-negative.
48
  const Divider({
49
    super.key,
50 51 52 53
    this.height,
    this.thickness,
    this.indent,
    this.endIndent,
54
    this.color,
55 56 57
  }) : assert(height == null || height >= 0.0),
       assert(thickness == null || thickness >= 0.0),
       assert(indent == null || indent >= 0.0),
58
       assert(endIndent == null || endIndent >= 0.0);
Hans Muller's avatar
Hans Muller committed
59

jslavitz's avatar
jslavitz committed
60 61

  /// The divider's height extent.
62
  ///
63 64
  /// The divider itself is always drawn as a horizontal line that is centered
  /// within the height specified by this value.
65
  ///
66 67
  /// If this is null, then the [DividerThemeData.space] is used. If that is
  /// also null, then this defaults to 16.0.
68
  final double? height;
69

70 71 72 73 74
  /// The thickness of the line drawn within the divider.
  ///
  /// A divider with a [thickness] of 0.0 is always drawn as a line with a
  /// height of exactly one device pixel.
  ///
75
  /// If this is null, then the [DividerThemeData.thickness] is used. If
76
  /// that is also null, then this defaults to 0.0.
77
  final double? thickness;
78 79 80 81 82

  /// The amount of empty space to the leading edge of the divider.
  ///
  /// If this is null, then the [DividerThemeData.indent] is used. If that is
  /// also null, then this defaults to 0.0.
83
  final double? indent;
84

85 86 87 88
  /// The amount of empty space to the trailing edge of the divider.
  ///
  /// If this is null, then the [DividerThemeData.endIndent] is used. If that is
  /// also null, then this defaults to 0.0.
89
  final double? endIndent;
90

91 92
  /// The color to use when painting the line.
  ///
93 94
  /// If this is null, then the [DividerThemeData.color] is used. If that is
  /// also null, then [ThemeData.dividerColor] is used.
95
  ///
96
  /// {@tool snippet}
97
  ///
98
  /// ```dart
99
  /// const Divider(
100 101
  ///   color: Colors.deepOrange,
  /// )
102
  /// ```
103
  /// {@end-tool}
104
  final Color? color;
Hans Muller's avatar
Hans Muller committed
105

106
  /// Computes the [BorderSide] that represents a divider.
107
  ///
108 109 110 111 112 113 114 115
  /// If [color] is null, then [DividerThemeData.color] is used. If that is also
  /// null, then [ThemeData.dividerColor] is used.
  ///
  /// If [width] is null, then [DividerThemeData.thickness] is used. If that is
  /// also null, then this defaults to 0.0 (a hairline border).
  ///
  /// If [context] is null, the default color of [BorderSide] is used and the
  /// default width of 0.0 is used.
116
  ///
117
  /// {@tool snippet}
118 119 120 121 122 123
  ///
  /// This example uses this method to create a box that has a divider above and
  /// below it. This is sometimes useful with lists, for instance, to separate a
  /// scrollable section from the rest of the interface.
  ///
  /// ```dart
124 125 126
  /// DecoratedBox(
  ///   decoration: BoxDecoration(
  ///     border: Border(
127 128 129 130 131 132 133
  ///       top: Divider.createBorderSide(context),
  ///       bottom: Divider.createBorderSide(context),
  ///     ),
  ///   ),
  ///   // child: ...
  /// )
  /// ```
134
  /// {@end-tool}
135 136
  static BorderSide createBorderSide(BuildContext? context, { Color? color, double? width }) {
    final Color? effectiveColor = color
137
        ?? (context != null ? (DividerTheme.of(context).color ?? Theme.of(context).dividerColor) : null);
138 139 140 141 142 143 144 145 146 147 148
    final double effectiveWidth =  width
        ?? (context != null ? DividerTheme.of(context).thickness : null)
        ?? 0.0;

    // Prevent assertion since it is possible that context is null and no color
    // is specified.
    if (effectiveColor == null) {
      return BorderSide(
        width: effectiveWidth,
      );
    }
149
    return BorderSide(
150 151
      color: effectiveColor,
      width: effectiveWidth,
152 153 154
    );
  }

155
  @override
Hans Muller's avatar
Hans Muller committed
156
  Widget build(BuildContext context) {
157 158 159 160 161 162
    final DividerThemeData dividerTheme = DividerTheme.of(context);
    final double height = this.height ?? dividerTheme.space ?? 16.0;
    final double thickness = this.thickness ?? dividerTheme.thickness ?? 0.0;
    final double indent = this.indent ?? dividerTheme.indent ?? 0.0;
    final double endIndent = this.endIndent ?? dividerTheme.endIndent ?? 0.0;

163
    return SizedBox(
164
      height: height,
165 166
      child: Center(
        child: Container(
167
          height: thickness,
168
          margin: EdgeInsetsDirectional.only(start: indent, end: endIndent),
169 170
          decoration: BoxDecoration(
            border: Border(
171
              bottom: createBorderSide(context, color: color, width: thickness),
172 173 174
            ),
          ),
        ),
Hans Muller's avatar
Hans Muller committed
175 176 177 178
      ),
    );
  }
}
jslavitz's avatar
jslavitz committed
179

180
/// A thin vertical line, with padding on either side.
jslavitz's avatar
jslavitz committed
181
///
182
/// In the Material Design language, this represents a divider. Vertical
183 184
/// dividers can be used in horizontally scrolling lists, such as a
/// [ListView] with [ListView.scrollDirection] set to [Axis.horizontal].
jslavitz's avatar
jslavitz committed
185 186 187 188
///
/// The box's total width is controlled by [width]. The appropriate
/// padding is automatically computed from the width.
///
189
/// {@tool dartpad}
190
/// This sample shows how to display a [VerticalDivider] between a purple and orange box
191 192 193 194
/// inside a [Row]. The [VerticalDivider] is 20 logical pixels in width and contains a
/// horizontally centered black line that is 1 logical pixels thick. The grey
/// line is indented by 20 logical pixels.
///
195
/// ** See code in examples/api/lib/material/divider/vertical_divider.0.dart **
196
/// {@end-tool}
197
///
jslavitz's avatar
jslavitz committed
198 199
/// See also:
///
200
///  * [ListView.separated], which can be used to generate vertical dividers.
201
///  * [Divider], which is the horizontal analog of this widget.
202
///  * <https://material.io/design/components/dividers.html>
jslavitz's avatar
jslavitz committed
203
class VerticalDivider extends StatelessWidget {
204
  /// Creates a Material Design vertical divider.
jslavitz's avatar
jslavitz committed
205
  ///
206 207
  /// The [width], [thickness], [indent], and [endIndent] must be null or
  /// non-negative.
jslavitz's avatar
jslavitz committed
208
  const VerticalDivider({
209
    super.key,
210 211 212 213
    this.width,
    this.thickness,
    this.indent,
    this.endIndent,
214
    this.color,
215 216 217
  }) : assert(width == null || width >= 0.0),
       assert(thickness == null || thickness >= 0.0),
       assert(indent == null || indent >= 0.0),
218
       assert(endIndent == null || endIndent >= 0.0);
jslavitz's avatar
jslavitz committed
219 220 221

  /// The divider's width.
  ///
222 223
  /// The divider itself is always drawn as a vertical line that is centered
  /// within the width specified by this value.
jslavitz's avatar
jslavitz committed
224
  ///
225 226
  /// If this is null, then the [DividerThemeData.space] is used. If that is
  /// also null, then this defaults to 16.0.
227
  final double? width;
jslavitz's avatar
jslavitz committed
228

229 230 231 232 233 234 235
  /// The thickness of the line drawn within the divider.
  ///
  /// A divider with a [thickness] of 0.0 is always drawn as a line with a
  /// width of exactly one device pixel.
  ///
  /// If this is null, then the [DividerThemeData.thickness] is used which
  /// defaults to 0.0.
236
  final double? thickness;
237

238
  /// The amount of empty space on top of the divider.
239 240 241
  ///
  /// If this is null, then the [DividerThemeData.indent] is used. If that is
  /// also null, then this defaults to 0.0.
242
  final double? indent;
jslavitz's avatar
jslavitz committed
243

244
  /// The amount of empty space under the divider.
245 246 247
  ///
  /// If this is null, then the [DividerThemeData.endIndent] is used. If that is
  /// also null, then this defaults to 0.0.
248
  final double? endIndent;
249

jslavitz's avatar
jslavitz committed
250 251
  /// The color to use when painting the line.
  ///
252 253
  /// If this is null, then the [DividerThemeData.color] is used. If that is
  /// also null, then [ThemeData.dividerColor] is used.
jslavitz's avatar
jslavitz committed
254
  ///
255
  /// {@tool snippet}
jslavitz's avatar
jslavitz committed
256 257
  ///
  /// ```dart
258
  /// const Divider(
jslavitz's avatar
jslavitz committed
259 260 261
  ///   color: Colors.deepOrange,
  /// )
  /// ```
262
  /// {@end-tool}
263
  final Color? color;
jslavitz's avatar
jslavitz committed
264 265 266

  @override
  Widget build(BuildContext context) {
267 268 269 270 271 272
    final DividerThemeData dividerTheme = DividerTheme.of(context);
    final double width = this.width ?? dividerTheme.space ?? 16.0;
    final double thickness = this.thickness ?? dividerTheme.thickness ?? 0.0;
    final double indent = this.indent ?? dividerTheme.indent ?? 0.0;
    final double endIndent = this.endIndent ?? dividerTheme.endIndent ?? 0.0;

jslavitz's avatar
jslavitz committed
273 274 275 276
    return SizedBox(
      width: width,
      child: Center(
        child: Container(
277
          width: thickness,
278
          margin: EdgeInsetsDirectional.only(top: indent, bottom: endIndent),
jslavitz's avatar
jslavitz committed
279 280
          decoration: BoxDecoration(
            border: Border(
281
              left: Divider.createBorderSide(context, color: color, width: thickness),
jslavitz's avatar
jslavitz committed
282 283 284 285 286 287 288
            ),
          ),
        ),
      ),
    );
  }
}