pinput 0.2.6 pinput: ^0.2.6 copied to clipboard
Flutter package to create Pin code input text field with every pixel customization possibility and beautiful animations
import 'package:flutter/material.dart';
import 'package:pinput/pin_put/pin_put.dart';
void main() => runApp(PinPutTest());
class PinPutTest extends StatefulWidget {
@override
PinPutTestState createState() => PinPutTestState();
}
class PinPutTestState extends State<PinPutTest> {
final _formKey = GlobalKey<FormState>();
final TextEditingController _pinPutController = TextEditingController();
final FocusNode _pinPutFocusNode = FocusNode();
BuildContext _context;
final PageController _pageController = PageController(initialPage: 1);
int _pageIndex = 0;
Widget onlySelectedBorderPinPut() {
final BoxDecoration pinPutDecoration = BoxDecoration(
color: const Color.fromRGBO(235, 236, 237, 1),
borderRadius: BorderRadius.circular(5.0),
);
return Form(
key: _formKey,
child: GestureDetector(
onLongPress: () {
print(_formKey.currentState.validate());
},
child: PinPut(
validator: (s) {
if (s.contains('1')) return null;
return 'NOT VALID';
},
autovalidateMode: AutovalidateMode.onUserInteraction,
withCursor: true,
fieldsCount: 5,
fieldsAlignment: MainAxisAlignment.spaceAround,
textStyle: const TextStyle(fontSize: 25.0, color: Colors.black),
eachFieldMargin: EdgeInsets.all(0),
eachFieldWidth: 45.0,
eachFieldHeight: 55.0,
onSubmit: (String pin) => _showSnackBar(pin),
focusNode: _pinPutFocusNode,
controller: _pinPutController,
submittedFieldDecoration: pinPutDecoration,
selectedFieldDecoration: pinPutDecoration.copyWith(
color: Colors.white,
border: Border.all(
width: 2,
color: const Color.fromRGBO(160, 215, 220, 1),
),
),
followingFieldDecoration: pinPutDecoration,
pinAnimationType: PinAnimationType.scale,
),
),
);
}
Widget darkRoundedPinPut() {
final BoxDecoration pinPutDecoration = BoxDecoration(
color: const Color.fromRGBO(25, 21, 99, 1),
borderRadius: BorderRadius.circular(20.0),
);
return PinPut(
eachFieldWidth: 65.0,
eachFieldHeight: 65.0,
fieldsCount: 4,
focusNode: _pinPutFocusNode,
controller: _pinPutController,
onSubmit: (String pin) => _showSnackBar(pin),
submittedFieldDecoration: pinPutDecoration,
selectedFieldDecoration: pinPutDecoration,
followingFieldDecoration: pinPutDecoration,
pinAnimationType: PinAnimationType.scale,
textStyle: const TextStyle(color: Colors.white, fontSize: 20.0),
);
}
Widget animatingBorders() {
final BoxDecoration pinPutDecoration = BoxDecoration(
border: Border.all(color: Colors.deepPurpleAccent),
borderRadius: BorderRadius.circular(15.0),
);
return PinPut(
fieldsCount: 5,
eachFieldHeight: 40.0,
onSubmit: (String pin) => _showSnackBar(pin),
focusNode: _pinPutFocusNode,
controller: _pinPutController,
submittedFieldDecoration: pinPutDecoration.copyWith(
borderRadius: BorderRadius.circular(20.0),
),
selectedFieldDecoration: pinPutDecoration,
followingFieldDecoration: pinPutDecoration.copyWith(
borderRadius: BorderRadius.circular(5.0),
border: Border.all(
color: Colors.deepPurpleAccent.withOpacity(.5),
),
),
);
}
Widget boxedPinPutWithPreFilledSymbol() {
final BoxDecoration pinPutDecoration = BoxDecoration(
color: const Color.fromRGBO(119, 125, 226, 1),
borderRadius: BorderRadius.circular(5.0),
);
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(3.0),
border: Border.all(color: Colors.white),
),
padding: const EdgeInsets.all(20.0),
child: PinPut(
fieldsCount: 5,
preFilledWidget: FlutterLogo(),
textStyle: const TextStyle(fontSize: 25.0, color: Colors.white),
eachFieldWidth: 50.0,
eachFieldHeight: 50.0,
onSubmit: (String pin) => _showSnackBar(pin),
focusNode: _pinPutFocusNode,
controller: _pinPutController,
submittedFieldDecoration: pinPutDecoration,
selectedFieldDecoration: pinPutDecoration.copyWith(color: Colors.white),
followingFieldDecoration: pinPutDecoration,
),
);
}
Widget justRoundedCornersPinPut() {
final BoxDecoration pinPutDecoration = BoxDecoration(
color: const Color.fromRGBO(43, 46, 66, 1),
borderRadius: BorderRadius.circular(10.0),
border: Border.all(
color: const Color.fromRGBO(126, 203, 224, 1),
),
);
return Padding(
padding: const EdgeInsets.all(30.0),
child: PinPut(
fieldsCount: 4,
textStyle: const TextStyle(fontSize: 25.0, color: Colors.white),
eachFieldWidth: 40.0,
eachFieldHeight: 55.0,
onSubmit: (String pin) => _showSnackBar(pin),
focusNode: _pinPutFocusNode,
controller: _pinPutController,
submittedFieldDecoration: pinPutDecoration,
selectedFieldDecoration: pinPutDecoration,
followingFieldDecoration: pinPutDecoration,
pinAnimationType: PinAnimationType.fade,
),
);
}
@override
Widget build(BuildContext context) {
final List<Widget> _pinPuts = [
onlySelectedBorderPinPut(),
darkRoundedPinPut(),
animatingBorders(),
boxedPinPutWithPreFilledSymbol(),
justRoundedCornersPinPut(),
];
final List<Color> _bgColors = [
Colors.white,
const Color.fromRGBO(43, 36, 198, 1),
Colors.white,
const Color.fromRGBO(75, 83, 214, 1),
const Color.fromRGBO(43, 46, 66, 1),
];
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
platform: TargetPlatform.iOS,
),
home: Scaffold(
backgroundColor: Colors.white,
body: Stack(
fit: StackFit.passthrough,
children: <Widget>[
Builder(
builder: (context) {
_context = context;
return AnimatedContainer(
color: _bgColors[_pageIndex],
duration: const Duration(milliseconds: 200),
padding: const EdgeInsets.all(40.0),
child: PageView(
scrollDirection: Axis.vertical,
controller: _pageController,
onPageChanged: (index) {
setState(() => _pageIndex = index);
},
children: _pinPuts.map((p) {
return FractionallySizedBox(
heightFactor: 1.0,
child: Center(child: p),
);
}).toList(),
),
);
},
),
_bottomAppBar,
],
),
),
);
}
Widget get _bottomAppBar {
return Positioned(
bottom: 0,
left: 0,
right: 0,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
FlatButton(
onPressed: () => _pinPutFocusNode.requestFocus(),
child: const Text('Focus'),
),
FlatButton(
onPressed: () => _pinPutFocusNode.unfocus(),
child: const Text('Unfocus'),
),
FlatButton(
onPressed: () => _pinPutController.text = '',
child: const Text('Clear All'),
),
FlatButton(
child: Text('Paste'),
onPressed: () => _pinPutController.text = '234',
),
],
),
);
}
void _showSnackBar(String pin) {
final snackBar = SnackBar(
duration: const Duration(seconds: 3),
content: Container(
height: 80.0,
child: Center(
child: Text(
'Pin Submitted. Value: $pin',
style: const TextStyle(fontSize: 25.0),
),
),
),
backgroundColor: Colors.deepPurpleAccent,
);
Scaffold.of(_context)
..hideCurrentSnackBar()
..showSnackBar(snackBar);
}
}