T
T
trickster20192022-03-25 16:00:23
Dart
trickster2019, 2022-03-25 16:00:23

Why doesn't the function see the text controller variable?

I tried to create a function to display the entered text, but it returned "Error: The getter '_nameController' isn't defined for the class '_Registration'."

import 'package:flutter/material.dart';
import 'package:flutter_/style.dart';

class Registration extends StatefulWidget {
  @override
  State<Registration> createState() => _Registration();
}

class _Registration extends State<Registration> {
  Widget button(text, linc) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
          primary: s.buttonColor, minimumSize: Size(2500, 70)),
      onPressed: () {
        Navigator.pushNamed(context, linc);
        submitForm();
      },
      child: Text("${text}", style: TextStyle(color: Colors.white)),
    );
  }

  @override
  Widget build(BuildContext context) {
    final _nameController = TextEditingController();
    final _passwordController = TextEditingController();
    return Scaffold(
        backgroundColor: s.backgroundColor,
        appBar: AppBar(
          centerTitle: true,
          backgroundColor: s.appBarColor,
          title: Text("Регистрация", style: TextStyle(color: Colors.white)),
        ),
        body: Center(
          child: Container(
              padding: EdgeInsets.symmetric(horizontal: 100, vertical: 100),
              child: Column(
                children: [
                  Icon(
                    Icons.watch_later,
                    color: s.buttonColor,
                    size: 150,
                  ),
                  SizedBox(
                    height: 50,
                  ),
                  TextFormField(
                    controller: _nameController,
                    decoration: InputDecoration(labelText: "Имя пользователя"),
                  ),
                  TextFormField(
                    controller: _passwordController,
                    decoration: InputDecoration(labelText: "Пароль"),
                  ),
                  SizedBox(
                    height: 50,
                  ),
                  button("Зарегистрироваться", "/home_page_schedule")
                ],
              )),
        ));
  }

  void submitForm() {
    print("Name ${_nameController.text}");
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Alexandrovich, 2022-03-25
@trickster2019

This is how it should work. Read about scope.

import 'package:flutter/material.dart';
import 'package:flutter_/style.dart';

class Registration extends StatefulWidget {
  @override
  State<Registration> createState() => _Registration();
}

class _Registration extends State<Registration> {

   final _nameController = TextEditingController();
    final _passwordController = TextEditingController();

  Widget button(text, linc) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
          primary: s.buttonColor, minimumSize: Size(2500, 70)),
      onPressed: () {
        Navigator.pushNamed(context, linc);
        submitForm();
      },
      child: Text("${text}", style: TextStyle(color: Colors.white)),
    );
  }

  @override
  Widget build(BuildContext context) {
 
    return Scaffold(
        backgroundColor: s.backgroundColor,
        appBar: AppBar(
          centerTitle: true,
          backgroundColor: s.appBarColor,
          title: Text("Регистрация", style: TextStyle(color: Colors.white)),
        ),
        body: Center(
          child: Container(
              padding: EdgeInsets.symmetric(horizontal: 100, vertical: 100),
              child: Column(
                children: [
                  Icon(
                    Icons.watch_later,
                    color: s.buttonColor,
                    size: 150,
                  ),
                  SizedBox(
                    height: 50,
                  ),
                  TextFormField(
                    controller: _nameController,
                    decoration: InputDecoration(labelText: "Имя пользователя"),
                  ),
                  TextFormField(
                    controller: _passwordController,
                    decoration: InputDecoration(labelText: "Пароль"),
                  ),
                  SizedBox(
                    height: 50,
                  ),
                  button("Зарегистрироваться", "/home_page_schedule")
                ],
              )),
        ));
  }

  void submitForm() {
    print("Name ${_nameController.text}");
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question