当前位置: 首页 > article >正文

Flutter 布局系统:构建响应式界面

Flutter 布局系统构建响应式界面掌握 Flutter 布局系统的核心概念和最佳实践。一、布局系统概述作为一名追求像素级还原的 UI 匠人我深知布局系统在 Flutter 开发中的重要性。Flutter 提供了一套强大的布局系统让我们能够创建各种复杂的界面。从简单的容器布局到复杂的响应式设计Flutter 的布局系统为我们提供了灵活的工具。二、基础布局组件1. ContainerContainer( width: 200, height: 200, decoration: BoxDecoration( color: Colors.blue, borderRadius: BorderRadius.circular(12), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.1), spreadRadius: 2, blurRadius: 4, offset: Offset(0, 2), ), ], ), padding: EdgeInsets.all(20), margin: EdgeInsets.all(10), child: Text( Container 示例, style: TextStyle(color: Colors.white, fontSize: 18), ), )2. Row 和 Column// Row 水平布局 Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.center, children: [ Icon(Icons.home, size: 30), Text(首页, style: TextStyle(fontSize: 16)), Icon(Icons.settings, size: 30), ], ) // Column 垂直布局 Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(标题, style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)), SizedBox(height: 10), Text(副标题, style: TextStyle(fontSize: 16, color: Colors.grey)), SizedBox(height: 20), ElevatedButton( onPressed: () {}, child: Text(按钮), ), ], )3. StackStack( alignment: Alignment.center, children: [ Container( width: 200, height: 200, color: Colors.blue, ), Positioned( top: 20, right: 20, child: Container( width: 40, height: 40, decoration: BoxDecoration( color: Colors.red, borderRadius: BorderRadius.circular(20), ), child: Center( child: Text(1, style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)), ), ), ), Text(Stack 示例, style: TextStyle(color: Colors.white, fontSize: 18)), ], )三、布局约束1. 约束系统// 约束示例 Container( width: 300, height: 300, color: Colors.grey[200], child: Container( width: 200, // 受父容器约束 height: 200, // 受父容器约束 color: Colors.blue, ), ) // 无约束示例 Center( child: Container( width: 200, height: 200, color: Colors.blue, ), )2. 布局算法// 布局算法示例 Column( children: [ Container( height: 100, color: Colors.red, ), Expanded( child: Container( color: Colors.green, ), ), Container( height: 100, color: Colors.blue, ), ], )四、响应式布局1. MediaQueryclass ResponsiveLayout extends StatelessWidget { override Widget build(BuildContext context) { final screenSize MediaQuery.of(context).size; final isMobile screenSize.width 768; return Scaffold( appBar: AppBar(title: Text(响应式布局)), body: isMobile ? MobileLayout() : DesktopLayout(), ); } } class MobileLayout extends StatelessWidget { override Widget build(BuildContext context) { return Column( children: [ Container(height: 100, color: Colors.blue), SizedBox(height: 20), Container(height: 100, color: Colors.green), ], ); } } class DesktopLayout extends StatelessWidget { override Widget build(BuildContext context) { return Row( children: [ Expanded(child: Container(color: Colors.blue)), SizedBox(width: 20), Expanded(child: Container(color: Colors.green)), ], ); } }2. LayoutBuilderLayoutBuilder( builder: (context, constraints) { if (constraints.maxWidth 600) { return MobileLayout(); } else if (constraints.maxWidth 1200) { return TabletLayout(); } else { return DesktopLayout(); } }, )3. AspectRatioAspectRatio( aspectRatio: 16 / 9, child: Container( color: Colors.blue, child: Center( child: Text(16:9 比例, style: TextStyle(color: Colors.white)), ), ), )五、高级布局1. CustomMultiChildLayoutclass MyCustomLayout extends StatelessWidget { override Widget build(BuildContext context) { return CustomMultiChildLayout( delegate: MyLayoutDelegate(), children: [ LayoutId( id: header, child: Container( color: Colors.blue, height: 100, child: Center(child: Text(Header)), ), ), LayoutId( id: content, child: Container( color: Colors.green, child: Center(child: Text(Content)), ), ), LayoutId( id: footer, child: Container( color: Colors.red, height: 80, child: Center(child: Text(Footer)), ), ), ], ); } } class MyLayoutDelegate extends MultiChildLayoutDelegate { override void performLayout(Size size) { final headerSize layoutChild(header, BoxConstraints.tightFor(width: size.width, height: 100)); positionChild(header, Offset(0, 0)); final footerSize layoutChild(footer, BoxConstraints.tightFor(width: size.width, height: 80)); positionChild(footer, Offset(0, size.height - footerSize.height)); final contentSize layoutChild( content, BoxConstraints.tightFor( width: size.width, height: size.height - headerSize.height - footerSize.height, ), ); positionChild(content, Offset(0, headerSize.height)); } override bool shouldRelayout(covariant MultiChildLayoutDelegate oldDelegate) { return false; } }2. FlowFlow( delegate: MyFlowDelegate(), children: List.generate( 5, (index) Container( width: 100, height: 100, color: Colors.primaries[index % Colors.primaries.length], child: Center( child: Text($index, style: TextStyle(color: Colors.white, fontSize: 24)), ), ), ), ) class MyFlowDelegate extends FlowDelegate { override void paintChildren(FlowPaintingContext context) { for (int i 0; i context.childCount; i) { context.paintChild( i, transform: Matrix4.translationValues( i * 110.0, 0.0, 0.0, ), ); } } override bool shouldRepaint(covariant FlowDelegate oldDelegate) { return false; } }六、实战案例1. 卡片布局class CardLayout extends StatelessWidget { override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text(卡片布局)), body: Padding( padding: const EdgeInsets.all(16.0), child: GridView.builder( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: MediaQuery.of(context).size.width 600 ? 3 : 2, crossAxisSpacing: 16, mainAxisSpacing: 16, childAspectRatio: 0.8, ), itemCount: 6, itemBuilder: (context, index) { return Card( elevation: 4, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( height: 120, decoration: BoxDecoration( borderRadius: BorderRadius.vertical(top: Radius.circular(12)), color: Colors.primaries[index % Colors.primaries.length], ), ), Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(卡片标题 $index, style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)), SizedBox(height: 8), Text(这是卡片内容包含一些描述信息。, style: TextStyle(color: Colors.grey)), ], ), ), ], ), ); }, ), ), ); } }2. 登录页面class LoginPage extends StatelessWidget { override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: Container( width: double.infinity, height: double.infinity, decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [Colors.blue, Colors.purple], ), ), child: Center( child: Container( width: MediaQuery.of(context).size.width 600 ? 400 : MediaQuery.of(context).size.width * 0.8, padding: EdgeInsets.all(24), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(16), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.1), spreadRadius: 5, blurRadius: 10, ), ], ), child: Column( mainAxisSize: MainAxisSize.min, children: [ Text(登录, style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)), SizedBox(height: 32), TextField( decoration: InputDecoration( labelText: 邮箱, border: OutlineInputBorder( borderRadius: BorderRadius.circular(8), ), ), ), SizedBox(height: 16), TextField( obscureText: true, decoration: InputDecoration( labelText: 密码, border: OutlineInputBorder( borderRadius: BorderRadius.circular(8), ), ), ), SizedBox(height: 24), ElevatedButton( onPressed: () {}, style: ElevatedButton.styleFrom( minimumSize: Size(double.infinity, 50), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), ), ), child: Text(登录, style: TextStyle(fontSize: 16)), ), SizedBox(height: 16), TextButton( onPressed: () {}, child: Text(忘记密码), ), ], ), ), ), ), ), ); } }七、最佳实践使用适当的布局组件根据需求选择合适的布局组件保持布局层次清晰避免过度嵌套布局组件使用 Expanded 和 Flexible合理分配空间响应式设计使用 MediaQuery 和 LayoutBuilder性能优化避免不必要的布局重建测试在不同设备和屏幕尺寸上测试布局八、常见问题1. 布局溢出// 解决水平溢出 SingleChildScrollView( scrollDirection: Axis.horizontal, child: Row( children: [ // 子组件 ], ), ) // 解决垂直溢出 SingleChildScrollView( child: Column( children: [ // 子组件 ], ), )2. 居中布局// 居中布局 Center( child: Container( width: 200, height: 200, color: Colors.blue, child: Center( child: Text(居中内容, style: TextStyle(color: Colors.white)), ), ), )3. 动态布局// 动态布局 Column( children: [ Text(标题), if (condition) Text(条件内容), for (var item in items) Text(item), ], )Flutter 布局系统是构建高质量应用的基础掌握它能够让你创建出更加灵活、响应式的界面。#flutter #layout #responsive #ui #dart

相关文章:

Flutter 布局系统:构建响应式界面

Flutter 布局系统:构建响应式界面掌握 Flutter 布局系统的核心概念和最佳实践。一、布局系统概述 作为一名追求像素级还原的 UI 匠人,我深知布局系统在 Flutter 开发中的重要性。Flutter 提供了一套强大的布局系统,让我们能够创建各种复杂的界…...

ccmusic-database开源模型教程:基于CV预训练模型迁移学习音频任务的完整路径

ccmusic-database开源模型教程:基于CV预训练模型迁移学习音频任务的完整路径 1. 项目简介 ccmusic-database是一个基于计算机视觉预训练模型的音乐流派分类系统,它巧妙地将图像处理领域的成熟技术迁移到音频分析任务中。这个开源项目使用VGG19_BN作为基…...

效率提升秘籍:借助快马平台快速构建魔鬼面具图像滤镜应用

最近在做一个有趣的个人项目——魔鬼面具在线滤镜应用。作为一个前端开发者,我发现这类图像处理应用如果从零开始搭建会花费大量时间在基础框架上,而使用InsCode(快马)平台可以快速生成项目骨架,让我能专注于核心功能的实现。下面分享下我的开…...

uosc:革命性MPV播放器UI,基于接近度智能显示界面元素

uosc:革命性MPV播放器UI,基于接近度智能显示界面元素 【免费下载链接】uosc Feature-rich minimalist proximity-based UI for MPV player. 项目地址: https://gitcode.com/gh_mirrors/uo/uosc uosc是一款为MPV播放器打造的功能丰富且极简的基于接…...

JPEGView:Windows平台轻量级图像工具的性能革命

JPEGView:Windows平台轻量级图像工具的性能革命 【免费下载链接】jpegview Fork of JPEGView by David Kleiner - fast and highly configurable viewer/editor for JPEG, BMP, PNG, WEBP, TGA, GIF and TIFF images with a minimal GUI. Basic on-the-fly image pr…...

Grimoire:终极书签管理器 - 为巫师打造的神奇知识宝库

Grimoire:终极书签管理器 - 为巫师打造的神奇知识宝库 【免费下载链接】grimoire Bookmark manager for the wizards 🧙 项目地址: https://gitcode.com/gh_mirrors/gr/grimoire Grimoire 是一款专为现代互联网巫师设计的终极书签管理器&#xff…...

用Anything to RealCharacters为游戏角色“拍照”:生成高质感真人定妆照

用Anything to RealCharacters为游戏角色"拍照":生成高质感真人定妆照 1. 引言:游戏角色的"数字摄影棚" 想象一下,你精心设计的游戏角色突然从屏幕里走出来,站在真实的摄影棚中,专业的灯光打在他…...

如何高效管理多平台网盘文件:LinkSwift直链获取工具深度解析

如何高效管理多平台网盘文件:LinkSwift直链获取工具深度解析 【免费下载链接】Online-disk-direct-link-download-assistant 一个基于 JavaScript 的网盘文件下载地址获取工具。基于【网盘直链下载助手】修改 ,支持 百度网盘 / 阿里云盘 / 中国移动云盘 …...

如何用数字记忆守护留住QQ空间的青春足迹?一份让回忆永不褪色的解决方案

如何用数字记忆守护留住QQ空间的青春足迹?一份让回忆永不褪色的解决方案 【免费下载链接】GetQzonehistory 获取QQ空间发布的历史说说 项目地址: https://gitcode.com/GitHub_Trending/ge/GetQzonehistory 当数字记忆面临消失危机 2023年的一个普通下午&…...

EdB Prepare Carefully完整教程:3步打造完美RimWorld开局体验

EdB Prepare Carefully完整教程:3步打造完美RimWorld开局体验 【免费下载链接】EdBPrepareCarefully EdB Prepare Carefully, a RimWorld mod 项目地址: https://gitcode.com/gh_mirrors/ed/EdBPrepareCarefully 还在为RimWorld随机生成的"废柴"殖…...

PixEz-flutter网络优化实战:3个技巧打造流畅的二次元内容体验

PixEz-flutter网络优化实战:3个技巧打造流畅的二次元内容体验 【免费下载链接】pixez-flutter 一个支持免代理直连及查看动图的第三方Pixiv flutter客户端 项目地址: https://gitcode.com/gh_mirrors/pi/pixez-flutter 作为一款支持免代理直连的第三方Pixiv客…...

到 2030 年的能力-AI

近年来,人工智能进步的关键投入(算力、算法改进和数据)呈指数级增长,新的推理时扩展(inferencetime scaling)方法正进一步提高模型的能力,甚至在模型完成训练之后。如果这些趋势继续下去&#x…...

告别手动标注!用MedCLIP-SAM+BiomedCLIP实现医学图像的“一句话分割”

医学图像智能分割革命:当自然语言指令遇上MedCLIP-SAM 在放射科医生的日常工作中,最耗时的往往不是诊断本身,而是那些繁琐的图像标注工作。想象一下,当一位胸外科医生需要从数百张CT片中定位所有肺结节时,传统方法要求…...

如何让老款RTX显卡免费获得AMD FSR3帧生成技术?5分钟完整解决方案

如何让老款RTX显卡免费获得AMD FSR3帧生成技术?5分钟完整解决方案 【免费下载链接】dlssg-to-fsr3 Adds AMD FSR 3 Frame Generation to games by replacing Nvidia DLSS Frame Generation (nvngx_dlssg). 项目地址: https://gitcode.com/gh_mirrors/dl/dlssg-to-…...

WinUtil:Windows系统管理工具让用户实现高效系统维护与优化

WinUtil:Windows系统管理工具让用户实现高效系统维护与优化 【免费下载链接】winutil Chris Titus Techs Windows Utility - Install Programs, Tweaks, Fixes, and Updates 项目地址: https://gitcode.com/GitHub_Trending/wi/winutil 问题诊断:…...

如何用Dism++解决Windows系统维护难题:高效全面的实用指南

如何用Dism解决Windows系统维护难题:高效全面的实用指南 【免费下载链接】Dism-Multi-language Dism Multi-language Support & BUG Report 项目地址: https://gitcode.com/gh_mirrors/di/Dism-Multi-language Windows系统使用一段时间后,往往…...

如何在Windows中快速读取Linux分区?Ext2Read完整教程指南

如何在Windows中快速读取Linux分区?Ext2Read完整教程指南 【免费下载链接】ext2read A Windows Application to read and copy Ext2/Ext3/Ext4 (With LVM) Partitions from Windows. 项目地址: https://gitcode.com/gh_mirrors/ex/ext2read 你是否曾经遇到过…...

League Akari:英雄联盟玩家的终极自动化工具箱完整指南

League Akari:英雄联盟玩家的终极自动化工具箱完整指南 【免费下载链接】League-Toolkit An all-in-one toolkit for LeagueClient. Gathering power 🚀. 项目地址: https://gitcode.com/gh_mirrors/le/League-Toolkit League Akari是一款专为《英…...

新手避坑指南:当npm报错128时,如何用快马AI轻松完成第一个项目

最近在帮朋友入门Node.js开发时,发现很多新手卡在环境配置这一步就放弃了。特别是遇到npm error code 128这种报错时,往往连错误说明都看不懂。今天分享一个用InsCode(快马)平台快速搭建第一个Node.js项目的避坑指南,特别适合零基础开发者。 …...

告别金融数据获取难题:mootdx打造一站式通达信数据解决方案

告别金融数据获取难题:mootdx打造一站式通达信数据解决方案 【免费下载链接】mootdx 通达信数据读取的一个简便使用封装 项目地址: https://gitcode.com/GitHub_Trending/mo/mootdx 在金融数据分析和量化交易领域,获取高质量、实时的市场数据一直…...

AI开发AI:基于快马平台多模型能力深度打造旗博士口播智能体

AI开发AI:基于快马平台多模型能力深度打造旗博士口播智能体 最近在做一个挺有意思的项目——旗博士口播智能体。这个项目本身是个AI应用,但更有趣的是,整个开发过程都借助了AI来辅助完成。这种"用AI开发AI"的体验,让我…...

Wan2.2-TI2V-5B:消费级GPU上的720P视频生成革命

Wan2.2-TI2V-5B:消费级GPU上的720P视频生成革命 【免费下载链接】Wan2.2-TI2V-5B Wan2.2-TI2V-5B是一款开源的先进视频生成模型,基于创新的混合专家架构(MoE)设计,显著提升了视频生成的质量与效率。该模型支持文本生成…...

Java面试八股文总结(金三银四版)建议收藏。

今年的行情,让招聘面试变得雪上加霜。已经有不少大厂,如腾讯、字节跳动的招聘名额明显减少,面试门槛却一再拔高,如果不用心准备,很可能就被面试官怼得哑口无言,甚至失去了难得的机会。 现如今,…...

当网盘变成龟速:如何优雅地找回你的下载自由?

当网盘变成龟速:如何优雅地找回你的下载自由? 【免费下载链接】Online-disk-direct-link-download-assistant 一个基于 JavaScript 的网盘文件下载地址获取工具。基于【网盘直链下载助手】修改 ,支持 百度网盘 / 阿里云盘 / 中国移动云盘 / 天…...

SDMatte Web服务灰度流量控制:基于用户ID哈希的AB测试分流规则

SDMatte Web服务灰度流量控制:基于用户ID哈希的AB测试分流规则 1. 引言 在AI服务实际落地过程中,灰度发布和AB测试是验证新功能效果的关键手段。对于SDMatte这样的专业级图像抠图服务,如何科学地分配流量到不同版本,直接影响着功…...

数字电路设计终极指南:用Logisim-Evolution从零搭建你的第一个逻辑系统

数字电路设计终极指南:用Logisim-Evolution从零搭建你的第一个逻辑系统 【免费下载链接】logisim-evolution Digital logic design tool and simulator 项目地址: https://gitcode.com/gh_mirrors/lo/logisim-evolution 数字电路设计与仿真是电子工程和计算机…...

3个维度解析Helix Toolkit:跨平台3D渲染框架的技术突破与商业价值

3个维度解析Helix Toolkit:跨平台3D渲染框架的技术突破与商业价值 【免费下载链接】helix-toolkit Helix Toolkit is a collection of 3D components for .NET. 项目地址: https://gitcode.com/gh_mirrors/he/helix-toolkit Helix Toolkit是一套功能完备的.N…...

如何将网页转化为可编辑设计稿?3大核心场景与实现方案

如何将网页转化为可编辑设计稿?3大核心场景与实现方案 【免费下载链接】figma-html Convert any website to editable Figma designs 项目地址: https://gitcode.com/gh_mirrors/fi/figma-html 你是否曾遇到过看到优秀网页设计却无法直接复用的困境&#xff…...

LeetCode 热题100(JAVA)

LeetCode 热题100(JAVA) 哈希 1. 两数之和 给定一个整数数组 nums和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那两个整数, 并返回它们的数组下标。你可以假设每种输入只会对应一个答案,并且你不…...

如何让你的10美元鼠标秒变Mac神器?Mac Mouse Fix终极指南

如何让你的10美元鼠标秒变Mac神器?Mac Mouse Fix终极指南 【免费下载链接】mac-mouse-fix Mac Mouse Fix - Make Your $10 Mouse Better Than an Apple Trackpad! 项目地址: https://gitcode.com/GitHub_Trending/ma/mac-mouse-fix 还在为Mac上的第三方鼠标发…...