spacer.dart 2.16 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8 9 10 11 12 13 14 15 16 17 18
import 'package:flutter/rendering.dart';

import 'basic.dart';
import 'framework.dart';

/// Spacer creates an adjustable, empty spacer that can be used to tune the
/// spacing between widgets in a [Flex] container, like [Row] or [Column].
///
/// The [Spacer] widget will take up any available space, so setting the
/// [Flex.mainAxisAlignment] on a flex container that contains a [Spacer] to
/// [MainAxisAlignment.spaceAround], [MainAxisAlignment.spaceBetween], or
/// [MainAxisAlignment.spaceEvenly] will not have any visible effect: the
Ian Hickson's avatar
Ian Hickson committed
19 20
/// [Spacer] has taken up all of the additional space, therefore there is none
/// left to redistribute.
21
///
22
/// {@tool snippet}
23 24
///
/// ```dart
25
/// Row(
26
///   children: <Widget>[
27 28 29
///     Text('Begin'),
///     Spacer(), // Defaults to a flex of one.
///     Text('Middle'),
30
///     // Gives twice the space between Middle and End than Begin and Middle.
31 32
///     Spacer(flex: 2),
///     Text('End'),
33 34 35
///   ],
/// )
/// ```
36
/// {@end-tool}
37
///
38 39
/// {@youtube 560 315 https://www.youtube.com/watch?v=7FJgd7QN1zI}
///
40 41 42 43 44 45 46 47 48
/// See also:
///
///  * [Row] and [Column], which are the most common containers to use a Spacer
///    in.
///  * [SizedBox], to create a box with a specific size and an optional child.
class Spacer extends StatelessWidget {
  /// Creates a flexible space to insert into a [Flexible] widget.
  ///
  /// The [flex] parameter may not be null or less than one.
49
  const Spacer({Key key, this.flex = 1})
50 51 52
    : assert(flex != null),
      assert(flex > 0),
      super(key: key);
53 54 55 56 57 58 59 60 61 62 63 64

  /// The flex factor to use in determining how much space to take up.
  ///
  /// The amount of space the [Spacer] can occupy in the main axis is determined
  /// by dividing the free space proportionately, after placing the inflexible
  /// children, according to the flex factors of the flexible children.
  ///
  /// Defaults to one.
  final int flex;

  @override
  Widget build(BuildContext context) {
65
    return Expanded(
66
      flex: flex,
Ian Hickson's avatar
Ian Hickson committed
67
      child: const SizedBox.shrink(),
68 69
    );
  }
70
}