我的代码:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:food_delivery/pages/favoritespage.dart';
import 'package:food_delivery/pages/profilePage.dart';
import 'package:food_delivery/pages/shoppingScreen.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Food Delivery App',
theme: ThemeData(
primarySwatch: Colors.blue,
secondaryHeaderColor: Colors.blue,
accentColor: Colors.blue,
backgroundColor: Colors.black,
primaryColorDark: Colors.blueAccent,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage()));
}
class _MyHomePageState extends State<MyHomePage>{
static Widget _appCache;
int currentIndex = 0;
List<Widget> pages;
Widget currentWidget;
MyHomePage homePage;
ShoppingPage shoppingPage;
FavoritesPage favoritesPage;
ProfilePage profilePage;
bool _lights = false;
String themeText = 'Light';
BottomAppBar _bar;
FloatingActionButton _fab;
Widget _container;
int _currentPos = 0;
Widget getContainer(){
return Container(
height: 200.0,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.only(left: 16,top: 16),
child: Text('Theme',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold),),
),
SwitchListTile(
activeColor: Colors.orange,
inactiveThumbColor: Colors.black,
title: Text('$themeText',style: TextStyle(color: Colors.white,fontWeight: FontWeight.w400)),
value: _lights,
onChanged: (bool value) { setState(() {
_lights = value;
themeText = value? 'Light':'Dark';
}); },
secondary: Icon(Icons.lightbulb_outline,color: Colors.white,),
),
],
),
);
}
FloatingActionButton getFab(){
return FloatingActionButton(
onPressed: () {
},
child: Icon(
Icons.fastfood,
color: Colors.black45,
),
backgroundColor: Colors.orangeAccent,
heroTag: null,
);
}
BottomAppBar getNavBar() {
return BottomAppBar(
color: Colors.orangeAccent,
shape: CircularNotchedRectangle(),
notchMargin: 4,
clipBehavior: Clip.antiAlias,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('1'),
Text('2'),
Text('3'),
Text('4'),
],
)
);
}
@override
void initState() {
super.initState();
_bar = getNavBar();
_fab = getFab();
_container = getContainer();
homePage = MyHomePage();
shoppingPage = ShoppingPage();
favoritesPage = FavoritesPage();
profilePage = ProfilePage();
pages = [homePage,shoppingPage,favoritesPage,profilePage];
currentWidget = homePage;
print('init');
}
Widget _buildApp() {
return Scaffold(
backgroundColor: Colors.transparent,
extendBody: true,
primary: true,
resizeToAvoidBottomInset: false,
resizeToAvoidBottomPadding: true,
bottomNavigationBar: _bar,
floatingActionButton:_fab,
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
body: currentWidget
);
}
@override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.deepOrange,
));
if(_appCache==null)
_appCache = _buildApp();
return _appCache;
}
}
我正在使用BackdropScafflold,但由于没有放置空间 bottomNavigation我已经在其中使用了脚手架来放置BottomAppbar,布局可以正确渲染,但是过了一会儿错误开始出现,我没有想到的明显原因。
我的日志
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
I/flutter (29504): init
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following StackOverflowError was thrown building RawGestureDetector(state:
RawGestureDetectorState#e7638(gestures: [tap], behavior: opaque)):
Stack Overflow
The relevant error-causing widget was:
IconButton
file:///D:/Flutter/flutter/.pub-cache/hosted/pub.dartlang.org/backdrop-0.4.3/lib/button.dart:42:12
When the exception was thrown, this was the stack:
#0 _WordWrapParseMode.values (package:flutter/src/foundation/diagnostics.dart:778:6)
#1 _SyncIterator.moveNext (dart:core-patch/core_patch.dart:165:25)
#2 Iterable.length (dart:core/iterable.dart:429:15)
#3 _PrefixedStringBuilder._finalizeLine (package:flutter/src/foundation/diagnostics.dart:858:30)
#4 _PrefixedStringBuilder.write (package:flutter/src/foundation/diagnostics.dart:975:9)
#5 TextTreeRenderer._debugRender (package:flutter/src/foundation/diagnostics.dart:1279:15)
#6 TextTreeRenderer.render (package:flutter/src/foundation/diagnostics.dart:1128:14)
#7 TextTreeRenderer._debugRender (package:flutter/src/foundation/diagnostics.dart:1319:39)
#8 TextTreeRenderer.render (package:flutter/src/foundation/diagnostics.dart:1128:14)
#9 WidgetInspectorService._reportError (package:flutter/src/widgets/widget_inspector.dart:939:9)
#10 FlutterError.reportError (package:flutter/src/foundation/assertions.dart:1087:14)
#11 _debugReportException (package:flutter/src/widgets/framework.dart:6034:16)
#12 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4575:9)
#13 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#14 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4525:5)
... Normal element mounting (4 frames)
#18 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
#19 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
#20 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
#21 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
#22 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#23 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4525:5)
#24 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4520:5)
#25 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
#26 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
#27 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
#28 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#29 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4525:5)
... Normal element mounting (4 frames)
#33 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
#34 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
#35 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
#36 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
#37 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#38 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4525:5)
#39 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4520:5)
... Normal element mounting (4 frames)
...
...
#5348 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#5349 RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#5350 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#5351 RenderOffstage.performLayout (package:flutter/src/rendering/proxy_box.dart:3228:13)
#5352 RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#5353 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#5354 RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#5355 _RenderTheatre.performLayout (package:flutter/src/widgets/overlay.dart:702:15)
#5356 RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#5357 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#5358 RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#5359 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#5360 RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#5361 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#5362 RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#5363 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#5364 RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#5365 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#5366 RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#5367 RenderView.performLayout (package:flutter/src/rendering/view.dart:170:13)
#5368 RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1632:7)
#5369 PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:889:18)
#5370 RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:404:19)
#5371 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:867:13)
#5372 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:286:5)
#5373 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1117:15)
#5374 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1056:9)
#5375 SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:865:7)
(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
════════════════════════════════════════════════════════════════════════════════════════════════════
其余屏幕viz(ShoppingPage(),FavoritesPage(),ProfilePage())是简单的窗口小部件,其文本包裹在中间。