Hi,
So I encountered this issue while trying to show a loader after the submition of the pin. If the pin is wrong, I go back to the initial state and show back the loader.
I used Visibility to achieve this.
The problem is, every time my PinPut widget is re-redered after being hidden, the onSubmit callback is called one time more, e.g. if I hide and show it 6 time, the next time I complete the form the onSubmit callback will be called 6 times.
Here is the content to past in main.dart to reproduce the issue :
import 'package:flutter/material.dart';
import 'package:pinput/pin_put/pin_put.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: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final TextEditingController _pinPutController = TextEditingController();
final FocusNode _pinPutFocusNode = FocusNode();
int counter = 1;
bool showPinPut = true;
BoxDecoration get _pinPutDecoration {
return BoxDecoration(
border: Border.all(color: Colors.black),
borderRadius: BorderRadius.circular(5),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
child: Visibility(
visible: showPinPut == true,
replacement: Container(),
child: PinPut(
fieldsCount: 4,
textStyle: TextStyle(color: Colors.black),
eachFieldHeight: 50,
eachFieldWidth: 50,
followingFieldDecoration: _pinPutDecoration,
preFilledChar: "-",
onSubmit: (String pin) {
print('submitted $counter times');
counter++;
},
focusNode: _pinPutFocusNode,
controller: _pinPutController,
),
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() {
this.showPinPut = !this.showPinPut;
}),
tooltip: 'show/hide',
child: Icon(Icons.remove_red_eye),
),
);
}
}
After hiding and showing the pinput widget 4 times, this is the output I get when I finally submit it :
As a workaround, I tried resetting the _pinputController at the beginning of a build cycle. This fixed the issue, but made it impossible to access the properties (text, etc...), so it clearly is not ideal.
Any thoughts?
EDIT :
As a working workaround, I wrapped both my pinput and my loader in a stack :
// Previous widgets
child: Stack(
children: [
Visibility(
visible: !state.isLoading,
// This is what allows to fix the issue
maintainState: true,
child: PinPut(
// Some PinPut properties
),
),
Visibility(
visible: state.isLoading,
child: Loading(
// Some Loading properties
),
),