当使用BottomSheet时,没有发现脚手架部件。

问题描述 投票:0回答:1

我只是在学习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

但正如你从我的代码中看到的那样,我确实有一个脚手架,我玩了一圈,把它添加到任何可以添加的地方,但我没有工作。

什么原因会是我做了什么或没有注意到那里?

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(),
      ),
    );
  }
}
flutter dart flutter-layout
1个回答
1
投票

此错误是由于您的以下范围引起的 _showSettingsPanel 办法

有2件事情你可以做

1.使 _showSettingsPanel 办法 Home 类,并允许它将上下文作为参数。因此,将你的 设置 FlatButton 在...中 Builder 并通过 context_showSettingsPanel 方法。

像这样

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

2.包裹 Home 小部件 Scaffold 无论你在哪里使用它,而不是只使用 Home

像这样

Scaffold(body: Home())
© www.soinside.com 2019 - 2024. All rights reserved.