未在Flutter中为类TagColumn定义Getter _text [英] Getter _text isn't defined for class TagColumn in Flutter

查看:60
本文介绍了未在Flutter中为类TagColumn定义Getter _text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Stack Overflow上查看了这个问题

解决方案

dart将以下划线开头的变量视为私有变量(因为dart中没有private关键字),因此为了解决您的问题,您需要删除文本变量前的_(下划线).

这是什么病

1-将_text变量移到由State类插入的 TagColumn 类中

  class TagColumn扩展了StatefulWidget {最终文本= TextEditingController();//删除了_,以便在Practice类中对其进行访问@override状态< StatefulWidget>createState()=>新的_TagColumn();} 

并更新TagColumn类以反映这些更改

 类别_TagColumn扩展了State< TagColumn> {//final _text = TextEditingController();< ----因为文本现在在TagColumn类而不是状态类中bool _validate = false;@override窗口小部件构建(BuildContext上下文){最后的tagField = TextField(控制器:widget.text,obscureText:否,样式:TextStyle(fontFamily:播放",颜色:Colors.white,fontSize:20),maxLines:null,keyboardType:TextInputType.text,装饰:InputDecoration(contentPadding:EdgeInsets.fromLTRB(20.0,15.0,20.0,15.0),hintText:标记",errorText:_validate吗?值不能为空":null,边界:OutlineInputBorder(borderRadius:BorderRadius.circular(32.0))),);返回容器(宽度:MediaQuery.of(context).size.width/2-40,边距:EdgeInsets.symmetric(水平:20,垂直:20),装饰:BoxDecoration(颜色:Colors.blue,borderRadius:BorderRadius.circular(32.0),),子代:主题(数据:ThemeData(hintColor:Colors.white,),子代:tagField,),);}} 

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.

解决方案

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,
      ),
    );
  }
}

这篇关于未在Flutter中为类TagColumn定义Getter _text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆