Flutter 密碼輸入框 驗證碼輸入框

尤先森發表於2020-07-07

Flutter 密碼輸入框 驗證碼輸入框

支援iOS、Android、web

Flutter 密碼輸入框 驗證碼輸入框

支援明文/密文,有2種風格可供選擇,並且支援多種UI風格定製,包括密文字元、邊框、圓角、顏色、TextStyle等等,以下是所有支援的內容。

  final HBPasswordInputTextFieldType type; //格子樣式
  final Function onChange;//輸入監聽
  final int length; //輸入長度
  final TextEditingController controller; //輸入控制器
  final FocusNode node; //焦點
  final double boxWidth; //格子寬
  final double boxHeight; //格子高
  final double borderWidth; //邊框寬
  final double borderRaiuds; //圓角
  final Color borderColor; //邊框顏色
  final Color fillColor; //填充顏色
  final Color backgroundColor; //填充顏色
  final double spacing; //格子間隔
  final bool obscureText; //是否密文
  final String obscureTextString; //密文字元
  final TextStyle textStyle; //文字樣式
複製程式碼

安裝

pub.dev 地址

在你的專案中pubspec.yaml中新增

dependencies:
  hb_password_input_textfield: ^0.1.1    
複製程式碼

有必要的話,執行一下這個命令,我是VSCode儲存之後自動就執行了。 $ flutter pub get

然後在對應檔案引用他

import 'package:hb_password_input_textfield/hb_password_input_textfield.dart';
複製程式碼

Example

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

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'HBPasswordInputTextField Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: HBPasswordInputTextFieldPage(),
    );
  }
}

class HBPasswordInputTextFieldPage extends StatefulWidget {
  @override
  _HBPasswordInputTextFieldPageState createState() =>
      _HBPasswordInputTextFieldPageState();
}

class _HBPasswordInputTextFieldPageState
    extends State<HBPasswordInputTextFieldPage> {
  // int length = 0;
  TextEditingController _controller = TextEditingController();
  FocusNode _node = FocusNode();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("HBPasswordInputTextField"),
      ),
      body: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            _node.unfocus();
          },
          child: Container(
            margin: EdgeInsets.only(top: 50),
            child: HBPasswordInputTextField(
              // backgroundColor: Colors.red,
              // fillColor: Colors.red,
              borderWidth: 0.5,
              borderRaiuds: 5,
              controller: _controller,
              node: _node,
              backgroundColor: Colors.red,
              obscureText: true,
              // obscureTextString: "?",
              // boxWidth: 50,
              // boxHeight: 50,
              type: HBPasswordInputTextFieldType.BOXES,
              length: 6,
              textStyle: TextStyle(fontSize: 20),
              onChange: (text) {
                print(text);
              },
            ),
          )),
    );
  }
}
複製程式碼

相關文章