button_bar.dart 2.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 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 'button.dart';
import 'dialog.dart';
import 'flat_button.dart';
import 'raised_button.dart';

/// A horizontal arrangement of buttons.
///
/// Places the buttons horizontally according to the padding in the current
/// [ButtonTheme].
///
/// Used by [Dialog] to arrange the actions at the bottom of the dialog.
///
/// See also:
///
21 22 23 24 25
///  * [RaisedButton], a kind of button.
///  * [FlatButton], another kind of button.
///  * [Card], at the bottom of which it is common to place a [ButtonBar].
///  * [Dialog], which uses a [ButtonBar] for its actions.
///  * [ButtonTheme], which configures the [ButtonBar].
26 27 28 29
class ButtonBar extends StatelessWidget {
  /// Creates a button bar.
  ///
  /// The alignment argument defaults to [MainAxisAlignment.end].
30
  const ButtonBar({
31 32
    Key key,
    this.alignment: MainAxisAlignment.end,
33
    this.mainAxisSize: MainAxisSize.max,
34
    this.children: const <Widget>[],
35 36 37 38 39
  }) : super(key: key);

  /// How the children should be placed along the horizontal axis.
  final MainAxisAlignment alignment;

40 41 42
  /// How much horizontal space is available. See [Row.mainAxisSize].
  final MainAxisSize mainAxisSize;

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  /// The buttons to arrange horizontally.
  ///
  /// Typically [RaisedButton] or [FlatButton] widgets.
  final List<Widget> children;

  @override
  Widget build(BuildContext context) {
    // We divide by 4.0 because we want half of the average of the left and right padding.
    final double paddingUnit = ButtonTheme.of(context).padding.horizontal / 4.0;
    return new Padding(
      padding: new EdgeInsets.symmetric(
        vertical: 2.0 * paddingUnit,
        horizontal: paddingUnit
      ),
      child: new Row(
        mainAxisAlignment: alignment,
59
        mainAxisSize: mainAxisSize,
60
        children: children.map<Widget>((Widget child) {
61 62 63 64 65 66 67 68 69
          return new Padding(
            padding: new EdgeInsets.symmetric(horizontal: paddingUnit),
            child: child
          );
        }).toList()
      )
    );
  }
}