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

Flutter Material 3设计语言详解

在 Flutter 中,Material 3(也称为 Material You)是 Google 最新的设计语言,它在 Material 2 的基础上进行了重大升级,提供了更个性化、更现代的设计体验。

Material 3 的主要特性

1. 动态色彩 (Dynamic Color)

Material 3 的核心特性之一是从用户壁纸中提取颜色主题。

import 'package:flutter/material.dart';void main() {runApp(const MyApp());
}class MyApp extends StatelessWidget {const MyApp({super.key});@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Material 3 Demo',theme: ThemeData(useMaterial3: true, // 启用 Material 3colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple, // 从种子颜色生成完整配色),),home: const MyHomePage(),);}
}

2. 更新的组件样式

按钮 (Buttons)
class ButtonExamples extends StatelessWidget {@overrideWidget build(BuildContext context) {return Column(children: [FilledButton(onPressed: () {},child: const Text('Filled Button'),),const SizedBox(height: 10),FilledButton.tonal(onPressed: () {},child: const Text('Tonal Button'),),const SizedBox(height: 10),OutlinedButton(onPressed: () {},child: const Text('Outlined Button'),),const SizedBox(height: 10),TextButton(onPressed: () {},child: const Text('Text Button'),),],);}
}
导航栏 (Navigation Bar)
class BottomNavigationExample extends StatefulWidget {const BottomNavigationExample({super.key});@overrideState<BottomNavigationExample> createState() =>_BottomNavigationExampleState();
}class _BottomNavigationExampleState extends State<BottomNavigationExample> {int _selectedIndex = 0;void _onItemTapped(int index) {setState(() {_selectedIndex = index;});}@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text('Material 3 Navigation'),),body: Center(child: Text('Selected Page: $_selectedIndex'),),bottomNavigationBar: NavigationBar(selectedIndex: _selectedIndex,onDestinationSelected: _onItemTapped,destinations: const [NavigationDestination(icon: Icon(Icons.home),label: 'Home',),NavigationDestination(icon: Icon(Icons.business),label: 'Business',),NavigationDestination(icon: Icon(Icons.school),label: 'School',),],),);}
}

3. 完整的 Material 3 主题配置

ThemeData getMaterial3Theme(bool isDark) {return ThemeData(useMaterial3: true,colorScheme: isDark ? ColorScheme.dark(primary: Colors.blue,secondary: Colors.cyan,background: Colors.grey[900]!,): ColorScheme.light(primary: Colors.blue,secondary: Colors.cyan,background: Colors.white,),// 或者使用 fromSeed 自动生成// colorScheme: ColorScheme.fromSeed(//   seedColor: Colors.blue,//   brightness: isDark ? Brightness.dark : Brightness.light,// ),// 文字主题textTheme: TextTheme(displayLarge: TextStyle(fontSize: 57,fontWeight: FontWeight.w400,color: isDark ? Colors.white : Colors.black87,),titleLarge: TextStyle(fontSize: 22,fontWeight: FontWeight.w400,color: isDark ? Colors.white : Colors.black87,),bodyLarge: TextStyle(fontSize: 16,fontWeight: FontWeight.w400,color: isDark ? Colors.white70 : Colors.black54,),),// 组件主题cardTheme: CardTheme(elevation: 1,shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12),),),elevatedButtonTheme: ElevatedButtonThemeData(style: ElevatedButton.styleFrom(padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20),),),),);
}

4. 响应式设计改进

class AdaptiveLayoutExample extends StatelessWidget {const AdaptiveLayoutExample({super.key});@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text('Adaptive Layout'),),body: CustomScrollView(slivers: [// 大屏幕顶部的应用栏SliverAppBar.large(title: const Text('Large App Bar'),),// 中等屏幕应用栏SliverAppBar.medium(title: const Text('Medium App Bar'),),// 常规应用栏SliverAppBar(title: const Text('Regular App Bar'),),// 内容区域SliverList(delegate: SliverChildBuilderDelegate((context, index) => ListTile(leading: Icon(Icons.circle,color: Theme.of(context).colorScheme.primary,),title: Text('Item $index'),subtitle: Text('Subtitle for item $index'),trailing: IconButton(icon: const Icon(Icons.more_vert),onPressed: () {},),),childCount: 20,),),],),);}
}

启用 Material 3 的步骤

  1. 在 ThemeData 中设置 useMaterial3

MaterialApp(theme: ThemeData(useMaterial3: true),
)
  1. 配置颜色方案

// 方法1: 从种子颜色生成
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue)// 方法2: 自定义完整配色
colorScheme: ColorScheme(primary: Colors.blue,secondary: Colors.cyan,// ... 其他颜色
)
  1. 更新组件使用新的 Widget

    • 使用 NavigationBar 替代 BottomNavigationBar

    • 使用 FilledButton 替代已弃用的按钮变体

    • 使用 SegmentedButton 进行分段选择

http://www.dtcms.com/a/609353.html

相关文章:

  • 天猫魔盒M19_晶晨S912H当贝桌面线刷机包_adb开启
  • 长沙seo优化排名东营优化网站
  • Python 编程实战 · 实用工具与库 — Flask 基础入门
  • supOS工厂操作系统 | 像“拼乐高”一样做数据分析
  • 青岛营销型网站推广wordpress doc导入
  • upload-labs(1-13)(配合源码分析)
  • Kubernetes-架构安装
  • 【剑斩OFFER】算法的暴力美学——二维前缀和
  • 网站开发教程全集哪些网站做的好看
  • 2025IPTV 源码优化版实测:双架构兼容 + 可视化运维
  • 建设一个网站步骤揭阳专业网站建设
  • ftp下的内部网站建设竞价培训课程
  • 技术观察 | 语音增强技术迎来新突破!TFCM模型如何攻克“保真”与“降噪”的难题?
  • FPGA系统架构设计实践5_IP的封装优化
  • UDP服务端绑定INADDR_ANY后,客户端该用什么IP访问?
  • 不同传感器前中后融合方案简介
  • 《C++在LLM系统中的核心赋能与技术深耕》
  • sward V2.1.5 版本发布,支持文档导出为html\PDF,社区版新增多种账号集成与认证
  • 东莞建站网站模板怎么做电脑网站后台
  • 物联网赋能互联网医院:构建智慧医疗新生态
  • node.js+npm的环境配置以及添加镜像(保姆级教程)
  • Java 大视界 -- 基于 Java 的大数据联邦学习在跨行业数据协同创新中的实践突破
  • 企业做网站电话约见客户的对话北京网站建设 一流
  • Android Studio + Gemini 移动开发领域的一次范式转移
  • 基于多源政策信号解析与量化因子的“12月降息预期降温”重构及黄金敏感性分析
  • LinuxC语言文件i/o笔记(第十七天)
  • Spring AI Evaluation Testing(评估测试)
  • 太仓营销型网站建设四平网站建设怎么选
  • 记一次慢接口优化:载体详情页从90秒到800毫秒的性能提升实战
  • 微软Visual Studio 2026正式登场,AI融入开发核心操作体验更流畅