Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 1 | // 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 Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 5 | import 'package:flutter/material.dart'; |
| 6 | |
Greg Spencer | e3bc8ef | 2023-04-04 13:34:29 -0700 | [diff] [blame] | 7 | /// Flutter code sample for [LayoutBuilder]. |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 8 | |
Greg Spencer | e3bc8ef | 2023-04-04 13:34:29 -0700 | [diff] [blame] | 9 | void main() => runApp(const LayoutBuilderExampleApp()); |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 10 | |
Greg Spencer | e3bc8ef | 2023-04-04 13:34:29 -0700 | [diff] [blame] | 11 | class LayoutBuilderExampleApp extends StatelessWidget { |
| 12 | const LayoutBuilderExampleApp({super.key}); |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 13 | |
| 14 | @override |
| 15 | Widget build(BuildContext context) { |
| 16 | return const MaterialApp( |
Greg Spencer | e3bc8ef | 2023-04-04 13:34:29 -0700 | [diff] [blame] | 17 | home: LayoutBuilderExample(), |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 18 | ); |
| 19 | } |
| 20 | } |
| 21 | |
Greg Spencer | e3bc8ef | 2023-04-04 13:34:29 -0700 | [diff] [blame] | 22 | class LayoutBuilderExample extends StatelessWidget { |
| 23 | const LayoutBuilderExample({super.key}); |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 24 | |
| 25 | @override |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 26 | 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 Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 70 | } |