divider.dart 2.4 KB
Newer Older
Hans Muller's avatar
Hans Muller committed
1 2 3 4 5 6 7 8
// 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.

import 'package:flutter/widgets.dart';

import 'theme.dart';

9
/// A one device pixel thick horizontal line, with padding on either
10 11 12
/// side.
///
/// In the material design language, this represents a divider.
13
///
14 15
/// Dividers can be used in lists, [Drawer]s, and elsewhere to separate content
/// vertically. To create a one-pixel divider between items in a list, consider
Ian Hickson's avatar
Ian Hickson committed
16
/// using [ListTile.divideTiles], which is optimized for this case.
17
///
18 19 20
/// The box's total height is controlled by [height]. The appropriate padding is
/// automatically computed from the height.
///
21
/// See also:
22
///
23
///  * [PopupMenuDivider], which is the equivalent but for popup menus.
24
///  * [ListTile.divideTiles], another approach to dividing widgets in a list.
25
///  * <https://material.google.com/components/dividers.html>
26
class Divider extends StatelessWidget {
27 28
  /// Creates a material design divider.
  ///
29
  /// The height must be positive.
30
  const Divider({
31 32 33 34
    Key key,
    this.height: 16.0,
    this.indent: 0.0,
    this.color
35
  }) : assert(height >= 0.0),
36
       super(key: key);
Hans Muller's avatar
Hans Muller committed
37

38 39
  /// The divider's vertical extent.
  ///
40
  /// The divider itself is always drawn as one device pixel thick horizontal
41
  /// line that is centered within the height specified by this value.
42 43 44
  ///
  /// A divider with a height of 0.0 is always drawn as a line with a height of
  /// exactly one device pixel, without any padding around it.
Hans Muller's avatar
Hans Muller committed
45
  final double height;
46 47

  /// The amount of empty space to the left of the divider.
Hans Muller's avatar
Hans Muller committed
48
  final double indent;
49 50 51

  /// The color to use when painting the line.
  ///
52 53
  /// Defaults to the current theme's divider color, given by
  /// [ThemeData.dividerColor].
54 55 56
  ///
  /// ```dart
  ///  new Divider(
57
  ///    color: Colors.deepOrange,
58 59
  ///  ),
  /// ```
Hans Muller's avatar
Hans Muller committed
60 61
  final Color color;

62
  @override
Hans Muller's avatar
Hans Muller committed
63
  Widget build(BuildContext context) {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    return new SizedBox(
      height: height,
      child: new Center(
        child: new Container(
          height: 0.0,
          margin: new EdgeInsets.only(left: indent),
          decoration: new BoxDecoration(
            border: new Border(
              bottom: new BorderSide(
                color: color ?? Theme.of(context).dividerColor,
                width: 0.0,
              ),
            ),
          ),
        ),
Hans Muller's avatar
Hans Muller committed
79 80 81 82
      ),
    );
  }
}