Flutter自定义组件插件hmwidget的使用

Flutter自定义组件插件hmwidget的使用

HM Widget

这是一个易于使用的开源UI库,包含用于构建Flutter应用程序的各种组件。

安装

步骤 1

在你的 pubspec.yaml 文件中添加最新版本的包,并运行 flutter pub get

dependencies:
  hmwidget: ^0.0.3

步骤 2

导入该包并在你的Flutter应用中使用它:

import 'package:hmwidget/hmwidget.dart';

开始使用

以下是一些你可以自定义的组件:

  • HMButton
  • HMIconButton
  • HMCheckBox
  • HMSwitch
  • HMSlider
  • HMRangeSlider
  • HMTextField
  • HMRadio
  • HMSelect
  • HMAutocomplete

示例

基本示例

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

void main() => runApp(
      const MaterialApp(
        home: Material(
          child: Scaffold(
            body: Center(
              child: HMButton(
                onPressed: () => print("Pressed"),
                buttonVariant: HMButtonVariant.outlined,
                content: 'Press',
                textColor: Colors.blue,
              ),
            ),
          ),
        ),
      ),
    );

复杂示例

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        extensions: [
          HMButtonTheme(
            fillColor: Colors.pink,
            textColor: Colors.green,
            buttonVariant: HMButtonVariant.filled,
            size: HMButtonSize.md,
          ),
          HMIconButtonTheme(
            fillColor: Colors.pink,
            iconColor: Colors.green,
            buttonVariant: HMButtonVariant.filled,
            size: HMIconButtonSize.xs,
          ),
          HMSwitchTheme(
            color: Colors.redAccent,
            radius: HMRadius.xl,
            size: HMSwitchSize.md,
          ),
          HMCheckBoxTheme(
            color: Colors.redAccent,
            radius: HMRadius.sm,
            size: HMCheckBoxSize.md,
          ),
          HMTextFieldTheme(
            radius: HMRadius.md,
            variant: HMTextVariant.outlined,
          ),
          HMSliderTheme(
            color: Colors.orange,
            radius: HMRadius.xs,
            size: HMSliderSize.md,
          )
        ],
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool ischecked = false;
  String select = '';
  List<String> choix = [];
  RangeValues rangeValues = const RangeValues(10, 50);
  List<String> options = [
    'pomme',
    'banane',
    'orange',
    'ananas',
    'pastèque',
    'fraise',
    'melon',
    'raisin',
    'kiwi',
    'mangue',
    'papaye',
    'poire',
    'citron',
    'lime',
    'pamplemousse',
    'mandarine',
    'framboise',
    'myrtille',
    'cerise',
    'pêche'
  ];
  String choosedValue = "";

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("HmWidget demo"),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.symmetric(vertical: 10),
        child: Column(
          children: [
            HMAutocomplete(
              hintText: "Enter something",
              optionsBuilder: (value) {
                if (value == '') {
                  return [];
                }
                return options.where((String option) {
                  return option.contains(value);
                }).toList();
              },
              initialValue: choosedValue,
              onSelected: (value) {
                setState(() {
                  choosedValue = value;
                });
              },
            ),
            const SizedBox(height: 50),
            HMIconButton(
              icon: const Icon(Icons.edit_calendar_sharp),
              onPressed: () {},
            ),
            SizedBox(
              width: 200,
              child: HMSlider(
                value: 10,
                onChange: (val) {},
              ),
            ),
            const SizedBox(height: 10),
            HMRangeSlider(
              rangeValues: rangeValues,
              onChange: (range) {},
            ),
            HMRadio(
              value: select,
              radioList: const ["Flutter", 'React', 'Svelte', 'Vue', 'Python'],
              onChanged: (value) {
                setState(() {
                  select = value;
                });
              },
            ),
            HMSelect(
              value: select,
              selectionPageTitle: const Align(
                alignment: Alignment.centerLeft,
                child: Padding(
                  padding: EdgeInsets.all(15.0),
                  child: Text('Select a Framework'),
                ),
              ),
              selectList: const [
                "Flutter",
                'React',
                'Svelte',
                'Vue',
                'Angular'
              ],
              onChanged: (value) {
                setState(() {
                  select = value;
                });
              },
            ),
            HMCheckBox(
              value: ischecked,
              onChange: (val) {
                setState(() {
                  ischecked = val;
                  print(ischecked);
                });
              },
            ),
            HMSelectBadge(
              selectedList: choix
                  .map((element) => HMSelectedItem(
                        avatar: Text(element.toString()[0]),
                        value: element.toString(),
                        label: Text(element.toString()),
                      ))
                  .toList(),
              radius: HMRadius.md,
              onDeleted: (deletedValue) {
                setState(() {
                  choix.remove(deletedValue);
                });
              },
              direction: Axis.vertical,
              onTap: (index) {
                print(index);
              },
            ),
            HMMultiSelect(
              selectionPageTitle: const Align(
                alignment: Alignment.centerLeft,
                child: Padding(
                  padding: EdgeInsets.all(15.0),
                  child: Text('Select a Framework'),
                ),
              ),
              selectListItem: const [
                "Création d'une armoire de rangement sur mesure",
                "Réparation d'une chaise en bois",
                "Fabrication d'un escalier en bois",
                "Création d'une bibliothèque encastrée",
                "Réparation d'un plancher en bois",
                "Fabrication d'un banc de jardin en bois",
                "Création d'un présentoir à bijoux en bois"
              ],
              onChanged: (value) {
                setState(() {
                  choix = value;
                  print(choix);
                });
              },
              selectedValues: choix,
              onSelectedValuePressed: (int index) {
                print(index);
              },
            ),
            HMChoiceChips(
              label: const Text("Call"),
              isFilled: true,
              radius: HMRadius.md,
              onSelected: (value) {
                print(value);
              },
            ),
            HMFilterChips(
              label: const Text("Flutter"),
              isFilled: true,
              onSelected: (value) {
                print(value);
              },
            ),
            const HMTextField(
              size: HMTextFieldSize.md,
            ),
            HMSwitch(
              value: ischecked,
              onChange: (val) {
                setState(() {
                  ischecked = val;
                });
              },
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: HMButton(
                onPressed: () => print("Pressed"),
                buttonVariant: HMButtonVariant.outlined,
                fullWidth: true,
                content: 'Press',
              ),
            ),
            SizedBox(
              width: 200,
              child: HMTextField(
                textFieldType: HMTextFieldType.password,
                variant: HMTextVariant.filled,
                maxLength: 8,
                onChange: (val) => print(val),
              ),
            ),
            HMButton(content: "CLIC", onPressed: () {}),
            HMColorInput(
              initialColor: colorToString(Colors.blue),
              onColorChange: (color) {
                print(color);
              },
            ),
            const SizedBox(height: 50),
            HMFilePiker(
              fileViewInModal: false,
              isMultipleFiles: true,
              onFileSelected: (files) {
                print('nombres de fichiers : ${files.length}');
              },
            ),
            HMImagePicker(
              isMultipleImage: false,
              onImageSelected: (value) => print(value),
            ),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter自定义组件插件hmwidget的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

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


hmwidget 是一个自定义的 Flutter 组件库,它提供了一些常用的 UI 组件和工具,帮助开发者快速构建应用程序。使用 hmwidget 可以简化开发流程,减少重复代码。以下是如何在 Flutter 项目中使用 hmwidget 的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 hmwidget 的依赖。

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

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

2. 导入包

在你的 Dart 文件中导入 hmwidget 包:

import 'package:hmwidget/hmwidget.dart';

3. 使用组件

hmwidget 提供了多种常用的组件,例如按钮、卡片、对话框等。以下是一些常见组件的使用示例:

按钮 (HmButton)

HmButton(
  onPressed: () {
    print('Button Pressed!');
  },
  text: 'Click Me',
)

卡片 (HmCard)

HmCard(
  child: Text('This is a card'),
)

对话框 (HmDialog)

HmDialog.show(
  context: context,
  title: 'Alert',
  content: 'This is a dialog',
  actions: [
    HmButton(
      text: 'OK',
      onPressed: () {
        Navigator.of(context).pop();
      },
    ),
  ],
)

输入框 (HmTextField)

HmTextField(
  hintText: 'Enter your name',
  onChanged: (value) {
    print('Input: $value');
  },
)

4. 自定义主题

hmwidget 支持自定义主题。你可以在 MaterialApp 中设置主题:

MaterialApp(
  theme: ThemeData(
    primarySwatch: Colors.blue,
    // 其他主题设置
  ),
  home: MyHomePage(),
)
回到顶部