Flutter自定義Widget之驗(yàn)證碼輸入框

Flutter網(wǎng)絡(luò)請求庫Dio的封裝(單例、動態(tài)baseUrl简识、攔截器赶掖、日志、請求loading)

實(shí)現(xiàn)效果

gifhome_480x960_15s.gif

支持屬性

屬性名 效果
cellCount 單格數(shù)量
autofocus 是否自動聚焦
borderRadius 單格圓角
solidColor 單格背景色
strokeColor 邊框顏色
textColor 字體顏色
fontSize 字體大小
inputCompleteCallback 輸入完成的回調(diào)

源碼

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

typedef InputCompleteCallback(String content);

class CellInput extends StatefulWidget {
  int cellCount = 6;
  InputType inputType = InputType.number;
  InputCompleteCallback inputCompleteCallback;
  bool autofocus = true;
  BorderRadiusGeometry borderRadius;
  Color solidColor;
  Color strokeColor;
  Color textColor;
  double fontSize;

  CellInput(
      {Key key,
      this.cellCount = 6,
      this.inputType,
      this.autofocus = true,
      this.inputCompleteCallback,
      this.borderRadius,
      this.solidColor,
      this.strokeColor = Colors.blue,
      this.textColor,
      this.fontSize = 22})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _CellInputState(
        cellCount,
        inputType,
        autofocus,
        inputCompleteCallback,
        borderRadius,
        solidColor,
        strokeColor,
        textColor,
        fontSize);
  }
}

enum InputType {
  password,
  number,
  text,
}

class _CellInputState extends State<CellInput> {
  String inputStr = "";
  int _cellCount = 6;
  InputType _inputType = InputType.number;
  InputCompleteCallback _inputCompleteCallback;
  bool _autofocus = true;
  BorderRadiusGeometry _borderRadius;
  Color _solidColor;
  Color _strokeColor;
  Color _textColor;
  double _fontSize;

  _CellInputState(
      this._cellCount,
      this._inputType,
      this._autofocus,
      this._inputCompleteCallback,
      this._borderRadius,
      this._solidColor,
      this._strokeColor,
      this._textColor,
      this._fontSize);

  TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return new Container(
      height: 48,
      child: Stack(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: getCells(),
          ),
          Container(
            height: 48,
            width: double.infinity,
            child: TextField(
              keyboardType: _keyboardType(),
              inputFormatters: [LengthLimitingTextInputFormatter(_cellCount)],
              decoration: InputDecoration(border: InputBorder.none),
              cursorWidth: 0,
              style: TextStyle(color: Colors.transparent),
              controller: _controller,
              autofocus: _autofocus,
              onChanged: (v) {
                setState(() {
                  inputStr = v;
                  if (v.length == _cellCount) {
                    if (_inputCompleteCallback != null) {
                      _inputCompleteCallback(v);
                    }
                  }
                });
              },
            ),
          ),
        ],
      ),
    );
  }

  String getIndexStr(int index) {
    if (inputStr == null || inputStr.isEmpty) return "";
    if (inputStr.length > index) {
      if (_inputType == InputType.password) {
        return "●";
      } else {
        return inputStr[index];
      }
    } else {
      return "";
    }
  }

  TextInputType _keyboardType() {
    if (_inputType == InputType.number) {
      return TextInputType.number;
    } else if (_inputType == InputType.password) {
      return TextInputType.number;
    } else {
      return TextInputType.text;
    }
  }

  List<Widget> getCells() {
    var cells = <Widget>[];
    for (var i = 0; i < _cellCount; i++) {
      cells.add(Expanded(
        flex: 1,
        child: Center(
          child: Container(
            width: 42,
            height: 42,
            margin: EdgeInsets.only(left: 6, right: 6),
            alignment: Alignment.center,
            child: Text(
              getIndexStr(i),
              style: TextStyle(
                  fontSize: _fontSize,
                  color: _textColor,
                  fontWeight: FontWeight.w600),
            ),
            decoration: BoxDecoration(
                color: _solidColor,
                border: Border.all(width: 1, color: getBoarderColor(i)),
                borderRadius: _borderRadius),
          ),
        ),
      ));
    }

    return cells;
  }

  Color getBoarderColor(int index) {
    if (inputStr == null || inputStr.isEmpty) {
      if (index == 0) {
        return _strokeColor;
      } else {
        return _solidColor;
      }
    } else {
      if (index == inputStr.length) {
        return _strokeColor;
      } else {
        return _solidColor;
      }
    }
  }
}

demo地址

https://github.com/po1arbear/cell_input-flutter

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末称诗,一起剝皮案震驚了整個(gè)濱河市主经,隨后出現(xiàn)的幾起案子偏竟,更是在濱河造成了極大的恐慌,老刑警劉巖膳灶,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異立由,居然都是意外死亡轧钓,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進(jìn)店門锐膜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來聋迎,“玉大人,你說我怎么就攤上這事枣耀∶乖危” “怎么了?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵捞奕,是天一觀的道長牺堰。 經(jīng)常有香客問我,道長颅围,這世上最難降的妖魔是什么伟葫? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮院促,結(jié)果婚禮上筏养,老公的妹妹穿的比我還像新娘。我一直安慰自己常拓,他們只是感情好渐溶,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著弄抬,像睡著了一般茎辐。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天拖陆,我揣著相機(jī)與錄音弛槐,去河邊找鬼。 笑死依啰,一個(gè)胖子當(dāng)著我的面吹牛乎串,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播速警,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼灌闺,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了坏瞄?” 一聲冷哼從身側(cè)響起桂对,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎鸠匀,沒想到半個(gè)月后蕉斜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡缀棍,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年宅此,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片爬范。...
    茶點(diǎn)故事閱讀 40,133評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡父腕,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出青瀑,到底是詐尸還是另有隱情璧亮,我是刑警寧澤,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布斥难,位于F島的核電站枝嘶,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏哑诊。R本人自食惡果不足惜群扶,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望镀裤。 院中可真熱鬧竞阐,春花似錦、人聲如沸暑劝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽铃岔。三九已至汪疮,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間毁习,已是汗流浹背智嚷。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留纺且,地道東北人盏道。 一個(gè)月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像载碌,于是被迫代替她去往敵國和親猜嘱。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評論 2 355