老师你好,我在PhotoApp中build外层加了MaterialApp会报错
来源:6-16 拍照APP开发-图片获取与图片展示【实战尝鲜】
qq_雪儿_19
2019-04-22
老师你好,我在PhotoApp中build如下代码:
如果 没有 children: _getImages(),就可以运行,添加后会报错
麻烦可以帮我看下为什么会报那个错误么?
代码如下:
return MaterialApp(
title: ‘这是一个相机APP的demo’,
theme: ThemeData(backgroundColor: Colors.white),
home: Scaffold(
appBar: AppBar(
title: Text(‘相机APP的demo’),
leading: GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Icon(Icons.arrow_back),
),
),
body: Center(
child: Wrap(
spacing: 5,
runSpacing: 5,
children: _getImages(),
)),
floatingActionButton: FloatingActionButton(
onPressed: _pickImage(),
tooltip: ‘选择图片’,
child: Icon(Icons.add_a_photo),
),
),
);
错误如下:
Syncing files to device AOSP on HammerHead…
I/flutter (16585): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (16585): The following assertion was thrown building PhotoApp(dirty, dependencies:
I/flutter (16585): [_LocalizationsScope-[GlobalKey#69ffc], _InheritedTheme], state: _PhotoAppState#21835):
I/flutter (16585): setState() or markNeedsBuild() called during build.
I/flutter (16585): This Overlay widget cannot be marked as needing to build because the framework is already in the
I/flutter (16585): process of building widgets. A widget can be marked as needing to be built during the build phase
I/flutter (16585): only if one of its ancestors is currently building. This exception is allowed because the framework
I/flutter (16585): builds parent widgets before children, which means a dirty descendant will always be built.
I/flutter (16585): Otherwise, the framework might not visit this widget during this build phase.
I/flutter (16585): The widget on which setState() or markNeedsBuild() was called was:
I/flutter (16585): Overlay-[LabeledGlobalKey#bf2f4](state: OverlayState#992e6(tickers: tracking 1
I/flutter (16585): ticker, entries: [OverlayEntry#f93c3(opaque: false; maintainState: false),
I/flutter (16585): OverlayEntry#c4a28(opaque: false; maintainState: true), OverlayEntry#2fddd(opaque: false;
I/flutter (16585): maintainState: false), OverlayEntry#ac5f2(opaque: false; maintainState: true),
I/flutter (16585): OverlayEntry#c9481(opaque: false; maintainState: false), OverlayEntry#d8035(opaque: false;
I/flutter (16585): maintainState: true)]))
I/flutter (16585): The widget which was currently being built when the offending call was made was:
I/flutter (16585): PhotoApp(dirty, dependencies: [_LocalizationsScope-[GlobalKey#69ffc], _InheritedTheme], state:
I/flutter (16585): _PhotoAppState#21835)
I/flutter (16585): When the exception was thrown, this was the stack:
I/flutter (16585): #0 Element.markNeedsBuild. (package:flutter/src/widgets/framework.dart:3497:11)
I/flutter (16585): #1 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:3523:6)
I/flutter (16585): #2 State.setState (package:flutter/src/widgets/framework.dart:1138:14)
I/flutter (16585): #3 OverlayState.insertAll (package:flutter/src/widgets/overlay.dart:301:5)
I/flutter (16585): #4 OverlayRoute.install (package:flutter/src/widgets/routes.dart:43:24)
I/flutter (16585): #5 TransitionRoute.install (package:flutter/src/widgets/routes.dart:184:11)
I/flutter (16585): #6 ModalRoute.install (package:flutter/src/widgets/routes.dart:899:11)
I/flutter (16585): #7 NavigatorState.push (package:flutter/src/widgets/navigator.dart:1672:11)
I/flutter (16585): #8 Navigator.push (package:flutter/src/widgets/navigator.dart:1011:34)
1回答
-
CrazyCodeBoy
2019-04-22
参考下:
import 'dart:io'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; ///【实战尝鲜】拍照APP开发 class PhotoApp extends StatefulWidget { @override _PhotoAppState createState() => _PhotoAppState(); } class _PhotoAppState extends State<PhotoApp> { List<File> _images = []; Future getImage(bool isTakePhoto) async { Navigator.pop(context); var image = await ImagePicker.pickImage( source: isTakePhoto ? ImageSource.camera : ImageSource.gallery); setState(() { _images.add(image); }); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('【实战尝鲜】拍照APP开发'), leading: GestureDetector( onTap: () { Navigator.pop(context); }, child: Icon(Icons.arrow_back), ), ), body: Center( child: Wrap( spacing: 5, runSpacing: 5, children: _genImages(), ), ), floatingActionButton: FloatingActionButton( onPressed: _pickImage, tooltip: '选择图片', child: Icon(Icons.add_a_photo), ), ), ); } _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), title: Text(title), onTap: () => getImage(isTakePhoto), ), ); } _genImages() { return _images.map((file) { return Stack( children: <Widget>[ ClipRRect( //圆角效果 borderRadius: BorderRadius.circular(5), child: Image.file(file, width: 120, height: 90, fit: BoxFit.fill), ), Positioned( right: 5, top: 5, child: GestureDetector( onTap: () { setState(() { _images.remove(file); }); }, child: ClipOval( //圆角删除按钮 child: Container( padding: EdgeInsets.all(3), decoration: BoxDecoration(color: Colors.black54), child: Icon( Icons.close, size: 18, color: Colors.white, ), ), ), )) ], ); }).toList(); } }
022019-04-23
相似问题