Flutter 中区分 Material 和 Cupertino_Text

介绍

在 Android 和 iOS 平台上创建类似原生的体验对于移动应用程序开发至关重要。Flutter 通过单一代码库实现了这一点,但了解不同的设计语言(Android 的 Material Design 和 iOS 的 Cupertino)至关重要。在本文中,我们将探讨 Flutter 中 Material 和 Cupertino 之间的差异,以及如何实现它们以在每个平台上提供真实、无缝的用户体验。

定义

Material Design是 Google 开发的一种设计语言,专注于为 Android 应用程序创建现代、响应式且具有视觉吸引力的用户界面。它提供了布局、排版、颜色、图标和动画的指南,确保 Android 应用程序的外观和感觉一致。Flutter 中的 Material Design 组件可通过该包获得material.dart

Cupertino Design是 Apple 为 iOS 应用程序创建的一种设计语言。它强调干净、简约的美感,注重细节,提供直观且用户友好的体验。Cupertino Design 提供自己的一组组件、图标和交互,确保 iOS 应用程序之间的一致性。在 Flutter 中,可以通过包访问 Cupertino 组件cupertino.dart

差异

Material Design 和 Cupertino Design 在视觉外观、组件、交互模式等方面有着明显的差异,体现了 Google 和 Apple 独特的设计理念。

  1. 视觉外观:Material Design 使用大胆的颜色、阴影和深度,为 UI 元素提供“纸张般”的外观。另一方面,库比蒂诺设计专注于极简主义,具有微妙的色彩、扁平的元素和更少的阴影。
  2. 组件:Material Design 和 Cupertino Design 提供针对各自平台量身定制的不同组件集。例如,Material Design 有“浮动操作按钮”和“底部导航栏”,而 Cupertino Design 使用“选项卡栏”并且缺少与浮动操作按钮等效的工具。按钮、对话框和滑块等组件在每个平台上也有不同的外观和动画。
  3. 交互模式:两种设计语言具有不同的交互模式和导航风格。Material Design 经常利用侧抽屉并强调运动和过渡。Cupertino Design 更喜欢基于选项卡的导航,并专注于直接操作,屏幕元素响应用户手势,例如滑动和捏合。
  4. 版式和图标:Material Design 使用 Roboto 字体系列和 Material 图标,而 Cupertino Design 使用 San Francisco 字体系列和 SF Symbols。这些印刷和图像选择有助于每个平台的整体外观和感觉。

尽管存在这些差异,但这两个设计系统都旨在在各自的平台上提供一致且直观的用户体验。通过在 Flutter 中理解和实现 Material 和 Cupertino 设计原则,开发人员可以为其 Android 和 iOS 应用程序提供更真实且特定于平台的 UI。

例子

我将通过两个简单的示例来演示 Flutter 中 Material 和 Cupertino 设计语言之间的差异:按钮和警报对话框。

材料

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Material Example')),
      body: Center(
        child: ElevatedButton(
          onPressed: () => showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text('Material Alert Dialog'),
                content: Text('This is an example of a Material alert dialog.'),
                actions: [
                  TextButton(
                    child: Text('OK'),
                    onPressed: () => Navigator.of(context).pop(),
                  ),
                ],
              );
            },
          ),
          child: Text('Show Material Alert Dialog'),
        ),
      ),
    );
  }
}


库比蒂诺:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(CupertinoApp(home: MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text('Cupertino Example'),
      ),
      child: Center(
        child: CupertinoButton(
          onPressed: () => showCupertinoDialog(
            context: context,
            builder: (BuildContext context) {
              return CupertinoAlertDialog(
                title: Text('Cupertino Alert Dialog'),
                content: Text('This is an example of a Cupertino alert dialog.'),
                actions: [
                  CupertinoDialogAction(
                    child: Text('OK'),
                    onPressed: () => Navigator.of(context).pop(),
                  ),
                ],
              );
            },
          ),
          child: Text('Show Cupertino Alert Dialog'),
          color: CupertinoColors.activeBlue,
        ),
      ),
    );
  }
}


结论

总之,Material 和 Cupertino 设计语言分别是在 Android 和 iOS 平台上创建类似原生体验的基础。它们都有自己独特的视觉外观、组件、交互模式、版式和图标,反映了谷歌和苹果的设计理念。在 Flutter 中理解和实现这些设计语言对于创建特定于平台的用户界面以及在两个平台上提供真实且无缝的用户体验至关重要。material.dart通过利用来自包的组件cupertino.dart,开发人员可以有效地接受 Material 和 Cupertino 的独特设计原则,确保他们的应用程序在 Android 和 iOS 设备上都感觉自在。