6.4 Pin 组件封装
实现步骤:
https://pub.flutter-io.cn/packages/pinput
第 2 步:定义参数
作为业务组件放在 lib/common/components
lib/common/components/pin.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class PinPutWidget extends StatelessWidget { final Function(String)? onSubmit;
final FocusNode? focusNode;
final TextEditingController? controller;
final String? Function(String?)? validator;
|
第 3 步:样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| @override Widget build(BuildContext context) { final defaultPinTheme = PinTheme( width: 45, height: 45, textStyle: const TextStyle( fontSize: 18, fontWeight: FontWeight.w600), decoration: BoxDecoration( border: Border.all(color: AppColors.surfaceVariant), borderRadius: BorderRadius.circular(5), ), ); final focusedPinTheme = defaultPinTheme.copyDecorationWith( border: Border.all(color: AppColors.primary), borderRadius: BorderRadius.circular(5), ); final submittedPinTheme = defaultPinTheme.copyWith( decoration: defaultPinTheme.decoration?.copyWith( color: AppColors.surfaceVariant, ), );
|
最后:完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| import 'package:flutter/material.dart'; import 'package:pinput/pinput.dart';
import '../index.dart';
class PinPutWidget extends StatelessWidget { final Function(String)? onSubmit;
final FocusNode? focusNode;
final TextEditingController? controller;
final String? Function(String?)? validator;
const PinPutWidget({ Key? key, this.onSubmit, this.focusNode, this.controller, this.validator, }) : super(key: key);
@override Widget build(BuildContext context) { final defaultPinTheme = PinTheme( width: 45, height: 45, textStyle: const TextStyle( fontSize: 18, fontWeight: FontWeight.w600), decoration: BoxDecoration( border: Border.all(color: AppColors.surfaceVariant), borderRadius: BorderRadius.circular(5), ), ); final focusedPinTheme = defaultPinTheme.copyDecorationWith( border: Border.all(color: AppColors.primary), borderRadius: BorderRadius.circular(5), ); final submittedPinTheme = defaultPinTheme.copyWith( decoration: defaultPinTheme.decoration?.copyWith( color: AppColors.surfaceVariant, ), );
return Pinput( defaultPinTheme: defaultPinTheme, focusedPinTheme: focusedPinTheme, submittedPinTheme: submittedPinTheme, length: 6, validator: validator, pinputAutovalidateMode: PinputAutovalidateMode.onSubmit, showCursor: true, autofocus: true, obscureText: true, keyboardAppearance: Brightness.light, focusNode: focusNode, controller: controller, onCompleted: onSubmit, ); } }
|
提交代码到 git