最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Flutter驗(yàn)證碼輸入框的2種方法實(shí)現(xiàn)

 更新時(shí)間:2021年12月24日 16:11:51   作者:堅(jiān)果前端の博客  
本文主要介紹了Flutter驗(yàn)證碼輸入框的2種方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文向您展示了在 Flutter 中實(shí)現(xiàn)完美的驗(yàn)證碼輸入框幾種不同方法。

重點(diǎn)是什么?

真實(shí)世界的 完美的驗(yàn)證碼輸入框或 PIN 輸入 UI 通常滿足以下最低要求:

  • 有4個(gè)或6個(gè)文本域,每個(gè)文本域只能接受1個(gè)字符(通常是一個(gè)數(shù)字)
  • 輸入數(shù)字后自動(dòng)聚焦下一個(gè)字段

您經(jīng)常在需要電話號(hào)碼確認(rèn)、電子郵件或雙因素身份驗(yàn)證的應(yīng)用程序中看到此功能。

從頭開始制作 OTP 字段

應(yīng)用預(yù)覽

image-20211220134159049

此示例創(chuàng)建一個(gè)簡單的 OTP 屏幕。首先,聚焦第一個(gè)輸入字段。當(dāng)您輸入一個(gè)數(shù)字時(shí),光標(biāo)將自動(dòng)移動(dòng)到下一個(gè)字段。當(dāng)按下提交按鈕時(shí),您輸入的 OTP 代碼將顯示在屏幕上。

以下是它的工作原理:

測試此應(yīng)用程序時(shí),您應(yīng)該使用模擬器的軟鍵盤而不是計(jì)算機(jī)的硬件鍵盤。

代碼

創(chuàng)建一個(gè)名為OtpInput的可重用小部件:

// Create an input widget that takes only one digit
class OtpInput extends StatelessWidget {
  final TextEditingController controller;
  final bool autoFocus;
  const OtpInput(this.controller, this.autoFocus, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 60,
      width: 50,
      child: TextField(
        autofocus: autoFocus,
        textAlign: TextAlign.center,
        keyboardType: TextInputType.number,
        controller: controller,
        maxLength: 1,
        cursorColor: Theme.of(context).primaryColor,
        decoration: const InputDecoration(
            border: OutlineInputBorder(),
            counterText: '',
            hintStyle: TextStyle(color: Colors.black, fontSize: 20.0)),
        onChanged: (value) {
          if (value.length == 1) {
            FocusScope.of(context).nextFocus();
          }
        },
      ),
    );
  }
}

main.dart 中的完整源代碼和解釋(我將OtpInput類放在文件底部):

import 'dart:math' as math;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:async/async.dart';
import 'package:flutter/scheduler.dart';
import 'package:url_strategy/url_strategy.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Hide the debug banner
      debugShowCheckedModeBanner: false,
      title: '堅(jiān)果',
      theme: ThemeData(
        primarySwatch: Colors.indigo,
      ),
      home: const HomeScreen(),
    );
  }
}

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

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  String _imageUrl =
      'https://luckly007.oss-cn-beijing.aliyuncs.com/image/image-20211124085239175.png';
  double _fontSize = 20;
  String _title = "堅(jiān)果公眾號(hào)";
  // 4 text editing controllers that associate with the 4 input fields
  final TextEditingController _fieldOne = TextEditingController();
  final TextEditingController _fieldTwo = TextEditingController();
  final TextEditingController _fieldThree = TextEditingController();
  final TextEditingController _fieldFour = TextEditingController();

  // This is the entered code
  // It will be displayed in a Text widget
  String? _otp;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(_title),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const Text('請輸入驗(yàn)證碼'),
          const SizedBox(
            height: 30,
          ),
          // Implement 4 input fields
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              OtpInput(_fieldOne, true),
              OtpInput(_fieldTwo, false),
              OtpInput(_fieldThree, false),
              OtpInput(_fieldFour, false)
            ],
          ),
          const SizedBox(
            height: 30,
          ),
          ElevatedButton(
              onPressed: () {
                setState(() {
                  _otp = _fieldOne.text +
                      _fieldTwo.text +
                      _fieldThree.text +
                      _fieldFour.text;
                });
              },
              child: const Text('提交')),
          const SizedBox(
            height: 30,
          ),
          // Display the entered OTP code
          Text(
            _otp ?? '驗(yàn)證碼',
            style: const TextStyle(fontSize: 30),
          )
        ],
      ),
    );
  }
}

// Create an input widget that takes only one digit
class OtpInput extends StatelessWidget {
  final TextEditingController controller;
  final bool autoFocus;
  const OtpInput(this.controller, this.autoFocus, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 60,
      width: 50,
      child: TextField(
        autofocus: autoFocus,
        textAlign: TextAlign.center,
        keyboardType: TextInputType.number,
        controller: controller,
        maxLength: 1,
        cursorColor: Theme.of(context).primaryColor,
        decoration: const InputDecoration(
            border: OutlineInputBorder(),
            counterText: '',
            hintStyle: TextStyle(color: Colors.black, fontSize: 20.0)),
        onChanged: (value) {
          if (value.length == 1) {
            FocusScope.of(context).nextFocus();
          }
        },
      ),
    );
  }
}

使用第三個(gè)包

為了僅用幾行代碼快速實(shí)現(xiàn)您的目標(biāo),您可以使用第三方插件。在我們的例子中一些好的是pin_code_fields,otp_text_field等。 下面的例子將使用pin_code_fileds,它提供了很多很棒的功能:

image-20211220134456679

  • 自動(dòng)將下一個(gè)字段集中在打字上,將上一個(gè)字段集中在委派上
  • 可以設(shè)置為任意長度
  • 高度可定制
  • 輸入文本的 3 種不同類型的動(dòng)畫
  • 動(dòng)畫活動(dòng)、非活動(dòng)、選定和禁用字段顏色切換
  • 自動(dòng)對焦選項(xiàng)
  • 從剪貼板粘貼 OTP 代碼

您還可以在終端窗口中看到您輸入的字符:

img

代碼

1.安裝插件:

flutter pub add pin_code_fields

2.最終代碼:

import 'dart:math' as math;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:async/async.dart';
import 'package:pin_code_fields/pin_code_fields.dart';
import 'package:url_strategy/url_strategy.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Hide the debug banner
      debugShowCheckedModeBanner: false,
      title: '堅(jiān)果',
      theme: ThemeData(
        primarySwatch: Colors.indigo,
      ),
      home: const HomeScreen(),
    );
  }
}

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

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  String _imageUrl =
      'https://luckly007.oss-cn-beijing.aliyuncs.com/image/image-20211124085239175.png';
  double _fontSize = 20;
  String _title = "堅(jiān)果公眾號(hào)";
  // 4 text editing controllers that associate with the 4 input fields
  TextEditingController textEditingController = TextEditingController();
  String currentText = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(_title),
      ),
      body: Padding(
        padding: const EdgeInsets.all(30),
        child: Center(
          child: PinCodeTextField(
            length: 6,
            obscureText: false,
            animationType: AnimationType.fade,
            pinTheme: PinTheme(
              shape: PinCodeFieldShape.box,
              borderRadius: BorderRadius.circular(5),
              fieldHeight: 50,
              fieldWidth: 40,
              activeFillColor: Colors.white,
            ),
            animationDuration: const Duration(milliseconds: 300),
            backgroundColor: Colors.blue.shade50,
            enableActiveFill: true,
            controller: textEditingController,
            onCompleted: (v) {
              debugPrint("Completed");
            },
            onChanged: (value) {
              debugPrint(value);
              setState(() {
                currentText = value;
              });
            },
            beforeTextPaste: (text) {
              return true;
            },
            appContext: context,
          ),
        ),
      ),
    );
  }
}

結(jié)論

我們已經(jīng)介紹了 2 個(gè)在 Flutter 中創(chuàng)建現(xiàn)代優(yōu)雅的 完美的驗(yàn)證碼輸入框/PIN 輸入字段的示例。

到此這篇關(guān)于Flutter驗(yàn)證碼輸入框的2種方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Flutter驗(yàn)證碼輸入框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatis查詢、新增、更新與刪除操作指南

    MyBatis查詢、新增、更新與刪除操作指南

    這篇文章主要給大家介紹了關(guān)于MyBatis查詢、新增、更新與刪除操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用MyBatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Mybatis動(dòng)態(tài)SQL之if、choose、where、set、trim、foreach標(biāo)記實(shí)例詳解

    Mybatis動(dòng)態(tài)SQL之if、choose、where、set、trim、foreach標(biāo)記實(shí)例詳解

    動(dòng)態(tài)SQL就是動(dòng)態(tài)的生成SQL。接下來通過本文給大家介紹Mybatis動(dòng)態(tài)SQL之if、choose、where、set、trim、foreach標(biāo)記實(shí)例詳解的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2016-09-09
  • Java I/O深入學(xué)習(xí)之File和RandomAccessFile

    Java I/O深入學(xué)習(xí)之File和RandomAccessFile

    這篇文章主要介紹了Java I/O深入學(xué)習(xí)之File和RandomAccessFile, I/O系統(tǒng)即輸入/輸出系統(tǒng),對于一門程序語言來說,創(chuàng)建一個(gè)好的輸入/輸出系統(tǒng)并非易事。在充分理解Java I/O系統(tǒng)以便正確地運(yùn)用之前,我們需要學(xué)習(xí)相當(dāng)數(shù)量的類。,需要的朋友可以參考下
    2019-06-06
  • Springboot Thymeleaf字符串對象實(shí)例解析

    Springboot Thymeleaf字符串對象實(shí)例解析

    這篇文章主要介紹了Springboot Thymeleaf字符串對象實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2007-09-09
  • 一篇文章教你將JAVA的RabbitMQz與SpringBoot整合

    一篇文章教你將JAVA的RabbitMQz與SpringBoot整合

    這篇文章主要介紹了如何將JAVA的RabbitMQz與SpringBoot整合,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2021-09-09
  • 深入理解JSON及其在Java中的應(yīng)用小結(jié)

    深入理解JSON及其在Java中的應(yīng)用小結(jié)

    json它是一種輕量級(jí)的數(shù)據(jù)交換格式,由于其易于閱讀和編寫,同時(shí)也易于機(jī)器解析和生成,因此廣泛應(yīng)用于網(wǎng)絡(luò)數(shù)據(jù)交換和配置文件,這篇文章主要介紹了深入理解JSON及其在Java中的應(yīng)用,需要的朋友可以參考下
    2023-12-12
  • Spring Boot面試題總結(jié)

    Spring Boot面試題總結(jié)

    這篇文章主要介紹了Spring Boot面試題總結(jié),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • tio-boot整合hotswap-classloader實(shí)現(xiàn)熱加載方法實(shí)例

    tio-boot整合hotswap-classloader實(shí)現(xiàn)熱加載方法實(shí)例

    這篇文章主要為大家介紹了tio-boot整合hotswap-classloader實(shí)現(xiàn)熱加載方法實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Java如何實(shí)現(xiàn)保證線程安全

    Java如何實(shí)現(xiàn)保證線程安全

    這篇文章主要介紹了Java如何實(shí)現(xiàn)保證線程安全問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Java 8 Stream filter流式過濾器詳解

    Java 8 Stream filter流式過濾器詳解

    本文介紹了Java 8的Stream API中的filter方法,展示了如何使用lambda表達(dá)式根據(jù)條件過濾流式數(shù)據(jù),通過實(shí)際代碼示例,展示了filter方法的高效性以及如何結(jié)合findAny和orElse方法處理更復(fù)雜的情況,適合Java新手和追求代碼優(yōu)雅的開發(fā)者閱讀,感興趣的朋友一起看看吧
    2025-02-02

最新評(píng)論

阿克| 阿巴嘎旗| 全州县| 额敏县| 正安县| 儋州市| 南溪县| 英德市| 衢州市| 苍南县| 高密市| 封开县| 宜良县| 炎陵县| 静乐县| 桂林市| 天门市| 环江| 宝山区| 长海县| 乐陵市| 阿克苏市| 曲沃县| 广丰县| 浏阳市| 红安县| 两当县| 青龙| 始兴县| 阿荣旗| 专栏| 吐鲁番市| 文化| 缙云县| 嘉义市| 特克斯县| 上思县| 汾阳市| 寿宁县| 葫芦岛市| 霍山县|