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

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

DlWidgets

DlWidgets 是一个 Flutter 库,旨在通过提供一系列有用的可定制组件来简化开发过程。

特性

  • 设计组件:减少开发密码文本字段和普通文本字段的时间。所有组件都是预先构建好的。

安装

要在您的 Flutter 项目中使用 DlWidgets,您需要将其作为依赖项添加到 pubspec.yaml 文件中:

dependencies:
  dl_widgets: ^1.0.0

然后,运行以下命令以获取包:

flutter pub get

使用

将 DlWidgets 包导入到您的 Dart 文件中:

import 'package:dl_widgets/dl_widgets.dart';

现在,您可以开始使用 DlWidgets 库了。以下是一个如何使用其中一个组件的示例:

登录页面演示

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

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

  [@override](/user/override)
  State<LoginPage> createState() => _State();
}

class _State extends State<LoginPage> {

  String? token;
  String? emailError;
  String? passwordError;
  String? tickerMessage;
  TickerType? tickerType;

  [@override](/user/override)
  void dispose() {
    // TODO: 实现 dispose 方法
    textEditingController.dispose();
    passwordController.dispose();
    super.dispose();
  }

  Future login(String email, String password, BuildContext context) async {
    token = "Make an API CALL for getting";
    setState(() {
      if(token != null) {
        ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
            content: Text("Authenticated")
        ));
        tickerMessage = "Authenticated";
        tickerType = TickerType.success;
      } else {
        showDialog(context: context, builder: (context) => DlAlertDialog(dlAlertDialogImpl: alertDialog, showDialogFlag: showDialogF));
        ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
            content: Text("Invalid user")
        ));
        tickerMessage = "Invalid user";
        tickerType = TickerType.error;
      }
    });
  }

  TextEditingController textEditingController = TextEditingController();
  TextEditingController passwordController = TextEditingController();
  late DlTextFormFieldImpl emailField;
  late DlAlertDialogImpl alertDialog;
  late DlTextFormFieldImpl passWordField;
  
  [@override](/user/override)
  void initState() {
    alertDialog = DlAlertDialogImpl(
      title: Text("Invalid User"),
      content: Text("Not allowed"),
      positiveAction: ElevatedButton(onPressed:(){}, child: Text("Yes")),
      negativeAction: ElevatedButton(onPressed:(){}, child: Text("No")),
      backgroundColor: Colors.white,
      shape: DialogShape.curved
    );

    emailField = DlTextFormFieldImpl(
      textEditingController, 
      (value) {
        setState(() {
          emailField.errorText = null;
          passWordField.errorText = null;
        });
      }, 
      emailError, 
      "Email"
    );
    passWordField = DlTextFormFieldImpl(
      textEditingController, 
      (value) {
        setState(() {
          emailField.errorText = null;
          passWordField.errorText = null;
        });
      }, 
      passwordError, 
      "Password"
    );
    
    super.initState();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Padding(padding: EdgeInsets.symmetric(vertical: MediaQuery.of(context).size.height / 5), child:
          SingleChildScrollView(
              physics: const BouncingScrollPhysics(),
              child: Center(child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Padding(padding: EdgeInsets.symmetric(horizontal: MediaQuery.of(context).size.width / 3), child: DlCardView(
                    child:  Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        DlTextFormField(isCenter: false, activeColor: Colors.blue, isPassword: false, input: emailField),
                        DlTextFormField(isCenter: false, activeColor: Colors.blue, isPassword: true, input: passWordField),
                        tickerType != null ? DlTicker(textInfo: tickerMessage ?? "", tickerType: tickerType) : Container()
                      ],
                    ),
                  )),
                  DlButton(buttonName: "Login", size: ButtonSize.small, onPressed: () {
                    setState(() {
                      if (emailField.textFieldController.text.isEmpty) {
                        emailField.errorText = "Email must not be empty";
                      }
                      if (passWordField.textFieldController.text.isEmpty) {
                        passWordField.errorText = "Password must not be empty";
                      }
                    });
                    login(emailField.textFieldController.text, passWordField.textFieldController.text, context);
                  }, buttonColor: Colors.blue),
                  Center(
                      child: Padding(padding: const EdgeInsets.all(24), child: GestureDetector(onTap: () {
                        Navigator.push(context, MaterialPageRoute(builder: (context) => const SignUpPage()));
                      }, child: const Text("Sign up", maxLines: 1, style: TextStyle(color: Colors.blue)),),
                      ))
                ],
              )))),
        ],
      ),
    );
  }
}

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

1 回复

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


dlwidgets 是一个 Flutter 自定义组件插件,它提供了一些常用的自定义 Widget,帮助开发者快速构建复杂的 UI 界面。使用 dlwidgets 可以提高开发效率,减少重复代码的编写。

安装

  1. pubspec.yaml 中添加依赖

    打开你的 pubspec.yaml 文件,在 dependencies 部分添加 dlwidgets 依赖:

    dependencies:
      flutter:
        sdk: flutter
      dlwidgets: ^版本号  # 替换为最新版本号
    
  2. 安装依赖

    在终端中运行以下命令来安装依赖:

    flutter pub get
    

使用

在项目的 Dart 文件中导入 dlwidgets

import 'package:dlwidgets/dlwidgets.dart';

示例组件

以下是一些 dlwidgets 中可能包含的组件示例(具体组件可能因版本不同而有所变化):

  1. DLButton

    DLButton 是一个自定义按钮组件,支持多种样式和点击事件。

    DLButton(
      text: 'Click Me',
      onPressed: () {
        print('Button Pressed!');
      },
      backgroundColor: Colors.blue,
      textColor: Colors.white,
    );
    
  2. DLText

    DLText 是一个自定义文本组件,支持多种文本样式。

    DLText(
      'Hello, dlwidgets!',
      style: TextStyle(
        fontSize: 20,
        fontWeight: FontWeight.bold,
        color: Colors.black,
      ),
    );
    
  3. DLContainer

    DLContainer 是一个自定义容器组件,支持背景颜色、边框、阴影等。

    DLContainer(
      child: DLText('This is a container'),
      padding: EdgeInsets.all(16),
      margin: EdgeInsets.all(8),
      backgroundColor: Colors.grey[200],
      borderRadius: BorderRadius.circular(8),
      boxShadow: [
        BoxShadow(
          color: Colors.black26,
          blurRadius: 4,
          offset: Offset(0, 2),
        ),
      ],
    );
    
  4. DLListTile

    DLListTile 是一个自定义列表项组件,通常用于构建列表中的每一行。

    DLListTile(
      leading: Icon(Icons.person),
      title: DLText('John Doe'),
      subtitle: DLText('Software Developer'),
      trailing: Icon(Icons.arrow_forward),
    );
    
  5. DLAlertDialog

    DLAlertDialog 是一个自定义的对话框组件,支持标题、内容和操作按钮。

    DLAlertDialog(
      title: 'Alert',
      content: DLText('This is an alert dialog.'),
      actions: [
        DLButton(
          text: 'OK',
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ],
    );
回到顶部