divider.dart 2.21 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 logical pixel thick horizontal line, with padding on either
10 11 12
/// side.
///
/// In the material design language, this represents a divider.
13 14 15 16 17
///
/// 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.
///
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 24
///  * [PopupMenuDivider], which is the equivalent but for popup menus.
///  * [ListItem.divideItems], another approach to dividing widgets in a list.
25
///  * <https://material.google.com/components/dividers.html>
26
class Divider extends StatelessWidget {
27 28 29 30 31 32 33 34 35
  /// 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
36 37 38
    assert(height >= 1.0);
  }

39 40 41 42
  /// 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
43
  final double height;
44 45

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

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

60
  @override
Hans Muller's avatar
Hans Muller committed
61 62 63 64
  Widget build(BuildContext context) {
    final double bottom = (height ~/ 2.0).toDouble();
    return new Container(
      height: 0.0,
65
      margin: new EdgeInsets.only(
Hans Muller's avatar
Hans Muller committed
66 67 68 69 70 71 72 73 74 75 76 77
        top: height - bottom - 1.0,
        left: indent,
        bottom: bottom
      ),
      decoration: new BoxDecoration(
        border: new Border(
          bottom: new BorderSide(color: color ?? Theme.of(context).dividerColor)
        )
      )
    );
  }
}