Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 使用onTap navigator颤振创建变量_Dart_Flutter - Fatal编程技术网

Dart 使用onTap navigator颤振创建变量

Dart 使用onTap navigator颤振创建变量,dart,flutter,Dart,Flutter,这将是很多解释,但我希望有人能够帮助 目前,我在appbar上有一个搜索按钮,按下该按钮时,会在appbar标题上显示一个文本字段 正常的appbar标题是一个图像,我添加了一些功能,当按下时,它会将您带到主屏幕。这是因为它变得很棘手,因为我需要使用这行代码来实现这一点 new InkWell ( child: Image.asset( 'images/logoGrey.png', fit: BoxFit.fill, ), onTap: () {

这将是很多解释,但我希望有人能够帮助

目前,我在appbar上有一个搜索按钮,按下该按钮时,会在appbar标题上显示一个文本字段

正常的appbar标题是一个图像,我添加了一些功能,当按下时,它会将您带到主屏幕。这是因为它变得很棘手,因为我需要使用这行代码来实现这一点

new InkWell (
    child: Image.asset(
      'images/logoGrey.png',
      fit: BoxFit.fill,
    ),
    onTap: () {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) =>
              LandingPage(),
        ),
      );
    },
  ); 
我把它设置成这样的变量

class _ControlsPageState extends State<ControlsPage> {
  Widget appBarTitle = new InkWell (
    child: Image.asset(
      'images/logoGrey.png',
      fit: BoxFit.fill,
    ),
    onTap: () {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) =>
              LandingPage(),
        ),
      );
    },
  );
底线是我需要我的appbar标题是对变量“appBarTitle”的回调,而该变量在“context”上出现错误,那么我是否有办法做到这一点

以下是appbar代码,以防它有所帮助

appBar: AppBar(
          iconTheme: new IconThemeData(color: Theme.CompanyColors.coolGrey),
          backgroundColor: Colors.white,
          centerTitle: true,
          title: appBarTitle ,
          actions: <Widget>[
            new IconButton(
              icon: actionIcon,
              onPressed: () {
                setState(() {
                  if (this.actionIcon.icon == Icons.search) {
                    this.actionIcon =
                        new Icon(Icons.close, color: Theme.CompanyColors.coolGrey);
                    this.appBarTitle = new TextField(
                      onSubmitted: (String str) {
                        setState(() {
                          result = str;
                        });
                        controller.text = "";
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) => ControlSearchPage(
                                search: result, title: "${widget.title}"),
                          ),
                        );
                      },
                      style: new TextStyle(
                        color: Colors.black,
                      ),
                      decoration: new InputDecoration(
                          prefixIcon:
                              new Icon(Icons.search, color: Theme.CompanyColors.coolGrey),
                          hintText: "Search...",
                          hintStyle: new TextStyle(color: Theme.CompanyColors.coolGrey)),
                    );
                  } else {
                    this.actionIcon =
                        new Icon(Icons.search, color: Theme.CompanyColors.coolGrey);
                    this.appBarTitle = new InkWell (
                      child: Image.asset(
                        'images/logoGrey.png',
                        fit: BoxFit.fill,
                      ),
                      onTap: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) =>
                                LandingPage(),
                          ),
                        );
                      },
                    );
                  }
                });
              },
            ),
          ],
        ),
appBar:appBar(
iconTheme:new IconThemeData(颜色:Theme.CompanyColor.coolGrey),
背景颜色:Colors.white,
标题:对,
标题:appBarTitle,
行动:[
新图标按钮(
图标:actionIcon,
已按下:(){
设置状态(){
if(this.actionIcon.icon==Icons.search){
这个.actionIcon=
新图标(Icons.close,颜色:Theme.CompanyColors.coolGrey);
this.appBarTitle=新文本字段(
提交:(字符串str){
设置状态(){
结果=str;
});
controller.text=“”;
导航器。推(
上下文
材料路线(
生成器:(上下文)=>ControlSearchPage(
搜索:结果,标题:“${widget.title}”),
),
);
},
样式:新文本样式(
颜色:颜色,黑色,
),
装饰:新的输入装饰(
前缀:
新图标(Icons.search,颜色:Theme.CompanyColors.coolGrey),
hintText:“搜索…”,
hintStyle:new TextStyle(颜色:Theme.CompanyColor.coolGrey)),
);
}否则{
这个.actionIcon=
新图标(Icons.search,颜色:Theme.CompanyColors.coolGrey);
this.appBarTitle=新墨水池(
子:Image.asset(
“images/logogray.png”,
fit:BoxFit.fill,
),
onTap:(){
导航器。推(
上下文
材料路线(
生成器:(上下文)=>
登录页(),
),
);
},
);
}
});
},
),
],
),

任何评论都将受到欢迎

您应该将appBarTitle更改为一种可以在状态更改时生成小部件的方法,而不是将其保存到变量中。这样,您就可以确保只有当
上下文可用时才会生成它

// Define a bool to hold the current search state
bool _isSearching = false;

...

// In your build method
appBar: AppBar(
  iconTheme: new IconThemeData(color: Theme.CompanyColors.coolGrey),
  backgroundColor: Colors.white,
  centerTitle: true,
  title: _buildAppBarTitle(),
  actions: <Widget>[
    new IconButton(
      icon: _isSearching
        ? new Icon(Icons.close, color: Theme.CompanyColors.coolGrey)
        : new Icon(Icons.search, color: Theme.CompanyColors.coolGrey),
      onPressed: () {
        setState(() => _isSearching = !_isSearching);
      },
    ),
  ],
),

...

// Define a separate method to build the appBarTitle
Widget _buildAppBarTitle() {
  if (_isSearching) {
    return new TextField(
      onSubmitted: (String str) {
        setState(() {
          result = str;
        });
        controller.text = "";
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => ControlSearchPage(
                search: result, title: "${widget.title}"),
          ),
        );
      },
      style: new TextStyle(
        color: Colors.black,
      ),
      decoration: new InputDecoration(
          prefixIcon:
              new Icon(Icons.search, color: Theme.CompanyColors.coolGrey),
          hintText: "Search...",
          hintStyle: new TextStyle(color: Theme.CompanyColors.coolGrey)),
    );
  } else {
    return new InkWell (
      child: Image.asset(
        'images/logoGrey.png',
        fit: BoxFit.fill,
      ),
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) =>
                LandingPage(),
          ),
        );
      },
    );
  }
//定义一个bool来保存当前搜索状态
bool\u isSearching=false;
...
//在您的构建方法中
appBar:appBar(
iconTheme:new IconThemeData(颜色:Theme.CompanyColor.coolGrey),
背景颜色:Colors.white,
标题:对,
标题:_buildAppBarTitle(),
行动:[
新图标按钮(
图标:\u正在搜索
?新图标(图标。关闭,颜色:主题。公司颜色。冷灰色)
:新图标(Icons.search,颜色:Theme.CompanyColors.coolGrey),
已按下:(){
设置状态(()=>\u正在搜索=!\u正在搜索);
},
),
],
),
...
//定义一个单独的方法来构建appBarTitle
Widget_buildAppBarTitle(){
如果(正在搜索){
返回新的文本字段(
提交:(字符串str){
设置状态(){
结果=str;
});
controller.text=“”;
导航器。推(
上下文
材料路线(
生成器:(上下文)=>ControlSearchPage(
搜索:结果,标题:“${widget.title}”),
),
);
},
样式:新文本样式(
颜色:颜色,黑色,
),
装饰:新的输入装饰(
前缀:
新图标(Icons.search,颜色:Theme.CompanyColors.coolGrey),
hintText:“搜索…”,
hintStyle:new TextStyle(颜色:Theme.CompanyColor.coolGrey)),
);
}否则{
返回新墨水池(
子:Image.asset(
“images/logogray.png”,
fit:BoxFit.fill,
),
onTap:(){
导航器。推(
上下文
材料路线(
生成器:(上下文)=>
登录页(),
),
);
},
);
}

您应该将appBarTitle更改为一种可以在状态更改时生成小部件的方法,而不是将其保存到变量中。这样,您可以确保它仅在
上下文可用时生成

// Define a bool to hold the current search state
bool _isSearching = false;

...

// In your build method
appBar: AppBar(
  iconTheme: new IconThemeData(color: Theme.CompanyColors.coolGrey),
  backgroundColor: Colors.white,
  centerTitle: true,
  title: _buildAppBarTitle(),
  actions: <Widget>[
    new IconButton(
      icon: _isSearching
        ? new Icon(Icons.close, color: Theme.CompanyColors.coolGrey)
        : new Icon(Icons.search, color: Theme.CompanyColors.coolGrey),
      onPressed: () {
        setState(() => _isSearching = !_isSearching);
      },
    ),
  ],
),

...

// Define a separate method to build the appBarTitle
Widget _buildAppBarTitle() {
  if (_isSearching) {
    return new TextField(
      onSubmitted: (String str) {
        setState(() {
          result = str;
        });
        controller.text = "";
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => ControlSearchPage(
                search: result, title: "${widget.title}"),
          ),
        );
      },
      style: new TextStyle(
        color: Colors.black,
      ),
      decoration: new InputDecoration(
          prefixIcon:
              new Icon(Icons.search, color: Theme.CompanyColors.coolGrey),
          hintText: "Search...",
          hintStyle: new TextStyle(color: Theme.CompanyColors.coolGrey)),
    );
  } else {
    return new InkWell (
      child: Image.asset(
        'images/logoGrey.png',
        fit: BoxFit.fill,
      ),
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) =>
                LandingPage(),
          ),
        );
      },
    );
  }
//定义一个bool来保存当前搜索状态
bool\u isSearching=false;
...
//在您的构建方法中
appBar:appBar(
iconTheme:new IconThemeData(颜色:Theme.CompanyColor.coolGrey),
背景颜色:Colors.white,
标题:对,
标题:_buildAppBarTitle(),
行动:[
新图标按钮(
图标:\u正在搜索
?新图标(图标。关闭,颜色:主题。公司颜色。冷灰色)
:新图标(Icons.search,颜色:Theme.CompanyColors.coolGrey),
已按下:(){
设置状态(()=>\u正在搜索=!\u正在搜索);
},
),
],
),
...
//定义一个单独的方法来构建appBarTitle
Widget\u buildAppBa