Flutter设计系统插件senior_design_system的潜在功能使用

发布于 1周前 作者 itying888 来自 Flutter

Flutter设计系统插件senior_design_system的潜在功能使用

Senior Design System

Senior Design System 是一个用于Flutter移动应用的组件实现项目。它提供了一系列可以在应用程序界面中使用的组件。

首次使用步骤

要将Senior Design System添加到您的应用程序中,您可以直接使用我们在Pub.dev上发布的包。您可以通过在pubspec.yaml文件中添加依赖项或使用命令flutter pub add senior_design_system来添加包。

我们还有一个用于我们的tokens的包,也推荐添加到项目中。您也可以在Pub.dev上找到它作为senior_design_tokens

如何使用

要在您的项目中使用我们的组件,首先需要导入我们的包和tokens包。

import 'package:senior_design_system/senior_design_system.dart';
import 'package:senior_design_tokens/senior_design_tokens.dart';

接下来,在您的widget树的最高点添加SeniorDesignSystem widget。这个widget会为应用程序中的组件添加必要的配置,并提供Senior Design System组件自己的主题管理。

void main() {
    runApp(
        SeniorDesignSystem(
            theme: SENIOR_LIGHT_THEME, // 使用默认的浅色主题
            child: MyApp(),
        ),
    );
}

现在,您只需在需要的地方添加所需的组件即可!下面是一个示例,我们将SeniorButton组件添加到一个名为Panel的通用组件中。

Panel(
    title: 'Primary button',
    child: SeniorButton(
        label: 'Salvar', // 按钮标签
        onPressed: () => _showSnackbar(
            context: context,
            message: 'Clicou sobre o botão Salvar!', // 点击按钮后显示的消息
        ),
        disabled: _isDisabled, // 是否禁用按钮
        fullLength: true, // 是否全宽
        size: SeniorButtonSize.big, // 按钮大小
        type: SeniorButtonType.primary, // 按钮类型
    ),
),

完整示例Demo

以下是完整的示例代码,展示了如何使用senior_design_system中的多个组件:

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
import 'package:senior_design_system/senior_design_system.dart';
import 'package:senior_design_tokens/senior_design_tokens.dart';

// 这里省略了具体的页面组件实现,例如IconExample(), BadgeExemple()等

void main() {
  runApp(
    SeniorDesignSystem(
      theme: SENIOR_LIGHT_THEME, // 使用默认的浅色主题
      child: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Senior Design System Showcase',
      home: HomePage(),
      theme: Provider.of<ThemeRepository>(context).theme.themeData,
    );
  }
}

class HomePage extends StatefulWidget {
  [@override](/user/override)
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  final PageController _pageController = PageController();
  NotificationMessage? notificationMessage;
  int tabIndex = 0;

  void updateMessageNotificationState() {
    setState(() {
      notificationMessage = NotificationMessage(
        message: 'Tivemos problemas pra registrar seu ponto.',
        messageType: MessageTypes.messageError,
        icon: FontAwesomeIcons.solidCircleCheck,
        timeout: const Duration(seconds: 5),
        actionNotification: ActionNotification(
          actionName: 'Tentar Novamente',
          action: actionFuncionNotification,
        ),
      );
    });
  }

  void actionFuncionNotification() {
    setState(() {
      notificationMessage = const NotificationMessage(
        message: 'Mudou.',
        messageType: MessageTypes.messageSuccess,
        icon: FontAwesomeIcons.circleCheck,
        timeout: const Duration(seconds: 5),
      );
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    final drawerItems = examplePages
        .asMap()
        .map(
          (pos, page) => MapEntry(
            pos,
            SeniorDrawerItem(
              icon: page.icon,
              onTap: () => _pageController.jumpToPage(pos),
              title: page.title,
              type: DrawerItemType.neutral,
            ),
          ),
        )
        .values
        .toList();

    final _tabBarConfig = TabBarConfig(
      tabIndex: tabIndex,
      onSelect: (int tab) {
        tabIndex = tab;
        _pageController.jumpToPage(tab);
      },
      tabs: ['Aba 1', 'Aba 2', 'Aba 3', 'Aba 4'],
    );

    return Consumer<ThemeRepository>(
      builder: (context, repository, child) => Scaffold(
        key: _scaffoldKey,
        body: SeniorColorfulHeaderStructure(
          notification: notificationMessage,
          actions: [
            Padding(
              padding: const EdgeInsets.all(SeniorSpacing.xxxsmall),
              child: SeniorIconButton(
                icon: repository.isLightTheme()
                    ? FontAwesomeIcons.moon
                    : FontAwesomeIcons.sun,
                onTap: () {
                  if (repository.isLightTheme()) {
                    repository.theme = SENIOR_DARK_THEME;
                  } else {
                    repository.theme = SENIOR_LIGHT_THEME;
                  }
                },
                size: SeniorIconButtonSize.small,
                type: SeniorIconButtonType.ghost,
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(SeniorSpacing.xxxsmall),
              child: SeniorIconButton(
                icon: FontAwesomeIcons.message,
                onTap: () {
                  updateMessageNotificationState();
                },
                size: SeniorIconButtonSize.small,
                type: SeniorIconButtonType.ghost,
              ),
            ),
          ],
          hasTopPadding: false,
          title: const Text('Senior Design System'),
          hideLeading: false,
          leading: IconButton(
            iconSize: SeniorIconSize.small,
            icon: const FaIcon(FontAwesomeIcons.bars),
            color: SeniorColors.pureWhite,
            onPressed: () => _scaffoldKey.currentState!.openDrawer(),
          ),
          body: PageView.builder(
            controller: _pageController,
            itemBuilder: (context, pos) =>
                examplePages.map((page) => page.pageComponent).toList()[pos],
            itemCount: examplePages.length,
            onPageChanged: (value) => setState(() {
              tabIndex = value;
            }),
          ),
          tabBarConfig: _tabBarConfig,
        ),
        drawer: SeniorDrawer(
          items: drawerItems,
          svgLogoPath: 'assets/images/logo-name.svg',
          footerText: 'total de ${examplePages.length} componentes exibidos',
          profilePicture: const SeniorProfilePicture(
            name: 'Albert Einstein',
            radius: 24.0,
            imageProvider: AssetImage('assets/images/albert-einstein.jpg'),
          ),
          profileTitle: 'Albert Einstein',
          profileSubtitle: 'Físico',
        ),
      ),
    );
  }
}

final List<Page> examplePages = [
  const Page(
    title: 'Icon',
    icon: FontAwesomeIcons.a,
    pageComponent: IconExample(),
  ),
  // 其他组件示例省略...
];

class Page {
  const Page({
    required this.icon,
    required this.pageComponent,
    required this.title,
  });

  final IconData icon;
  final Widget pageComponent;
  final String title;
}

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

1 回复

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


当然,针对Flutter设计系统插件senior_design_system的潜在功能使用,我们可以通过代码示例来展示一些典型的应用场景。请注意,由于senior_design_system可能是一个假设的插件名称,以下代码将基于一个假想的设计系统插件来展示可能的实现。在实际项目中,你需要根据具体的插件文档和API进行调整。

1. 主题和样式管理

设计系统通常包含一套预定义的主题和样式,用于保持应用的一致性和品牌识别度。

import 'package:flutter/material.dart';
import 'package:senior_design_system/senior_design_system.dart'; // 假设的包路径

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Senior Design System Demo',
      theme: SDSThemeData(
        primaryColor: SDSColors.primary,
        accentColor: SDSColors.accent,
        textTheme: SDSTextTheme.defaultTheme,
      ),
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Senior Design System Demo'),
      ),
      body: Center(
        child: Text(
          'Hello, Senior Design System!',
          style: SDSTextStyle.headline1,
        ),
      ),
    );
  }
}

2. 组件库使用

设计系统通常包含一系列预构建的组件,如按钮、卡片、对话框等,以简化开发过程。

import 'package:flutter/material.dart';
import 'package:senior_design_system/senior_design_system.dart'; // 假设的包路径

class ComponentScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Components Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            SDSButton(
              label: 'Primary Button',
              onPressed: () {},
              style: SDSButtonStyle.primary,
            ),
            SizedBox(height: 16.0),
            SDSButton(
              label: 'Secondary Button',
              onPressed: () {},
              style: SDSButtonStyle.secondary,
            ),
            SizedBox(height: 32.0),
            SDSCard(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Text('This is a card component from the design system.'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

3. 响应式设计

设计系统通常支持响应式设计,以确保应用在不同设备和屏幕尺寸上都能良好地显示。

import 'package:flutter/material.dart';
import 'package:senior_design_system/senior_design_system.dart'; // 假设的包路径

class ResponsiveScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        if (constraints.maxWidth < 600) {
          // Mobile layout
          return Column(
            children: <Widget>[
              SDSButton(
                label: 'Mobile Button',
                onPressed: () {},
                style: SDSButtonStyle.primary,
              ),
              Expanded(
                child: SDSCard(
                  child: Center(
                    child: Text('Mobile Card'),
                  ),
                ),
              ),
            ],
          );
        } else {
          // Desktop/Tablet layout
          return Row(
            children: <Widget>[
              Expanded(
                child: SDSButton(
                  label: 'Desktop Button',
                  onPressed: () {},
                  style: SDSButtonStyle.primary,
                ),
              ),
              Expanded(
                child: SDSCard(
                  child: Center(
                    child: Text('Desktop Card'),
                  ),
                ),
              ),
            ],
          );
        }
      },
    );
  }
}

4. 自定义和扩展

设计系统通常允许开发者自定义和扩展现有的主题和组件,以满足特定的需求。

import 'package:flutter/material.dart';
import 'package:senior_design_system/senior_design_system.dart'; // 假设的包路径

class CustomThemeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: SDSThemeData.copyWith(
        primaryColor: Colors.blueGrey,
        accentColor: Colors.deepOrange,
        textTheme: SDSTextTheme.defaultTheme.copyWith(
          headline1: TextStyle(fontSize: 32, color: Colors.white),
        ),
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Custom Theme Demo'),
        ),
        body: Center(
          child: Text(
            'Customized Theme in Action',
            style: SDSTextStyle.headline1.copyWith(color: Colors.yellow),
          ),
        ),
      ),
    );
  }
}

以上代码示例展示了如何使用一个假设的senior_design_system插件来实现主题管理、组件库使用、响应式设计和自定义扩展等功能。在实际项目中,你需要根据具体的插件文档和API进行相应的调整。

回到顶部