Skip to content

Instantly share code, notes, and snippets.

@nick45chen
Created March 12, 2020 04:47
Show Gist options
  • Save nick45chen/a743f544e4b25a197abce091a3566d4e to your computer and use it in GitHub Desktop.
Save nick45chen/a743f544e4b25a197abce091a3566d4e to your computer and use it in GitHub Desktop.
No MediaQuery widget found.
════════ Exception caught by gesture ═══════════════════════════════════════════════════════════════
The following assertion was thrown while handling a gesture:
No MediaQuery widget found.
MyApp widgets require a MediaQuery widget ancestor.
The specific widget that could not find a MediaQuery ancestor was: MyApp
state: _MyAppState#6b131
The ownership chain for the affected widget is: "MyApp ← [root]"
Typically, the MediaQuery widget is introduced by the MaterialApp or WidgetsApp widget at the top of your application widget tree.
When the exception was thrown, this was the stack:
#0 debugCheckHasMediaQuery.<anonymous closure> (package:flutter/src/widgets/debug.dart:219:7)
#1 debugCheckHasMediaQuery (package:flutter/src/widgets/debug.dart:231:4)
#2 showModalBottomSheet (package:flutter/src/material/bottom_sheet.dart:529:10)
#3 _MyAppState._pickImage (package:fluttercamera/main.dart:93:5)
#4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:705:14)
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#16761
debugOwner: GestureDetector
state: possible
won arena
finalPosition: Offset(200.4, 120.3)
finalLocalPosition: Offset(58.5, 18.3)
button: 1
sent tap down
════════════════════════════════════════════════════════════════════════════════════════════════════
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:multi_image_picker/multi_image_picker.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<Asset> images = List<Asset>();
String _error = 'No Error Dectected';
@override
void initState() {
super.initState();
}
Widget buildGridView() {
return GridView.count(
crossAxisCount: 3,
children: List.generate(images.length, (index) {
Asset asset = images[index];
return AssetThumb(
asset: asset,
width: 300,
height: 300,
);
}),
);
}
Future<void> loadAssets() async {
List<Asset> resultList = List<Asset>();
String error = 'No Error Dectected';
try {
resultList = await MultiImagePicker.pickImages(
maxImages: 300,
enableCamera: true,
selectedAssets: images,
cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
materialOptions: MaterialOptions(
actionBarColor: "#abcdef",
actionBarTitle: "Example App",
allViewTitle: "All Photos",
useDetailsView: false,
selectCircleStrokeColor: "#000000",
),
);
} on Exception catch (e) {
error = e.toString();
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
images = resultList;
_error = error;
});
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Plugin example app'),
),
body: Column(
children: <Widget>[
Center(child: Text('Error: $_error')),
RaisedButton(
child: Text("Pick images"),
onPressed: _pickImage,
),
Expanded(
child: buildGridView(),
)
],
),
),
);
}
_pickImage() {
showModalBottomSheet(
context: context,
builder: (context) =>
Container(
height: 160,
child: Column(
children: <Widget>[_item('拍照', true), _item('相簿', false)],
),
));
}
_item(String title, bool isTakePhoto) {
return GestureDetector(
child: ListTile(
leading: Icon(isTakePhoto ? Icons.camera_alt : Icons.photo_library),
onTap: () => loadAssets(),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment