“未定义的名称'上下文'。请尝试将名称更正为已定义的名称,或定义名称。”扑

时间:2019-09-28 12:47:13

标签: flutter dart navigation

这是我的widgets.dart文件的一个片段,其中定义了一个名为see_all_cards的小部件,其唯一目的是显示我最初显示的所有卡的扩展列表。它应该重定向到Trending.dart 。这是我的主要目标。

Widget see_all_cards(){
  return  Container(
        child: FlatButton(
                  child: Text(
                    "See all (43)",               
                    style: TextStyle(
                      color: Theme.of(context).accentColor, // error
                    ),
                    ),

                  onPressed: (){
                    Navigator.push(
                      context,  // error
                      MaterialPageRoute(
                        builder: (BuildContext context){
                          return trending();     
                        },
                      ),
                    );
                  },       
        )
  );
}

以下部分是我的主页。我已经从void main调用了SlowlyApp。

class SlowlyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'SlowlyApp',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Search',
            style: TextStyle(
              color: Color.fromRGBO(0,0,0,1),
            ),
          ),
          backgroundColor: Color.fromRGBO(225,225,0,1),         
          actions: <Widget>[
            IconButton(
              icon: 
                Icon(Icons.search), 
                onPressed: (){
                  showSearch(context: context, delegate: data_search());
                }
            ),
          ],
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[       
            smallgap(), 
            current_cards_heading(),
            current_cards(),
            see_all_cards(),
            smallgap(),
          ],
        ),
      ),
    );   
  }
}

1 个答案:

答案 0 :(得分:0)

see_all_cards应该将上下文作为参数。您的主窗口小部件的构建方法中只有上下文

Widget see_all_cards(BuildContext context){
  return  Container(
        child: FlatButton(
                  child: Text(
                    "See all (43)",               
                    style: TextStyle(
                      color: Theme.of(context).accentColor, // error
                    ),
                    ),

                  onPressed: (){
                    Navigator.push(
                      context,  // error
                      MaterialPageRoute(
                        builder: (BuildContext context){
                          return trending();     
                        },
                      ),
                    );
                  },       
        )
  );
}

然后您可以调用传递上下文。

class SlowlyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'SlowlyApp',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Search',
            style: TextStyle(
              color: Color.fromRGBO(0,0,0,1),
            ),
          ),
          backgroundColor: Color.fromRGBO(225,225,0,1),         
          actions: <Widget>[
            IconButton(
              icon: 
                Icon(Icons.search), 
                onPressed: (){
                  showSearch(context: context, delegate: data_search());
                }
            ),
          ],
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[       
            smallgap(), 
            current_cards_heading(),
            current_cards(),
            see_all_cards(context),
            smallgap(),
          ],
        ),
      ),
    );   
  }
}