@trickster2019

Почему функция не видет переменную контроллера текста?

Попытался создать функцию вывода введённого текста, но выдало "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}");
  }
}
  • Вопрос задан
  • 69 просмотров
Решения вопроса 1
RomReed
@RomReed
JavaScript, Flutter, ReactNative, Redux, Firebase
Так должно заработать. Почитайте про область видимости.
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}");
  }
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы