Getter _text isn't defined for class TagColumn in Flutter

微笑、不失礼 提交于 2021-01-29 02:24:39

问题


I have looked at this question on Stack Overflow Flutter getter isn't specified for the class, when it is specified. And I still cannot understand why my class Practice does not have access to the variable _text which is accessed from an element in the List with type TagColumn.

class Practice extends StatefulWidget {
  @override
  _PracticeState createState() => _PracticeState();
}

class _PracticeState extends State<Practice>{
  int count  = 0;

  @override
  Widget build(BuildContext context){
    List<TagColumn> ok = List.generate(count, (int i) => new TagColumn());
    return Scaffold(
      backgroundColor: Colors.black,
      body: new LayoutBuilder(builder: (context, constraint){
      return new Stack(
        children: <Widget>[
          SingleChildScrollView(
            child: SafeArea(
              child: new Wrap(
                direction: Axis.horizontal,
                children: ok,
              )
            ),
          ),
          new Positioned(
            child: new Align(
              alignment: FractionalOffset.bottomRight,
              child: Container(
                margin: EdgeInsets.only(bottom: 50.0, right: 40.0),
                child: RawMaterialButton(
                  onPressed: (){
                    setState(() {
                      if(count != 0 && ok[count]._text.text.isEmpty){

                      }
                      else{
                          count +=1;
                      }
                    });
                  },
                  shape: CircleBorder(),
                  child: Icon(
                    Icons.add_circle,
                    size: 100.0,
                    color: Color(0xffd3d3d3),
                  ),
                )
              )
            )
          )

        ],
      );
      }),
    );
  }
}

class TagColumn extends StatefulWidget{
  @override
  State<StatefulWidget> createState() => new _TagColumn();
}

class _TagColumn extends State<TagColumn>{
  final _text = TextEditingController();
  bool _validate = false;

  @override

  Widget build(BuildContext context){
    final tagField = TextField(
      controller: _text,
      obscureText: false,
      style: TextStyle(fontFamily: 'Play', color: Colors.white, fontSize: 20),
      maxLines: null,
      keyboardType: TextInputType.text,
      decoration: InputDecoration(
        contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
        hintText: "Tag",
          errorText: _validate ? 'Value Can\'t be Empty': null,
          border:
          OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
      );
    return Container(
      width: MediaQuery.of(context).size.width/2 - 40,
      margin: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
      decoration: BoxDecoration(
        color: Colors.blue,
        borderRadius: BorderRadius.circular(32.0),
      ),
      child: Theme(
        data: ThemeData(
          hintColor: Colors.white,
        ),
        child: tagField,
      ),
    );
  }
}

What I am trying to do is not allow the user to create a new tag when pressing, "Plus," in the bottom right corner(see the image below) if the user doesn't enter text in the current one. In other words, if it's not empty. Thus, I am using the variable final _text = TextEditingController(), to check if the current tag is empty when pressing the plus button. If not, a new tag is created.


回答1:


dart treates variables that start with an underscore as a private variable (since there is no private keyword in dart) so in order to solve your probelm, you need to remove the _(underscore) before the text variable.

what ill do is this

1- move the _text variable to the TagColumn class insted of the State class

class TagColumn extends StatefulWidget{
 final text = TextEditingController(); // removed the _ so that to access it inside the Practise class
  @override
  State<StatefulWidget> createState() => new _TagColumn();
}

and update the TagColumn class to reflect those changes


class _TagColumn extends State<TagColumn>{
   // final _text = TextEditingController(); <---- since the text is now in the TagColumn class not the state class
  bool _validate = false;

  @override

  Widget build(BuildContext context){
    final tagField = TextField(
      controller: widget.text,
      obscureText: false,
      style: TextStyle(fontFamily: 'Play', color: Colors.white, fontSize: 20),
      maxLines: null,
      keyboardType: TextInputType.text,
      decoration: InputDecoration(
        contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
        hintText: "Tag",
          errorText: _validate ? 'Value Can\'t be Empty': null,
          border:
          OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
      );
    return Container(
      width: MediaQuery.of(context).size.width/2 - 40,
      margin: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
      decoration: BoxDecoration(
        color: Colors.blue,
        borderRadius: BorderRadius.circular(32.0),
      ),
      child: Theme(
        data: ThemeData(
          hintColor: Colors.white,
        ),
        child: tagField,
      ),
    );
  }
}


来源:https://stackoverflow.com/questions/61534880/getter-text-isnt-defined-for-class-tagcolumn-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!