Flutter专业设计系统插件pro_design的使用

Flutter专业设计系统插件pro_design的使用

pro_design 插件帮助开发响应式的 Flutter 应用程序。

注意:此插件仍在开发中。请勿在移动设备、平板电脑和台式机或大型设备上使用相同的值。始终检查最新版本。

使用

添加依赖

请在安装前检查最新版本。

dependencies:
  flutter:
    sdk: flutter
  pro_design: ^0.0.5

导入到 Dart 代码中

import 'package:pro_design/pro_design.dart';

初始化

必须在 Widget build() 函数中调用 ProDesign.init() 并传递 context。它应该在 MaterialApp 中,而不是在其之前。

示例:

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    // 初始化 ProDesign!
    ProDesign.init(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'pro_design',
          style: TextStyle(
            color: Colors.white,
            // 设置 20 作为响应式字体大小
            fontSize: ProDesign.sp(20),
          ),
        ),
        backgroundColor: Colors.black87,
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.all(ProDesign.pt(16)),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              width: double.infinity,
              child: Text(
                // 文本将基于设备方向显示
                ProDesign.orientation == Orientation.portrait
                    ? '设备方向为纵向模式'
                    : '设备方向更改为横向模式,字体大小因此改变',
                style: TextStyle(
                  color: Colors.black87,
                  // 设置响应式字体大小,但会根据设备类型更改
                  fontSize: ProDesign.sp(16),
                ),
              ),
            ),
            SizedBox(
              // 使用屏幕垂直方向的 10%
              height: ProDesign.vertically(10),
            ),
            SizedBox(
              // 设置 250pt 响应宽度
              width: ProDesign.pt(250),
              // 设置 50pt 响应高度
              height: ProDesign.pt(50),
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.black87,
                ),
                child: Text(
                  '按钮',
                  style: TextStyle(
                    color: Colors.white,
                    // 设置 16 作为响应式字体大小
                    fontSize: ProDesign.sp(16),
                  ),
                ),
                onPressed: () {},
              ),
            ),
            SizedBox(
              // 使用屏幕垂直方向的 10%
              height: ProDesign.vertically(10),
            ),
            Container(
              alignment: Alignment.center,
              // 使用屏幕水平方向的 80%
              width: ProDesign.horizontally(80),
              // 使用屏幕垂直方向的 40%
              height: ProDesign.horizontally(40),
              color: Colors.black87,
              child: Text(
                '容器',
                style: TextStyle(
                  color: Colors.white,
                  // 设置 20 作为响应式字体大小
                  fontSize: ProDesign.sp(20),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter专业设计系统插件pro_design的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter专业设计系统插件pro_design的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


pro_design 是一个为 Flutter 应用程序提供专业设计系统的插件。它旨在帮助开发者快速实现一致且响应式的 UI 设计,尤其是在处理不同屏幕尺寸和设备时。pro_design 提供了一系列工具和方法,使得开发者可以轻松地根据设计系统来调整和应用样式、尺寸、颜色等。

以下是如何使用 pro_design 插件的基本指南:

1. 安装 pro_design 插件

首先,你需要在 pubspec.yaml 文件中添加 pro_design 依赖项:

dependencies:
  flutter:
    sdk: flutter
  pro_design: ^1.0.0  # 请使用最新的版本

然后运行 flutter pub get 来安装依赖。

2. 初始化 ProDesign

在使用 pro_design 之前,你需要在应用程序的入口点(通常是 main.dart)进行初始化。这通常涉及设置设计系统的基准尺寸(如设计稿的宽度和高度)。

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    // 初始化 ProDesign
    ProDesign.init(
      designWidth: 375,  // 设计稿的宽度
      designHeight: 812, // 设计稿的高度
    );

    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

3. 使用 ProDesign 进行响应式设计

ProDesign 提供了多种方法来帮助你在不同的屏幕尺寸上实现响应式设计。

3.1 尺寸适配

你可以使用 ProDesign 来根据屏幕尺寸自动调整 Widget 的尺寸。

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

class MyHomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ProDesign Example'),
      ),
      body: Center(
        child: Container(
          width: ProDesign.pt(200),  // 根据设计稿尺寸自动调整
          height: ProDesign.pt(100), // 根据设计稿尺寸自动调整
          color: Colors.blue,
          child: Center(
            child: Text(
              'Hello, ProDesign!',
              style: TextStyle(
                fontSize: ProDesign.pt(16), // 根据设计稿尺寸自动调整字体大小
              ),
            ),
          ),
        ),
      ),
    );
  }
}

3.2 颜色和样式

ProDesign 还可以帮助你在设计系统中统一颜色和样式。

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

class MyHomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ProDesign Example'),
      ),
      body: Center(
        child: Container(
          width: ProDesign.pt(200),
          height: ProDesign.pt(100),
          color: ProDesign.colors.primaryColor, // 使用设计系统中的颜色
          child: Center(
            child: Text(
              'Hello, ProDesign!',
              style: ProDesign.textStyles.headline, // 使用设计系统中的文本样式
            ),
          ),
        ),
      ),
    );
  }
}

3.3 响应式布局

ProDesign 提供了响应式布局工具,使得你可以根据屏幕尺寸动态调整布局。

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

class MyHomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ProDesign Example'),
      ),
      body: Center(
        child: ProResponsiveBuilder(
          builder: (context, screenSize) {
            if (screenSize == ScreenSize.small) {
              return Text('Small Screen');
            } else if (screenSize == ScreenSize.medium) {
              return Text('Medium Screen');
            } else {
              return Text('Large Screen');
            }
          },
        ),
      ),
    );
  }
}

4. 自定义设计系统

ProDesign 允许你自定义颜色、文本样式、尺寸等设计系统的元素。

ProDesign.init(
  designWidth: 375,
  designHeight: 812,
  colors: CustomColors(), // 自定义颜色
  textStyles: CustomTextStyles(), // 自定义文本样式
);

5. 其他功能

ProDesign 还提供了其他一些有用的功能,例如:

  • Spacing: 统一的间距系统。
  • Shadows: 统一的阴影效果。
  • BorderRadius: 统一的圆角半径。

6. 示例

以下是一个完整的示例,展示了如何使用 pro_design 插件:

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    ProDesign.init(
      designWidth: 375,
      designHeight: 812,
    );

    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ProDesign Example'),
      ),
      body: Center(
        child: Container(
          width: ProDesign.pt(200),
          height: ProDesign.pt(100),
          color: ProDesign.colors.primaryColor,
          child: Center(
            child: Text(
              'Hello, ProDesign!',
              style: ProDesign.textStyles.headline,
            ),
          ),
        ),
      ),
    );
  }
}
回到顶部