4Q-s-r-o / signature

Flutter plugin that creates a canvas for writing down a signature
MIT License
255 stars 85 forks source link

_controller.isEmpty() constantly listening to changes to signature pad #97

Closed rachelwong closed 6 months ago

rachelwong commented 10 months ago

I am using signature v5.4.1 and I am not able to use the _controller.isNotEmpty() or _controller.isEmpty() to constantly listen for whether the signature pad has been signed or not as user signs.

I notice that _controller.isEmpty() _controller.isNotEmpty() are only updated when the app completely refreshes/reloads.

Below is my workaround attempt, where I have to use a ValueListenableBuilder to listen for the value change when the user has completed drawing. I have referred to the official docs but the official example does not describe my use case.

I realise this is not raising a bug issue (unless someone else also experiences this), but would like to reach out to the development team and the wider community for a best practices approach to using signature.

Thank you kindly for reading 🙏🏼

class TestSignature extends StatefulWidget {
  @override
  State<TestSignature> createState() => _TestSignatureState();
}

class _TestSignatureState extends State<TestSignature> {
  static ValueNotifier<bool> _counterNotifier = ValueNotifier<bool>(false);

  @override
  void initState() {
    super.initState();
    _counterNotifier.value = false;
  }

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

  final SignatureController _controller = SignatureController(
      penStrokeWidth: 2.0,
      penColor: AppTheme.colorBrand0,
      exportBackgroundColor: Colors.transparent,
      exportPenColor: AppTheme.colorBrand0,
      onDrawStart: () => {},
      onDrawEnd: () => {
            _counterNotifier.value = true,
          });

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder(
        valueListenable: _counterNotifier,
        builder: (valueContext, bool listenableValue, _) {
          return Signature(
            key: const Key('signature'),
            controller: _controller,
            height: 150,
            backgroundColor: AppTheme.colorBrand1,
          );
        });
  }
}
MartinHlavna commented 6 months ago

Hi, I cannot test this right now, but maybe you need to call setState to rebuild widget?

MartinHlavna commented 6 months ago

I have tested it and it works as expected. I have updated example to show usage