使用BottomSheet时找不到脚手架小部件

时间:2020-05-02 10:32:30

标签: flutter dart flutter-layout

我只是在学习Flutter,并被卡在这个错误上:

No Scaffold widget found.

Home widgets require a Scaffold widget ancestor.
The specific widget that could not find a Scaffold ancestor was: Home
The ancestors of this widget were

但是正如您从我的代码中看到的那样,我确实有一个Scaffold,并且在可能的地方四处添加它,但是我没有工作。

我做了什么或没注意到那里的原因可能是什么?

import 'package:firebase_redux_app/services/firebase.auth.dart';
import 'package:flutter/material.dart';
// import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_redux_app/services/firestore.dart';
import 'package:provider/provider.dart';
import 'package:firebase_redux_app/screens/home/brewList.dart';
import 'package:firebase_redux_app/models/brew.dart';

class Home extends StatelessWidget {
  final AuthService _auth = AuthService();

  @override
  Widget build(BuildContext context) {
    void _showSettingsPanel() {
      showBottomSheet(
          context: context,
          builder: (context) {
            return Container(
              padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 60.0),
              child: Text('bottom sheet'),
            );
          });
    }

    return StreamProvider<List<Brew>>.value(
      value: DBFirestore().brews,
      child: Scaffold(
        backgroundColor: Colors.brown[50],
        appBar: AppBar(
          title: Text('Brew Crew'),
          backgroundColor: Colors.brown[400],
          elevation: 0.0,
          actions: <Widget>[
            FlatButton.icon(
                onPressed: () async {
                  await _auth.signOut();
                },
                icon: Icon(Icons.person),
                label: Text('Log Out')),
            FlatButton.icon(
              icon: Icon(Icons.settings),
              label: Text('settings'),
              onPressed: () => _showSettingsPanel(),
            )
          ],
        ),
        body: BrewList(),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:2)

此错误是由于您的date('j', $job_expiration)方法的范围

引起的

您可以做两件事

1。 使_showSettingsPanel_showSettingsPanel类中的方法,并允许其将上下文作为参数。因此,将您的设置 Home包装在FlatButton中,并将Builder传递给context方法。

_showSettingsPanel

2。 class Home extends StatelessWidget { void _showSettingsPanel(context) { showBottomSheet( context: context, builder: (context) { return Container( padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 60.0), child: Text('bottom sheet'), ); }); } @override Widget build(BuildContext context) { return StreamProvider<List<Brew>>( value: DBFireStore().brews child: Scaffold( backgroundColor: Colors.brown[50], appBar: AppBar( title: Text('Brew Crew'), backgroundColor: Colors.brown[400], elevation: 0.0, actions: <Widget>[ FlatButton.icon( onPressed: () async { }, icon: Icon(Icons.person), label: Text('Log Out')), Builder( builder: (context) { return FlatButton.icon( icon: Icon(Icons.settings), label: Text('settings'), onPressed: () => _showSettingsPanel(context), ); } ) ], ), body: BrewList(), ), ); } } 小部件包裹在Home中的任何位置,而不是仅使用Scaffold

Home