【问题标题】:Not getting context right... why?没有得到正确的上下文......为什么?
【发布时间】:2018-03-15 19:50:03
【问题描述】:

我开始关注 Firebase CodeLab,之后对其进行了修改和添加。我正在尝试使用 Snackbar,但没有成功。

class ChatScreenState extends State<ChatScreen> {      
   @override
   Widget build(BuildContext context) {
      return new Scaffold(                                                       
         appBar: new AppBar(                                                     
            title: new Text("Teapot"),
            elevation:
               Theme.of(context).platform == TargetPlatform.iOS ? 0.0 : 4.0),
         body: new Container(                                                    
            child: new Column(                                                   
               children: <Widget>[                                               
                  new Flexible(                                                                          
                     // stuff
                  ),
                  new Divider(height: 1.0),                                      
                  new Container(                                                 
                     decoration: new BoxDecoration(color: Theme
                        .of(context).cardColor),
//call --->              child: _buildTextComposer(),                         
                  ),
               ],
            ),              
         ),
      );
   }

   Widget _buildTextComposer() {
      return new IconTheme(                                                      
         data: new IconThemeData(color: Theme.of(context).accentColor),
         child: new Container(                                                   
            margin: const EdgeInsets.symmetric(horizontal: 8.0),
            child: new Row(                                                      
               children: <Widget>[                                               
                  new Container(
                     margin: new EdgeInsets.symmetric(horizontal: 4.0),
                     child: new IconButton(
                        icon: new Icon(Icons.photo_camera),
                        onPressed: () async {
                           await _ensureLoggedIn();
                           File imageFile = await ImagePicker.pickImage();
                           int random = new Random().nextInt(100000);
                           StorageReference ref =
                           FirebaseStorage.instance.ref().child("image_$random.jpg");
                           StorageUploadTask uploadTask = ref.put(imageFile);
                           Uri downloadUrl = (await uploadTask.future).downloadUrl;
                           _sendMessage(imageUrl: downloadUrl.toString());
//bonk!  --->              Scaffold.of(context).showSnackBar(new SnackBar(
                              content: new Text("Image Sent"),
                           ));
                        }
                     ),
                  ),
                  // more stuff

当它尝试显示距底部约 6 行的SnackBar 时(请参阅bonk! ---&gt;),我收到一条错误消息,指出上下文中没有脚手架。我对此感到困惑,因为 _buildTextComposer 是从 Scaffold 内部(参见第 4 行)调用的(参见 call --&gt;)。

我做错了什么?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    key 添加到您的Scaffold

    final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
    
    return new Scaffold(
          key:scaffoldKey,
    .....)
    

    然后

    onPressed: (){
              scaffoldKey.currentState.showSnackBar(snackbar);}
    

    我发现这比跟踪上下文更好、更清晰。

    【讨论】:

    • 我同意这是更简洁的方式...我将其作为我的默认模式。
    猜你喜欢
    • 2013-08-19
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多