Flutter多消息发送插件multi_sender_pinput的使用

Flutter多消息发送插件multi_sender_pinput的使用

🌍 关于项目

本插件用于在所有平台上发送多条消息。

🛠 构建工具

  • smart_auth: SmartAuth
  • universal_platform: UniversalPlatform

源代码结构

/my_camera
  example/
        lib/
          main.dart
  lib/
    multi_sender_pinput/
                      models/
                      utils/
                      widgets/
        multi_sender_pinput.dart
        pinput.dart    

🧾 设置项目

命名规则

类别 规则 示例
类、枚举、typedef Pascal Case ProductBloc
库、包、目录、文件名 snake_case product_detail_view
变量、常量、参数、命名参数 camel Case variants

清洁代码

  • 创建UI组件库以供通用组件使用。
  • 不要直接使用颜色或字体样式,而是使用UI组件库。
  • 将小部件拆分为子小部件。
  • 为每个功能创建组件包来存储子小部件。
  • 指定类成员的类型。
  • 使用if条件而不是条件表达式。
  • 使用??和?.操作符。
  • 使用级联操作符。
  • 在小部件中使用Const。

示例代码

以下是一个使用multi_sender_pinput插件的基本示例:

import 'package:flutter/material.dart';
import 'package:multi_sender_pinput/multi_sender_pinput/pinput.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.white,
          elevation: 0,
          title: const Text('Pinput Example'),
          centerTitle: true,
          titleTextStyle: const TextStyle(
            fontSize: 22,
            fontWeight: FontWeight.w600,
            color: Color.fromRGBO(30, 60, 87, 1),
          ),
        ),
        body: const FractionallySizedBox(
          widthFactor: 1,
          child: PinputExample(),
        ),
      ),
    ),
  );
}

/// 这是Pinput的基本用法
/// 更多例子请查看演示目录
class PinputExample extends StatefulWidget {
  const PinputExample({Key? key}) : super(key: key);

  [@override](/user/override)
  State<PinputExample> createState() => _PinputExampleState();
}

class _PinputExampleState extends State<PinputExample> {
  final pinController = TextEditingController();
  final focusNode = FocusNode();
  final formKey = GlobalKey<FormState>();

  [@override](/user/override)
  void dispose() {
    pinController.dispose();
    focusNode.dispose();
    super.dispose();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    const borderColor = Color.fromRGBO(23, 171, 144, 0.4);

    final defaultPinTheme = PinTheme(
      width: 56,
      height: 56,
      textStyle: const TextStyle(
        fontSize: 22,
        color: Color.fromRGBO(30, 60, 87, 1),
      ),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(19),
        border: Border.all(color: borderColor),
      ),
    );

    /// 你可以选择使用表单来验证Pinput
    return Form(
      key: formKey,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Directionality(
            // 指定方向(如果需要)
            textDirection: TextDirection.ltr,
            child: Pinput(
              defaultPinTheme: defaultPinTheme,
              scrollPadding: const EdgeInsets.all(56),
              focusNode: focusNode,
              autofocus: true,
              closeKeyboardWhenCompleted: false,
              // readOnly: true,
              autofillHints: const [AutofillHints.oneTimeCode],
              controller: pinController,
              length: 6,
              androidSmsAutofillMethod: AndroidSmsAutofillMethod.smsUserConsentApi,
              listenForMultipleSmsOnAndroid: true,
              senderPhoneNumber: const ['+989385371961', '+989398216784', '+989122331488'],
              textInputAction: TextInputAction.done,
              showCursor: false,
              keyboardType: TextInputType.number,
              animationCurve: Curves.ease,
              animationDuration: const Duration(milliseconds: 300),
              pinAnimationType: PinAnimationType.slide,
              smsPermissionStatus: (value) {
                debugPrint('Sms Permission Status: $value');
              },
              showSimpleTextField: true,
              readFromSms: true,
              onChanged: (value) {
                print('onChanged: $value');
              },
              onCompleted: (value) {
                print('onCompleted: $value');
              },
              autoFillValidation: (line) {
                return (line.contains('رمز') || line.contains('بلیت') || line.contains('بليت')) &&
                    !line.contains('زمان');
              },
              showPermissionDialog:(){
                return showDialog<void>(context: context, builder: (BuildContext context) { 
                  return Container(
                    color: Colors.white,
                    height: 300,
                    width: 300,
                    child: Center(
                      child: Text('Test'),
                    ),
                  );
                });
              },
            ),
          ),
          TextButton(
            onPressed: () {
              focusNode.unfocus();
              formKey.currentState!.validate();
            },
            child: const Text('Validate'),
          ),
        ],
      ),
    );
  }
}

更多关于Flutter多消息发送插件multi_sender_pinput的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

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


当然,下面是一个关于如何使用Flutter中的multi_sender_pinput插件来实现多消息发送功能的代码示例。这个插件允许用户在输入框中输入多行文本,并可以单独发送每一条消息。

首先,确保你已经在你的pubspec.yaml文件中添加了multi_sender_pinput的依赖:

dependencies:
  flutter:
    sdk: flutter
  multi_sender_pinput: ^最新版本号

然后,运行flutter pub get来安装这个插件。

接下来,在你的Flutter项目中,你可以按照以下步骤来使用multi_sender_pinput

  1. 导入必要的包
import 'package:flutter/material.dart';
import 'package:multi_sender_pinput/multi_sender_pinput.dart';
  1. 创建一个包含MultiSenderPInput的页面
class ChatScreen extends StatefulWidget {
  @override
  _ChatScreenState createState() => _ChatScreenState();
}

class _ChatScreenState extends State<ChatScreen> {
  final List<String> messages = [];
  final TextEditingController _controller = TextEditingController();

  void _sendMessage(String message) {
    setState(() {
      messages.add(message);
      _controller.clear();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Chat Screen'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Expanded(
              child: ListView.builder(
                itemCount: messages.length,
                itemBuilder: (context, index) {
                  return Padding(
                    padding: const EdgeInsets.symmetric(vertical: 4.0),
                    child: Text(
                      messages[index],
                      style: TextStyle(fontSize: 18),
                    ),
                  );
                },
              ),
            ),
            MultiSenderPInput(
              controller: _controller,
              sendButton: IconButton(
                icon: Icon(Icons.send),
                onPressed: () {
                  if (_controller.text.isNotEmpty) {
                    _sendMessage(_controller.text);
                  }
                },
              ),
              onTextAdded: (text) {
                // This callback is called when a new line is added
                // You can add custom logic here if needed
              },
              keyboardType: TextInputType.multiline,
              maxLines: null,
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                hintText: 'Type your message...',
                suffixIcon: IconButton(
                  icon: Icon(Icons.clear),
                  onPressed: () {
                    _controller.clear();
                  },
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
  1. 在你的主应用中使用这个页面
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ChatScreen(),
    );
  }
}

在这个示例中,我们创建了一个简单的聊天界面,其中:

  • 使用ListView.builder来显示已经发送的消息。
  • 使用MultiSenderPInput来输入多行文本,并通过点击发送按钮来发送消息。
  • 每条消息发送后,输入框的内容会被清空,并且消息会被添加到消息列表中。

你可以根据实际需求进一步自定义和扩展这个示例。

回到顶部