blob: b0254f7ed18bbf3d209127ac89402d3da8e54863 [file] [log] [blame]
Greg Spencer33403bd2021-08-25 09:45:12 -07001// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Greg Spencer33403bd2021-08-25 09:45:12 -07005import 'package:flutter/material.dart';
6
Greg Spencere3bc8ef2023-04-04 13:34:29 -07007/// Flutter code sample for [LayoutBuilder].
Greg Spencer33403bd2021-08-25 09:45:12 -07008
Greg Spencere3bc8ef2023-04-04 13:34:29 -07009void main() => runApp(const LayoutBuilderExampleApp());
Greg Spencer33403bd2021-08-25 09:45:12 -070010
Greg Spencere3bc8ef2023-04-04 13:34:29 -070011class LayoutBuilderExampleApp extends StatelessWidget {
12 const LayoutBuilderExampleApp({super.key});
Greg Spencer33403bd2021-08-25 09:45:12 -070013
14 @override
15 Widget build(BuildContext context) {
16 return const MaterialApp(
Greg Spencere3bc8ef2023-04-04 13:34:29 -070017 home: LayoutBuilderExample(),
Greg Spencer33403bd2021-08-25 09:45:12 -070018 );
19 }
20}
21
Greg Spencere3bc8ef2023-04-04 13:34:29 -070022class LayoutBuilderExample extends StatelessWidget {
23 const LayoutBuilderExample({super.key});
Greg Spencer33403bd2021-08-25 09:45:12 -070024
25 @override
Greg Spencer33403bd2021-08-25 09:45:12 -070026 Widget build(BuildContext context) {
27 return Scaffold(
28 appBar: AppBar(title: const Text('LayoutBuilder Example')),
29 body: LayoutBuilder(
30 builder: (BuildContext context, BoxConstraints constraints) {
31 if (constraints.maxWidth > 600) {
32 return _buildWideContainers();
33 } else {
34 return _buildNormalContainer();
35 }
36 },
37 ),
38 );
39 }
40
41 Widget _buildNormalContainer() {
42 return Center(
43 child: Container(
44 height: 100.0,
45 width: 100.0,
46 color: Colors.red,
47 ),
48 );
49 }
50
51 Widget _buildWideContainers() {
52 return Center(
53 child: Row(
54 mainAxisAlignment: MainAxisAlignment.spaceEvenly,
55 children: <Widget>[
56 Container(
57 height: 100.0,
58 width: 100.0,
59 color: Colors.red,
60 ),
61 Container(
62 height: 100.0,
63 width: 100.0,
64 color: Colors.yellow,
65 ),
66 ],
67 ),
68 );
69 }
Greg Spencer33403bd2021-08-25 09:45:12 -070070}