1

I have this code:

final _text = TextEditingController();

Widget _formatoCampo(String str_campo, Icon icon_campo) {
return TextField(
  controller: _text,
  onChanged: (text) {
    setState(() {});
  },
  autofocus: true,
  decoration: InputDecoration(
    filled: true,
    fillColor: Colors.white,
    focusedBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.red[100]),
      borderRadius: BorderRadius.circular(25.7),
    ),
    hintText: str_campo,
    suffixIcon: IconButton(
      icon: icon_campo,
    ),
  ),
);

}

That I;m using to build a widget, this:

Widget build(BuildContext context) {
final height = MediaQuery.of(context).size.height;
return Scaffold(
  body: Container(
    height: height,
    decoration: BoxDecoration(
        gradient: RadialGradient(
            radius: 0.7, colors: [Colors.red[100], Colors.white])),
    child: Stack(
      children: <Widget>[
        Positioned(
          top: -MediaQuery.of(context).size.height * .15,
          right: -MediaQuery.of(context).size.width * .4,
          child: BezierContainer(),
        ),
        Container(
          padding: EdgeInsets.symmetric(horizontal: 20),
          child: Column(
            children: [
              SizedBox(height: height * .1),
              titulo(),
              Expanded(
                child: Container(
                  margin: const EdgeInsets.all(8.0),
                  padding: const EdgeInsets.all(3.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      _formatoCampo('Introduzca su nombre y apellido',
                          Icon(Icons.accessibility_new_sharp)),
                      _separadores(height),
                      _formatoDescripcion(
                          'Con este nombre se te identificará al momento de realizar los pedidos'),
                    ],
                  ),
                ),
              ),
              ButtonElevationWithIcon(
                enable: _text.text != '' ? true : false,
                buttonText: 'Siguiente',
                onPressedButton: () {},
              ),
              _inicionsesionLabel(),
            ],
          ),
        ),
        Positioned(top: 40, left: 0, child: _atrasBoton()),
      ],
    ),
  ),
);

}

The thing is that this widget I use on anothers screens, I want to put the '_formatoCampo' method in another class in order to call it from multiple places. How can I pass the TextEditingController() as an argument and also the setState()?

PartnerTech
  • 119
  • 1
  • 9

1 Answers1

0
abstract class IsSilly {
  void makePeopleLaugh();
}

class Clown implements IsSilly {
  void makePeopleLaugh() {
    // Here is where the magic happens
  }
}

class Comedian implements IsSilly {
  void makePeopleLaugh() {
    // Here is where the magic happens
  }
}

Use a Abstract Class to define your method .then you can do this.