Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Search Gists
Sign in Sign up

Instantly share code, notes, and snippets.

@dubemezeagwu
Last activeAugust 2, 2023 18:42
    • Star(0)You must be signed in to star a gist
    • Fork(0)You must be signed in to fork a gist
    Save dubemezeagwu/e0130f9886f168fb450c375cfed49733 to your computer and use it in GitHub Desktop.
    Card Number InputFormatter - format payment card inputs automatically for month and card numbers
    classCardMonthInputFormatterextendsTextInputFormatter {
    @override
    TextEditingValueformatEditUpdate(
    TextEditingValue oldValue,TextEditingValue newValue) {
    var newText= newValue.text;
    if (newValue.selection.baseOffset==0) {
    return newValue;
    }
    var buffer=StringBuffer();
    for (int i=0; i< newText.length; i++) {
    buffer.write(newText[i]);
    var nonZeroIndex= i+1;
    if (nonZeroIndex%2==0&& nonZeroIndex!= newText.length) {
    buffer.write('/');
    }
    }
    var string= buffer.toString();
    return newValue.copyWith(
    text: string,
    selection:TextSelection.collapsed(offset: string.length));
    }
    }
    @dubemezeagwu
    classCardNumberInputFormatterextendsTextInputFormatter {
    @override
    TextEditingValueformatEditUpdate(
    TextEditingValue oldValue,TextEditingValue newValue) {
    var newText= newValue.text;
    if (newValue.selection.baseOffset==0) {
    return newValue;
    }
    var buffer=StringBuffer();
    for (int i=0; i< newText.length; i++) {
    buffer.write(newText[i]);
    var nonZeroIndex= i+1;
    if (nonZeroIndex%4==0&& nonZeroIndex!= newText.length) {
    buffer.write(" ");// Single space
    }
    }
    var string= buffer.toString();
    return newValue.copyWith(
    text: string,
    selection:TextSelection.collapsed(offset: string.length));
    }
    }
    Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

    [8]ページ先頭

    ©2009-2025 Movatter.jp