0

im Very new in flutter . i dont know what to do to fix this .

im trying to Use Flutter Plugin : flutter_numpad_widget

Here my Full code:

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

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

bool _confirmEnabled = false;

class _MyAppState extends State<MyApp> {
  int maxRawLength;
  final NumpadController _numpadController = NumpadController(
    format: NumpadFormat.NONE,
    hintText: "Ketikkan NIP",
    onInputValidChange: (bool valid) => setState(() {
      _confirmEnabled = valid;
    }),
  );
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Numpad Example',
        theme: ThemeData(
            primarySwatch: Colors.amber,
            buttonTheme: ButtonThemeData(
                textTheme: ButtonTextTheme.normal,
                buttonColor: Colors.blueGrey[300],
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(30))))),
        home: Scaffold(
          appBar: AppBar(
            title: Text('Numpad Example'),
          ),
          body: Container(
            child: Column(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: NumpadText(
                    style: TextStyle(fontSize: 30),
                    controller: _numpadController,
                  ),
                ),
                Expanded(
                  child: Numpad(
                    controller: _numpadController,
                    buttonTextSize: 40,
                  ),
                )
              ],
            ),
          ),
        ));
  }
}

im following the documentation here : onInputValidChange

but in this line its keep getting me Error "The instance member 'setState' can't be accessed in an initializer.":

onInputValidChange: (bool valid) => setState(() {
      _confirmEnabled = valid;
    }),

im Already searching in few days and gets nothing.

thanks for your help priciateit

2 Answers2

2

To add some explanation to your problem and I think in general is also valid:

You should init all your state properties in initState. If you have like bool flags or primitive properties that's fine but objects, in general, you should init in ```initState````. In your case:

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

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

bool _confirmEnabled = false;

class _MyAppState extends State<MyApp> {
  int maxRawLength;
  final NumpadController _numpadController; // this is the declaration

@override
void initState() {
  super.initState();
  _numpadController = NumpadController( // here is the init
    format: NumpadFormat.NONE,
    hintText: "Ketikkan NIP",
    onInputValidChange: (bool valid) => setState(() {
      _confirmEnabled = valid;
    }),
  );
}

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Numpad Example',
        theme: ThemeData(
            primarySwatch: Colors.amber,
            buttonTheme: ButtonThemeData(
                textTheme: ButtonTextTheme.normal,
                buttonColor: Colors.blueGrey[300],
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(30))))),
        home: Scaffold(
          appBar: AppBar(
            title: Text('Numpad Example'),
          ),
          body: Container(
            child: Column(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: NumpadText(
                    style: TextStyle(fontSize: 30),
                    controller: _numpadController,
                  ),
                ),
                Expanded(
                  child: Numpad(
                    controller: _numpadController,
                    buttonTextSize: 40,
                  ),
                )
              ],
            ),
          ),
        ));
  }
}
danypata
  • 9,895
  • 1
  • 31
  • 44
1

You should declare your state inside The state widget like this:

class _MyAppState extends State<MyApp> {
   int maxRawLength;
   bool _confirmEnabled = false; // here 
   ....
   onInputValidChange: (bool valid) => setState(() {
      _confirmEnabled = valid;
   }),
  ...
bouraine
  • 61
  • 3
  • onInputValidChange . but acording to the documentation it have to be inside the NumpadController properties . here https://pub.dev/documentation/flutter_numpad_widget/latest/flutter_numpad_widget/NumpadController-class.html . – Muhammad Irfan Shidqi Laksono Jan 19 '21 at 19:58