【问题标题】:Getting error while accessing super class variable访问超类变量时出错
【发布时间】:2019-06-30 02:14:36
【问题描述】:

我有以下飞镖继承结构:

class MySuperList extends StatefulWidget {
  final category_name;
  MySuperList({this.category_name});
  @override
  _MySuperListState createState() => new _MySuperListState();
}

class _MySuperListState extends State<MySuperList> {

  Widget appBarTitle  = new Text(
      widget.category_name,   <== Only static members can be accessed in initializers
      style: new TextStyle(color: Colors.white);

正如你所看到的,当我尝试使用 widget.category_name 访问超类中变量的值时,我收到以下编译器错误:

在初始化器中只能访问静态成员

我还能如何访问这个值?

更新 遵循建议的答案后,文本现在卡在 appbar 中。它确实根据以下代码进行了更改:

 Widget buildAppBar(BuildContext context) {
    return new AppBar(centerTitle: false, title: getAppBarTitle(), actions: <Widget>[
      new IconButton(
        icon: icon,
        onPressed: () {
          this.appBarTitle = null;
          setState(() {
            if (this.icon.icon == Icons.search) {
              this.icon = new Icon(
                Icons.close,
                color: Colors.white,
              );
                this.appBarTitle = new TextField(
                controller: _controller,
                autofocus: true,
                style: new TextStyle(
                  color: Colors.white,
                ),
                decoration: new InputDecoration(
                    prefixIcon: new Icon(Icons.search, color: Colors.white),
                    hintText: "Search...",
                    hintStyle: new TextStyle(color: Colors.white)),
                 onChanged: searchOperation,
              );                 
            } else {
              _handleSearchEnd();
            }
          });
        },
      ),
    ]);
  }

 void _handleSearchEnd() {
    setState(() {
      this.icon = new Icon(
        Icons.search,
        color: Colors.white,
      );

      _isSearching = false;
       this.appBarTitle = new Text(
        widget.category_name,
        style: new TextStyle(color: Colors.white),
      );
      _controller.clear();
    });
  }

如您所见,当单击搜索图标时,我将 appBarTitle 设置为 TextField。但是,不会生成文本字段。应用栏仍然显示标题。我没有进行热重载或热重启。我实际上做了一个完全重启。

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    这是你需要做的一个巧妙的方法

    import 'package:flutter/material.dart';
    
    class MySuperList extends StatefulWidget{
      final category_name;
      MySuperList({this.category_name});
    
      @override
      State<StatefulWidget> createState() {
    
        return _MySuperListState();
      }
    }
    
    class _MySuperListState extends State<MySuperList>{
    
      bool _isSearching=false;
      TextEditingController _controller = TextEditingController();
    
      @override
      Widget build(BuildContext context) {
    
        return Scaffold(
          appBar: buildAppBar(),
        );
      }
    
      Widget buildAppBar(){
        return AppBar(
          centerTitle: false,
          title: getAppBarTitle(),
          actions: <Widget>[
            getAction()
          ],
        );
      }
    
      Widget getAction(){
        if(_isSearching){
          return IconButton(
            icon: Icon(
              Icons.close
            ),
            onPressed: (){
              setState(() {
                _controller.clear();
                _isSearching = false;
              });
            },
          );
        }else{
          return IconButton(
            icon: Icon(
                Icons.search
            ),
            onPressed: (){
              setState(() {
                _isSearching = true;
              });
            },
          );
        }
      }
    
      Widget getAppBarTitle(){
        if(_isSearching){
          return TextField(
            controller: _controller,
            autofocus: true,
            style: new TextStyle(
              color: Colors.white,
            ),
            decoration: new InputDecoration(
                prefixIcon: new Icon(Icons.search, color: Colors.white),
                hintText: "Search...",
                hintStyle: new TextStyle(color: Colors.white)),
            onChanged: searchOperation,
          );
    
    
        }else{
          return Text(
              widget.category_name,
              style: new TextStyle(color: Colors.white)
          );
        }
      }
    
      searchOperation(String value){
        //do what you need to onChanged
      }
    
    }
    

    【讨论】:

    • 我编辑了答案以匹配您的情况,我对其进行了测试,一切正常
    • 谢谢。重构当然有很大帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 2016-09-18
    相关资源
    最近更新 更多