Flutter异常被手势捕获。在showModalBottomSheet中找不到MediaQuery小部件

时间:2020-01-22 16:26:09

标签: android flutter dart

大家好,我刚刚开始使用flutter,我无法弄清楚为什么我不能在floatActionButton中使用showModalBottomSheet。它只会不断向我显示此错误。谁能帮我吗?

I/flutter (16368): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter (16368): The following assertion was thrown while handling a gesture:
I/flutter (16368): No MediaQuery widget found.
I/flutter (16368): MyApp widgets require a MediaQuery widget ancestor.
I/flutter (16368): The specific widget that could not find a MediaQuery ancestor was:
I/flutter (16368):   MyApp
I/flutter (16368): The ownership chain for the affected widget is: "MyApp ← [root]"
I/flutter (16368): Typically, the MediaQuery widget is introduced by the MaterialApp or WidgetsApp widget at the top of
I/flutter (16368): your application widget tree.
I/flutter (16368): 
I/flutter (16368): When the exception was thrown, this was the stack:
I/flutter (16368): #0      debugCheckHasMediaQuery.<anonymous closure> (package:flutter/src/widgets/debug.dart:211:7)
I/flutter (16368): #1      debugCheckHasMediaQuery (package:flutter/src/widgets/debug.dart:223:4)
I/flutter (16368): #2      showModalBottomSheet (package:flutter/src/material/bottom_sheet.dart:469:10)
I/flutter (16368): #3      _MyAppState.build.<anonymous closure> (package:flutter_happy_habits/main.dart:32:29)
I/flutter (16368): #4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
import 'package:flutter/material.dart';
import './models/home.dart';
import 'models/progress.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _selectedPage = 0;
  final _pageOptions = [
    Home(),
    Progress(),
    Progress(),
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      debugShowCheckedModeBanner: true,
      home: new Scaffold(
        appBar: AppBar(title: Text('Flutter Demo')),
        body: _pageOptions[_selectedPage],
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          onPressed: () { showModalBottomSheet(
            context: context,
            builder: (context) {
              return Text('Modal bottom sheet', style: TextStyle(fontSize: 30));
            });
          }
        ),
        bottomNavigationBar: BottomAppBar(
          shape: CircularNotchedRectangle(),
          notchMargin: 4.0,
          child: new Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              IconButton(
                icon: Icon(Icons.home),
                onPressed: () {
                  print("Home");
                  setState(() {
                    _selectedPage = 0;
                  });
                },
              ),
              IconButton(
                icon: Icon(Icons.insert_chart),
                onPressed: () {
                  print("Progress");
                  setState(() {
                    _selectedPage = 1;
                  });
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

2 个答案:

答案 0 :(得分:20)

这是因为Sub test2() Dim arr, arr_1, arr_2, arr_3, arr_4, arr_5, arr_6, arr_7, totalrng As Variant Dim rowcount, x, rowx As Long With Sheets("sheet1") rowcount = .Cells(Rows.Count, 1).End(xlUp).Row Set arr_1 = .Range("D1:D" & rowcount) Set arr_2 = .Range("F1:F" & rowcount) Set arr_3 = .Range("H1:H" & rowcount) Set arr_4 = .Range("J1:J" & rowcount) Set arr_5 = .Range("L1:L" & rowcount) Set arr_6 = .Range("N1:N" & rowcount) Set arr_7 = .Range("P1:P" & rowcount) Dim dict As Variant Set dict = CreateObject("scripting.dictionary") dict.Add Key:="arr_1", Item:=Nothing dict.Add Key:="arr_2", Item:=Nothing dict.Add Key:="arr_3", Item:=Nothing dict.Add Key:="arr_4", Item:=Nothing dict.Add Key:="arr_5", Item:=Nothing dict.Add Key:="arr_6", Item:=Nothing dict.Add Key:="arr_7", Item:=Nothing For Each Key In dict Debug.Print findrng x = 2 For Each num In Array(Key) arr = Array(Key) .Cells(x, 22).Value = arr(1, 1) .Cells(x, 23).Value = arr(1, 2) x = x + 1 Next num Next Key End With End Sub 试图从给定的showModalBottomSheet中访问类型MaterialApp的祖先。

使用context小部件与Builder祖先获得新的context或将MaterialAppMaterialAapp小部件分离为单独的小部件。

使用Scaffold

Builder

答案 1 :(得分:2)

我有一个解决方案。我不知道这是不是最好的,但是可以用。 showModalBottomSheet的上下文不应与materialapp的上下文相同,因此必须在此示例中看到的无状态小部件中将其分隔开。

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter App',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {

    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(title: const Text('Modal bottom sheet')),
        body: new Center(
          child: new RaisedButton(
            child: const Text('SHOW BOTTOM SHEET'),
            onPressed: () {
              showModalBottomSheet<void>(context: context, builder: (BuildContext context) {
                return new Container(
                  child: new Padding(
                    padding: const EdgeInsets.all(32.0),
                    child: new Text('This is the modal bottom sheet. Click anywhere to dismiss.',
                      textAlign: TextAlign.center,
                      style: new TextStyle(
                        color: Theme.of(context).accentColor,
                        fontSize: 24.0
                      )
                    )
                  )
                );
              });
            }
          )
        )
      )
    );
  }
}