LiewJunTung / pin_code_text_field

A highly customisable Flutter widget for entering pin code. Suitable for use cases such as login and OTP.
MIT License
376 stars 78 forks source link

When setting both, onTextChanged and Controller the onTextChanged callback is called twice #66

Open carlos-enzo opened 2 years ago

carlos-enzo commented 2 years ago

Describe the bug When we set a callback for onTextChanged and a controller the callback is called twice.

To Reproduce example code:

import 'package:flutter/material.dart';
import 'package:pin_code_text_field/pin_code_text_field.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Connectivity example app'),
      ),
      body: Center(
        child: PinCodeTextField(
          onTextChanged: onTextChanged, //widget.onTextChanged,
          controller: _controller,
          pinBoxColor: Colors.transparent,
          pinBoxBorderWidth: 1.0,
          pinBoxOuterPadding: const EdgeInsets.all(2.5),
          pinBoxWidth: 40,
          pinBoxHeight: 40,
          pinBoxRadius: 2.0,
          focusNode: FocusNode(),
        ),
      ),
    );
  }

  void onTextChanged(String value) {
    print('value: $value');
  }
}

Expected behavior We can set the controller and the callback and when the text changes, the callback is only called once.

flutter doctor Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 2.10.3, on macOS 12.1 21C52 darwin-x64, locale en-US) [✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2020.3) [✓] VS Code (version 1.65.1) [✓] Connected device (3 available) [✓] HTTP Host Availability