ui_kit_component
UI Kit Component is a versatile and reusable component generator designed for building beautiful user interfaces in Flutter UI Kit Packages. It provides a skeleton to built a collection of commonly used UI elements, such as buttons, cards, forms, and more, that can be easily integrated into your Flutter projects.
Generated by [mason][1] š§±
Getting Started š
To get started with UI Kit Component using Mason CLI, follow the steps below:
1. Installation
Before proceeding, make sure you have Mason CLI installed on your machine. To check if it is installed, run the following command:
mason version
If you encounter an error while running this command, it means that Mason CLI is not yet activated on your machine. To activate it, use the following command:
dart pub global activate mason_cli
2. Adding ui_kit_component brick
To add this brick to your mason registry, use one of the following commands based on your preference:
# Install it locally
mason add ui_kit_component
# Install it globally
mason add -g ui_kit_component
Usage
Before proceeding with the generation of a component, please ensure that you have already installed and built the skeleton of your UI Kit Package using the
ui_kit_core
brick. This is necessary as theui_kit_component
brick relies on the core package to function properly. Once you have successfully set up the UI Kit Package withui_kit_core
, you can proceed with generating the component using the provided instructions.
You can watch an interactive tutorial on how to use this brick by visiting my YouTube video at the following link:
Eps. 2 of Flutter UI Kit Package Development - Using Mason Bricks
In the tutorial video, I provide step-by-step instructions and demonstrations on how to effectively utilize the UI Kit Component Brick in your Flutter projects. Feel free to watch the video to gain a better understanding of the brick's usage and maximize its potential in your UI Kit development workflow.
Before using this brick, you need to create a JSON file containing all the required input variables for generating your component. The following table provides the variables needed:
Variable | Type | Description | Required |
---|---|---|---|
name | String | The name of the component to be generated. | Yes |
isStateful | Boolean | Determines the type of widget used by this component. | Yes |
hasEnum | Boolean | Specifies if this class has an enum class or not (e.g., ButtonVariant, ButtonSize). | Yes |
prefix | String | The prefix of the component name. | Yes |
properties | List | A list of component properties. | No |
enums | List | A list of enums to be generated in JSON format with name and values. | Yes (if hasEnum is true) |
For the properties variable, it should be an array containing property items with the following structure:
{
"name": "propertyName",
"type": "propertyType", // Type according to flutter (ex. String)
"isRequired": true,
"default": "defaultValue"
}
For the enums variable, it should be an array containing enum items with the following structure:
{
"name": "enum name", //ex. Button Size
"values": [
"small",
"medium",
"large"
],
}
Feel free to customize the values according to your needs.
If you have created your JSON file, name it based on your preference, for example, component-config.json
. Then, you can run the following command to generate the UI Kit Component using Mason CLI:
mason make ui_kit_component -c component-config.json
Make sure you replace component-config.json
with the actual file name and path if it's located in a different directory. This command will utilize the JSON configuration file to generate the UI Kit Component based on the specified parameters.
Example
For example, if you have the following component-config.json
in your project:
{
"name": "button",
"isStateful": false,
"hasEnum": true,
"prefix": "sk",
"properties": [
{
"name": "label",
"type": "String",
"isRequired": true
},
{
"name": "onpressed",
"type": "Future Function()",
"isRequired": true
},
{
"name": "size",
"type": "SKButtonSize",
"default": "SKButtonSize.small"
},
{
"name": "variant",
"type": "SKButtonVariant",
"default": "SKButtonVariant.primary"
},
{
"name": "enabled",
"type": "bool",
"default": true
}
],
"enums": [
{
"name": "button variant",
"values": [
"primary",
"outlined",
"ghost"
]
},
{
"name": "button size",
"values": [
"large",
"medium",
"regular",
"small"
]
}
]
}
If you run the command mason make ui_kit_component -c component-config.json
, it will generate the following files based on the provided configuration:
test/components/button/sk_button_test.dart
: Contains the widget test forSKButton
component.
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'sk_button_test_scenario.dart';
void main() {
group(
'Button Test',
() {
testWidgets(
buttonTestScenario[0].title,
(WidgetTester tester) async {
debugDefaultTargetPlatformOverride = TargetPlatform.android;
await tester.pumpWidget(buttonTestScenario[0].widget);
///expect(find.text('Text'), findsOneWidget);
debugDefaultTargetPlatformOverride = null;
},
);
},
);
}
test/components/button/sk_button_test_scenario.dart
: Contains all widget test scenario forSKButton
component.
import 'package:flutter/material.dart';
import 'package:sikerja_ui_kit/sikerja_ui_kit.dart';
import '../../util/sk_component_init_test.dart';
import '../../util/widget_test_scenario_model.dart';
List<WidgetTestScenarioModel> buttonTestScenario = [
WidgetTestScenarioModel(
title: 'has label set to Save',
widget: SKComponentInitTest(
child: SKButton(label: 'Save', onpressed: () async {}),
),
),
];
example/lib/pages/button_page.dart
: Contains a page for previewingSKButton
component in example app.
import 'package:flutter/material.dart';
import 'package:sikerja_ui_kit/sikerja_ui_kit.dart';
class ButtonPage extends StatelessWidget {
const ButtonPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Button'),
),
body: Padding(
padding: EdgeInsets.all(20.r),
child: SKButton(label: 'Save', onpressed: () async {}),
),
);
}
}
lib/sikerja_ui_kit.dart
: Appendexport
forSKButton
component into you ui kit package.
library sikerja_ui_kit;
// 3rd party packages
export 'package:flutter_screenutil/flutter_screenutil.dart';
export 'package:nil/nil.dart';
// components
// styles
export 'src/core/styles/sk_colors.dart';
export 'src/core/styles/sk_text_style.dart';
// themes
export 'src/core/theme/sk_color_theme.dart';
export 'src/core/theme/sk_theme.dart';
// utils
export 'src/core/utils/component_init.dart';
export 'src/components/button/enums/sk_button_enums.dart';
export 'src/components/button/sk_button.dart';
lib/src/components/buttons/enums/sk_button_enums.dart
: Contains all enums that you have been define before.
enum SKButtonVariant {
primary,
outlined,
ghost,
}
enum SKButtonSize {
large,
medium,
regular,
small,
}
lib/src/components/button/sk_button.dart
: Contains implementation of the component.
import 'package:flutter/material.dart';
import '../../../sikerja_ui_kit.dart';
class SKButton extends StatelessWidget {
const SKButton({
super.key,
required this.label,
required this.onpressed,
this.size = SKButtonSize.small,
this.variant = SKButtonVariant.primary,
this.enabled = true,
});
final String label;
final Future Function() onpressed;
final SKButtonSize size;
final SKButtonVariant variant;
final bool enabled;
@override
Widget build(BuildContext context) => Container();
}
Support
Please support me in continuously developing useful bricks and packages like this by liking and subscribing. Feel free to provide your feedback and suggestions through the comments.
Help Banua Coder continue to provide valuable content:
Connect with Banua Coder:
- Instagram: Banua Coder
- Website: Banua Coder
- GitHub: Banua Coder
For my personal social media accounts:
- Instagram: ryanaidilp
- GitHub: ryanaidilp
- LinkedIn: ryanaidilp
- Twitter: ryanaidilp