【问题标题】:"Undefined name 'context'. Try correcting the name to one that is defined, or defining the name." Flutter“未定义的名称‘上下文’。尝试将名称更正为已定义的名称,或定义名称。”扑
【发布时间】:2020-01-28 11:35:08
【问题描述】:

这是我的 widgets.dart 文件的 sn-p,我在其中定义了一个名为 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(),
          ],
        ),
      ),
    );   
  }
}

【问题讨论】:

    标签: flutter dart navigation


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-03
      • 2023-02-07
      • 2020-11-01
      • 2022-08-22
      • 2021-03-24
      • 2022-06-10
      • 1970-01-01
      • 2021-11-28
      相关资源
      最近更新 更多