Home > Mobile >  Working With Seperate Files in Flutter/Dart
Working With Seperate Files in Flutter/Dart

Time:04-06

I am newbie at Dart and OOP.I have one input.dart file for Text Form Fields and login.dart file to conduct login.My problem is I want to acces text controller (located in input.dart) from login.dart.

I created getter method to obtain, (controller.text) data but I have encountered with Initilazation Error.

How Can I acces controller text(which is basically user input) from another file?

input.dart

class InputAlanState extends State<InputAlan> {
  late TextEditingController _emailKontroller;
  late TextEditingController _sifreKontroller;

  @override
  void initState() {
    super.initState();
    _emailKontroller = TextEditingController();
    _sifreKontroller = TextEditingController();
  }

  @override
  void dispose() {
    _emailKontroller.dispose();
    _sifreKontroller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.tur == "email") {
      return Padding(
        padding: const EdgeInsets.only(top: 50, left: 20, right: 20),
        child: TextFormField(
          controller: _emailKontroller,
          autofocus: true,
          decoration: const InputDecoration(
              labelText: "E - Mail",
              hintText: "E-Mail",
              prefixIcon: Icon(Icons.email_outlined),
              suffixIcon: Icon(Icons.lock),
              border: OutlineInputBorder(
                  borderRadius: BorderRadius.all(Radius.circular(20.0)))),
        ),
      );
    } else if (widget.tur == "sifre") {
      return Padding(
        padding:
            const EdgeInsets.only(top: 40, left: 20, right: 20, bottom: 15),
        child: TextFormField(
          controller: _sifreKontroller,
          obscureText: true,
          decoration: const InputDecoration(
              labelText: "Password",
              hintText: "Password",
              prefixIcon: Icon(Icons.password_sharp),
              suffixIcon: Icon(Icons.lock),
              border: OutlineInputBorder(
                  borderRadius: BorderRadius.all(Radius.circular(20.0)))),
        ),
      );
    } else {
      return Padding(
        padding: const EdgeInsets.only(top: 50, left: 20, right: 20),
        child: TextFormField(
          decoration: const InputDecoration(
              labelText: "E - Mail",
              hintText: "E-Mail",
              prefixIcon: Icon(Icons.email_outlined),
              suffixIcon: Icon(Icons.lock),
              border: OutlineInputBorder(
                  borderRadius: BorderRadius.all(Radius.circular(20.0)))),
        ),
      );
    }
  }
}

login.py

 Widget build(BuildContext context) {
    return OutlinedButton(
      onPressed: () {
        InputAlan inputAlan = InputAlan("email");
        String email = inputAlan.email;
        String password = inputAlan.sifre;
        Login login = login(email, sifre);
        girisYap.girisYap(context);
      },
      child: const Text("SIGN IN"),
      style: OutlinedButton.styleFrom(
          primary: const Color(0xFF166FC0),
          side: const BorderSide(color: Color(0xFF0FA9EA), width: 2),
          shape: const RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(10)))),
    );
  }
}

CodePudding user response:

to access variables from another state (in your case from InputAlanState) in flutter you have multiple options, the simplest way would be to use a GlobalKey, so in your code you can access InputAlanState's controllers from your login you can use this code in your OutlinedButton:

 GlobalKey<InputAlanState> myKey = GlobalKey();

 myKey.currentState!._emailKontroller; //here

CodePudding user response:

You're putting a widget in a function parameter. In this way the widget cannot be rendered and it just can't work. I suggest you take a look at how to build flutter layouts to grasp the basics. You probably want to build something like this:

enum Field { mail, password }

class MyApp extends StatelessWidget {

  final TextEditingController mailController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          body: Column(
            children: [
              InputAlan(fieldType: Field.mail, textEditingController: mailController,),
              InputAlan(fieldType: Field.password, textEditingController: passwordController,),
              OutlinedButton(
                onPressed: () {
                   String email = mailController.text;
                   String sifre = passwordController.text;
                  // Login login = login(email, sifre);
                  // girisYap.girisYap(context);
                },
                child: const Text("SIGN IN"),
                style: OutlinedButton.styleFrom(
                    primary: const Color(0xFF166FC0),
                    side: const BorderSide(color: Color(0xFF0FA9EA), width: 2),
                    shape: const RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(10)))),
              ),
            ],
          )
      ),
    );
  }
}

TextField widget

class InputAlan extends StatefulWidget {
  const InputAlan({
    Key? key,
    required this.fieldType,
    required this.textEditingController,
  }) : super(key: key);

  final Field fieldType;
  final TextEditingController textEditingController;

  @override
  State<InputAlan> createState() => _InputAlanState();
}

class _InputAlanState extends State<InputAlan> {
  @override
  Widget build(BuildContext context) {
    final isMailField = widget.fieldType == Field.mail;
    return Padding(
      padding: const EdgeInsets.only(top: 50, left: 20, right: 20),
      child: TextFormField(
        controller: widget.textEditingController,
        autofocus: widget.fieldType == Field.mail,
        obscureText: !isMailField,
        decoration: InputDecoration(
            labelText: isMailField ? "E - Mail" : "Password",
            hintText: isMailField ? "E-Mail" : "Password",
            prefixIcon:
                Icon(isMailField ? Icons.email_outlined : Icons.password_sharp),
            suffixIcon: const Icon(Icons.lock),
            border: const OutlineInputBorder(
              borderRadius: BorderRadius.all(Radius.circular(20.0)),
            )),
      ),
    );
  }
}
  • Related