Flutter布局插件kiss_layout的使用

Flutter布局插件kiss_layout的使用

kiss_layout 是一个帮助开发者在Flutter应用中创建一致UI布局的包。该系统采用T恤尺码(S, M, L)来实现一致的间距。该插件由First Mobile Digital Group SL开发。

特性

  • 简单的T恤尺码系统 - 使用小、中、大三种尺寸来保持间距的一致性。
  • 灵活的布局系统 - 轻松管理填充、间隔和圆角半径。
  • 全局布局配置 - 在应用级别设置布局规则。
  • 覆盖能力 - 可以为特定部分自定义布局。
  • 响应式设计 - 简单处理不同屏幕尺寸。
  • KISS原则 - 保持简单,愚蠢!代码干净且易于维护。
  • 减少Flutter参数地狱 - 消除参数和字面量到处乱用的问题。

开始使用

首先,在项目的 pubspec.yaml 文件中添加依赖:

dependencies:
  kiss_layout: ^1.0.0

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

基本设置

将你的应用包裹在一个 Layout 组件中,以便提供全局布局设置:

void main() {
  runApp(
    Layout(
      child: MaterialApp(
        home: MyHomePage(),
      ),
    ),
  );
}

使用间距

kiss_layout 提供了一些预构建的间隔组件,用于保持一致的间距:

Column(
  children: [
    Text('Header'),
    const GapLarge(), // 大间距
    Text('Content'),
    const GapMedium(), // 中间距
    Text('Footer'),
    const GapSmall(), // 小间距
  ],
)

使用填充

内置的填充组件可以用来保持一致的边缘间距:

PaddingOuterMedium(
  child: Card(
    child: PaddingInnerSmall(
      child: Text('Content'),
    ),
  ),
)

更复杂的例子

下面是一个使用布局设置的自定义组件的例子:

class CustomCard extends StatelessWidget {
  const CustomCard({
    super.key,
    required this.title,
    required this.content,
  });

  final String title;
  final String content;

  @override
  Widget build(BuildContext context) {
    // 获取当前布局设置
    final layout = Layout.of(context);

    return Container(
      // 使用布局设置的圆角
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(layout.cornerRadii.medium),
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            blurRadius: 4,
            offset: const Offset(0, 2),
            color: Colors.black.withOpacity(0.1),
          ),
        ],
      ),
      // 使用布局设置的边缘间距
      padding: layout.edgeSpacing.inner.medium,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: Theme.of(context).textTheme.titleMedium,
          ),
          // 使用布局设置的项目间距
          Gap(layout.itemSpacing.medium),
          Text(content),
          Gap(layout.itemSpacing.large),
          // 使用布局设置的动作大小
          SizedBox(
            width: layout.actionSizes.medium.width,
            height: layout.actionSizes.medium.height,
            child: ElevatedButton(
              onPressed: () {},
              child: const Text('Action'),
            ),
          ),
        ],
      ),
    );
  }
}

自定义布局设置

直接配置

你可以直接自定义布局设置:

Layout(
  itemSpacing: const LayoutItemGaps(
    large: 24.0,
    medium: 16.0,
    small: 8.0,
  ),
  edgeSpacing: const LayoutEdgeSpacing(
    outer: LayoutEdgeSpacingSizes(
      large: EdgeInsets.all(24),
      medium: EdgeInsets.all(16),
      small: EdgeInsets.all(8),
    ),
  ),
  child: YourWidget(),
)

预定义布局样式

你还可以创建自定义的布局子类,以在整个应用中保持一致的风格:

// 紧凑布局,间距更紧密,组件更小
class CompactLayout extends Layout {
  const CompactLayout({
    required super.child,
    super.key,
  }) : super(
          itemSpacing: const LayoutItemGaps(
            large: 16.0,
            medium: 8.0,
            small: 4.0,
          ),
          edgeSpacing: const LayoutEdgeSpacing(
            outer: LayoutEdgeSpacingSizes(
              large: EdgeInsets.all(16),
              medium: EdgeInsets.all(12),
              small: EdgeInsets.all(8),
            ),
            inner: LayoutEdgeSpacingSizes(
              large: EdgeInsets.all(12),
              medium: EdgeInsets.all(8),
              small: EdgeInsets.all(4),
            ),
          ),
          // 其他自定义设置
        );
}

// 宽敞布局,间距更大,组件更大
class SpaciousLayout extends Layout {
  const SpaciousLayout({
    required super.child,
    super.key,
  }) : super(
          itemSpacing: const LayoutItemGaps(
            large: 32.0,
            medium: 24.0,
            small: 16.0,
          ),
          edgeSpacing: const LayoutEdgeSpacing(
            outer: LayoutEdgeSpacingSizes(
              large: EdgeInsets.all(32),
              medium: EdgeInsets.all(24),
              small: EdgeInsets.all(16),
            ),
            // 其他自定义设置
          ),
        );
}

// 使用示例
void main() {
  runApp(
    CompactLayout(
      child: MaterialApp(
        home: MyCompactPage(),
      ),
    ),
  );
}

其他功能

  • 边缘间距(外边距和内边距)
  • 动作大小(按钮和其他交互元素的大小)
  • 圆角半径(保持一致的圆角)
  • 英雄大小(突出元素的大小)
  • 模态底部表单配置
  • 屏幕尺寸断点

如果我需要超过S、M、L的更多尺寸怎么办?

  • 如果你在单个屏幕上需要更多的尺寸,请重新考虑你的设计或调整组件的组合。
  • 如果应用的一部分需要更紧凑或更宽敞的布局,可以在该部分使用新的S、M、L尺寸。
  • 如果你确实需要超过三个尺寸,请打开一个问题,我们会考虑添加更多的尺寸。

贡献

我们欢迎贡献!请随时提交Pull请求。

许可证

MIT License

Copyright (c) 2024 First Mobile Digital Group SL

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

更多关于Flutter布局插件kiss_layout的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter布局插件kiss_layout的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中使用kiss_layout插件的简单示例。kiss_layout是一个轻量级的布局插件,旨在简化Flutter中的布局操作。虽然这个插件可能不是官方或广泛使用的,但我们可以假设它提供了一些简化的布局组件。

首先,你需要在你的pubspec.yaml文件中添加kiss_layout依赖(注意:由于kiss_layout可能不是一个真实存在的插件,这里我们假设它存在并这样命名,实际应用中请替换为真实的插件名):

dependencies:
  flutter:
    sdk: flutter
  kiss_layout: ^x.y.z  # 替换为实际版本号

然后运行flutter pub get来获取依赖。

接下来,在你的Dart文件中使用kiss_layout提供的组件。由于我们不知道kiss_layout具体提供了哪些组件,这里我假设它提供了一个KissColumnKissRow组件来分别实现列布局和行布局。以下是一个简单的使用示例:

import 'package:flutter/material.dart';
import 'package:kiss_layout/kiss_layout.dart'; // 假设这是插件的导入路径

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Kiss Layout Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Kiss Layout Demo'),
        ),
        body: KissColumn(
          crossAxisAlignment: CrossAxisAlignment.start, // 假设支持的属性
          children: <Widget>[
            KissRow(
              mainAxisAlignment: MainAxisAlignment.spaceBetween, // 假设支持的属性
              children: <Widget>[
                Text('Item 1'),
                Text('Item 2'),
                Text('Item 3'),
              ],
            ),
            SizedBox(height: 20), // 垂直间距
            KissColumn(
              children: <Widget>[
                Text('Nested Column Item 1'),
                Text('Nested Column Item 2'),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,其中使用了假设的KissColumnKissRow组件来实现布局。KissColumn用于垂直排列子组件,而KissRow用于水平排列子组件。我们还使用了CrossAxisAlignmentMainAxisAlignment属性来调整子组件的对齐方式(这些属性是基于Flutter的Flex布局的属性,假设kiss_layout也支持这些属性)。

请注意,由于kiss_layout可能不是一个真实存在的插件,上述代码中的组件和属性都是假设的。在实际使用中,你需要参考kiss_layout的官方文档或源代码来了解其提供的具体组件和属性。如果kiss_layout确实存在,上面的代码结构应该是一个很好的起点,你只需要替换为实际的组件和属性即可。

回到顶部