divider.dart 1.92 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 10
/// A material design divider.
///
11 12
/// A one logical pixel thick horizontal line, with padding on either
/// side. The box's total height is controlled by [height].
13 14 15 16 17 18
///
/// Dividers can be used in lists and [Drawer]s to separate content vertically.
/// To create a one-pixel divider between items in a list, consider using
/// [ListItem.divideItems], which is optimized for this case.
///
/// See also:
19
///
20
///  * [ListItem.divideItems]
21
///  * [PopupMenuDivider]
22
///  * <https://www.google.com/design/spec/components/dividers.html>
23
class Divider extends StatelessWidget {
24 25 26 27 28 29 30 31 32
  /// Creates a material design divider.
  ///
  /// The height must be at least 1.0 logical pixels.
  Divider({
    Key key,
    this.height: 16.0,
    this.indent: 0.0,
    this.color
  }) : super(key: key) {
Hans Muller's avatar
Hans Muller committed
33 34 35
    assert(height >= 1.0);
  }

36 37 38 39
  /// The divider's vertical extent.
  ///
  /// The divider itself is always drawn as one logical pixel thick horizontal
  /// line that is centered within the height specified by this value.
Hans Muller's avatar
Hans Muller committed
40
  final double height;
41 42

  /// The amount of empty space to the left of the divider.
Hans Muller's avatar
Hans Muller committed
43
  final double indent;
44 45 46

  /// The color to use when painting the line.
  ///
47 48
  /// Defaults to the current theme's divider color, given by
  /// [ThemeData.dividerColor].
Hans Muller's avatar
Hans Muller committed
49 50
  final Color color;

51
  @override
Hans Muller's avatar
Hans Muller committed
52 53 54 55
  Widget build(BuildContext context) {
    final double bottom = (height ~/ 2.0).toDouble();
    return new Container(
      height: 0.0,
56
      margin: new EdgeInsets.only(
Hans Muller's avatar
Hans Muller committed
57 58 59 60 61 62 63 64 65 66 67 68
        top: height - bottom - 1.0,
        left: indent,
        bottom: bottom
      ),
      decoration: new BoxDecoration(
        border: new Border(
          bottom: new BorderSide(color: color ?? Theme.of(context).dividerColor)
        )
      )
    );
  }
}