【问题标题】:Getter _text isn't defined for class TagColumn in Flutter没有为 Flutter 中的类 TagColumn 定义 Getter _text
【发布时间】:2020-05-01 00:08:40
【问题描述】:

我在 Stack Overflow Flutter getter isn't specified for the class, when it is specified 上查看过这个问题。而且我仍然无法理解为什么我的班级 Practice 无法访问变量 _text,该变量是从 List 中类型为 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,
      ),
    );
  }
}

我想要做的是不允许用户在按下右下角的“加号”时创建新标签(见下图),如果用户不这样做在当前文本中输入文本。换句话说,如果它不为空。因此,我使用变量 final _text = TextEditingController() 来检查按下加号按钮时当前标签是否为空。如果不是,则创建一个新标签。

【问题讨论】:

    标签: flutter dart mobile-development


    【解决方案1】:

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

    这是怎么回事

    1- 将 _text 变量移动到 State 类的 TagColumn 类中

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

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

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

    【讨论】:

      猜你喜欢
      • 2020-09-02
      • 1970-01-01
      • 2021-10-19
      • 2021-05-21
      • 2019-12-01
      • 2021-08-27
      • 1970-01-01
      • 2021-05-18
      • 2021-06-06
      相关资源
      最近更新 更多