Created
March 10, 2021 08:13
-
-
Save kaboc/adfcda77eacb69b9bbace1164a76b33e to your computer and use it in GitHub Desktop.
FittedBox fitting content to screen width.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| void main() => runApp(MyApp()); | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| theme: ThemeData.from( | |
| colorScheme: ColorScheme.light(primary: Colors.green), | |
| ), | |
| home: MyHomePage(), | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatelessWidget { | |
| const MyHomePage(); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| body: SafeArea( | |
| child: SizedBox.expand( | |
| child: SingleChildScrollView( | |
| child: FittedBox( | |
| child: SizedBox( | |
| width: 400.0, | |
| child: const _Body(), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class _Body extends StatelessWidget { | |
| const _Body(); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Column( | |
| children: [ | |
| SizedBox( | |
| width: double.infinity, | |
| height: 250.0, | |
| child: FittedBox( | |
| child: Image.network( | |
| 'https://picsum.photos/id/824/400/250', | |
| ), | |
| ), | |
| ), | |
| Padding( | |
| padding: const EdgeInsets.all(24.0), | |
| child: Column( | |
| children: [ | |
| const Text( | |
| 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed ' | |
| 'do eiusmod tempor incididunt ut labore et dolore magna ' | |
| 'aliqua. Ut enim ad minim veniam, quis nostrud exercitation ' | |
| 'ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis ' | |
| 'aute irure dolor in reprehenderit in voluptate velit esse ' | |
| 'cillum dolore eu fugiat nulla pariatur. Excepteur sint ' | |
| 'occaecat cupidatat non proident, sunt in culpa qui officia ' | |
| 'deserunt mollit anim id est laborum.', | |
| ), | |
| const SizedBox(height: 24.0), | |
| Row( | |
| children: [ | |
| Expanded( | |
| child: _Button( | |
| icon: Icons.thumb_up, | |
| label: 'Good', | |
| onPressed: () => null, | |
| ), | |
| ), | |
| const SizedBox(width: 24.0), | |
| Expanded( | |
| child: _Button( | |
| icon: Icons.thumb_down, | |
| label: 'Bad', | |
| onPressed: () => null, | |
| ), | |
| ), | |
| ], | |
| ), | |
| const SizedBox(height: 24.0), | |
| Row( | |
| children: [ | |
| Expanded( | |
| child: _Button( | |
| icon: Icons.star, | |
| label: 'Like', | |
| onPressed: () => null, | |
| ), | |
| ), | |
| const SizedBox(width: 24.0), | |
| Expanded( | |
| child: _Button( | |
| icon: Icons.settings, | |
| label: 'Settings', | |
| onPressed: () => null, | |
| ), | |
| ), | |
| ], | |
| ), | |
| ], | |
| ), | |
| ), | |
| ], | |
| ); | |
| } | |
| } | |
| class _Button extends StatelessWidget { | |
| const _Button({ | |
| required this.icon, | |
| required this.label, | |
| required this.onPressed, | |
| }); | |
| final IconData icon; | |
| final String label; | |
| final VoidCallback onPressed; | |
| @override | |
| Widget build(BuildContext context) { | |
| return AspectRatio( | |
| aspectRatio: 1.0, | |
| child: ElevatedButton( | |
| onPressed: onPressed, | |
| child: Center( | |
| child: Padding( | |
| padding: const EdgeInsets.all(40.0), | |
| child: SizedBox.expand( | |
| child: FittedBox( | |
| child: Column( | |
| children: [ | |
| Icon(icon), | |
| const SizedBox(height: 4.0), | |
| Text(label, style: TextStyle(fontSize: 8.0)), | |
| ], | |
| ), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment