pin_input_text_field 2.0.0 copy "pin_input_text_field: ^2.0.0" to clipboard
pin_input_text_field: ^2.0.0 copied to clipboard

outdated

A textField widget to help display different style pin written in pure dart, no extra dependency.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:pin_input_text_field/pin_input_text_field.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: 'Pin Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  /// Default max pin length.
  static final int _pinLength = 4;

  /// Default Text style.
  static final TextStyle _textStyle = TextStyle(
    color: Colors.black,
    fontSize: 24,
  );

  /// Control the input text field.
  TextEditingController _pinEditingController = TextEditingController();

  /// Decorate the outside of the Pin.
  PinDecoration _pinDecoration = UnderlineDecoration(
    textStyle: _textStyle,
    enteredColor: Colors.deepOrange,
  );

  /// Control whether show the obscureCode.
  bool _obscureEnable = false;

  PinEntryType _pinEntryType = PinEntryType.underline;
  Color _solidColor = Colors.purpleAccent;
  bool _solidEnable = false;

  /// Control whether textField is enable.
  bool _enable = true;

  /// Set a pin to the textField.
  void _setPinValue() {
    var text = _generatePin();
    _pinEditingController
      ..text = text
      ..selection = TextSelection.collapsed(offset: text.runes.length);
  }

  String _generatePin() {
    StringBuffer sb = StringBuffer();
    for (int i = 1; i <= _pinLength; i++) {
      sb.write("$i");
    }
    return sb.toString();
  }

  @override
  void initState() {
    _pinEditingController.addListener(() {
      debugPrint('changed pin:${_pinEditingController.text}');
    });
    super.initState();
  }

  @override
  void dispose() {
    _pinEditingController.dispose();
    super.dispose();
  }

  void _selectedMenu(PinEntryType type) {
    _pinEntryType = type;
    switch (type) {
      case PinEntryType.underline:
        setState(() {
          _pinDecoration = UnderlineDecoration(
            textStyle: _textStyle,
            enteredColor: Colors.deepOrange,
            obscureStyle: ObscureStyle(
              isTextObscure: _obscureEnable,
              obscureText: 'πŸ˜‚',
            ),
          );
        });
        break;
      case PinEntryType.boxTight:
        setState(() {
          _pinDecoration = BoxTightDecoration(
            textStyle: _textStyle,
            solidColor: _solidEnable ? _solidColor : null,
            obscureStyle: ObscureStyle(
              isTextObscure: _obscureEnable,
              obscureText: 'πŸ‘Ώ',
            ),
          );
        });
        break;
      case PinEntryType.boxLoose:
        setState(() {
          _pinDecoration = BoxLooseDecoration(
            textStyle: _textStyle,
            enteredColor: Colors.deepOrange,
            solidColor: _solidEnable ? _solidColor : null,
            obscureStyle: ObscureStyle(
              isTextObscure: _obscureEnable,
              obscureText: '☺️',
            ),
          );
        });
        break;
    }
  }

  Widget _buildInListView() {
    return ListView(
      children: <Widget>[
        Row(
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            Icon(Icons.edit),
            Text('1'),
            Expanded(
              child: PinInputTextField(
                decoration: BoxLooseDecoration(
                    textStyle: TextStyle(color: Colors.black)),
                autoFocus: false,
                pinLength: 4,
                controller: _pinEditingController,
              ),
            ),
          ],
        ),
        PinInputTextField(
          pinLength: 4,
          autoFocus: false,
          decoration: BoxLooseDecoration(
            textStyle: TextStyle(
              color: Colors.black,
            ),
          ),
        ),
        Container(
          height: 120,
          color: Colors.purple,
        ),
        Container(
          height: 120,
          color: Colors.pink,
        ),
        Container(
          height: 120,
          color: Colors.deepOrange,
        ),
        Container(
          height: 120,
          color: Colors.teal,
        ),
        Container(
          height: 120,
          color: Colors.cyan,
        ),
      ],
    );
  }

  Widget _buildNormal() {
    return Center(
      // Center is a layout widget. It takes a single child and positions it
      // in the middle of the parent.
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'obscureEnabled',
                style: TextStyle(
                  fontSize: 18,
                ),
              ),
              SizedBox(
                width: 12,
              ),
              Checkbox(
                  value: _obscureEnable,
                  onChanged: (enable) {
                    setState(() {
                      _obscureEnable = enable;
                      _selectedMenu(_pinEntryType);
                    });
                  }),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'solidEnabled',
                style: TextStyle(
                  fontSize: 18,
                ),
              ),
              SizedBox(
                width: 12,
              ),
              Checkbox(
                  value: _solidEnable,
                  onChanged: (enable) {
                    setState(() {
                      _solidEnable = enable;
                      _selectedMenu(_pinEntryType);
                    });
                  }),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'enabled',
                style: TextStyle(fontSize: 18),
              ),
              SizedBox(width: 12),
              Checkbox(
                value: _enable,
                onChanged: (enable) {
                  setState(() {
                    _enable = enable;
                  });
                },
              )
            ],
          ),
          Padding(
            padding: const EdgeInsets.only(left: 12, right: 12, top: 32),
            child: PinInputTextField(
              pinLength: _pinLength,
              decoration: _pinDecoration,
              controller: _pinEditingController,
              autoFocus: true,
              textInputAction: TextInputAction.go,
              enabled: _enable,
              onSubmit: (pin) {
                debugPrint('submit pin:$pin');
              },
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        actions: <Widget>[
          PopupMenuButton<PinEntryType>(
            icon: Icon(Icons.more_vert),
            onSelected: _selectedMenu,
            itemBuilder: (context) {
              return [
                PopupMenuItem(
                  child: Text('underline decoration'),
                  value: PinEntryType.underline,
                ),
                PopupMenuItem(
                  child: Text('box loose decoration'),
                  value: PinEntryType.boxLoose,
                ),
                PopupMenuItem(
                  child: Text('box tight decoration'),
                  value: PinEntryType.boxTight,
                ),
              ];
            },
          ),
        ],
      ),
      body: _buildNormal(),
      floatingActionButton: FloatingActionButton(
        onPressed: _setPinValue,
        tooltip: 'setPinValue',
        child: Icon(Icons.border_color),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
140
likes
0
pub points
97%
popularity

Publisher

verified publishertinocheng.app

A textField widget to help display different style pin written in pure dart, no extra dependency.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on pin_input_text_field