我是Dart Flattery的初学者。我打算使用BottomNavigationBar创建多个页面,以便通过单击每个选项来显示另一个页面。例如: 通过选择红色屏幕的红色选项, 通过选择绿色选项“绿页” 等等 节目。但是我遇到这个错误:
Launching lib\main.dart on SM N950N in debug mode...
Initializing gradle...
Resolving dependencies...
Gradle task 'assembleDebug'...
Built build\app\outputs\apk\debug\app-debug.apk.
Installing build\app\outputs\apk\app.apk...
Syncing files to device SM N950N...
D/ ( 3965): HostConnection::get() New Host Connection established 0xaec29420, tid 3984
I/flutter ( 3965): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 3965): The following assertion was thrown building DefaultTextStyle(debugLabel: fallback style; consider
I/flutter ( 3965): putting your text in a Material, inherit: true, color: Color(0xd0ff0000), family: monospace, size:
I/flutter ( 3965): 48.0, weight: 900, decoration: double Color(0xffffff00) TextDecoration.underline, softWrap: wrapping
I/flutter ( 3965): at box width, overflow: clip):
I/flutter ( 3965): 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1321 pos 14:
I/flutter ( 3965): 'observer.navigator == null': is not true.
I/flutter ( 3965): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 3965): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 3965): In either case, please report this assertion by filing a bug on GitHub:
I/flutter ( 3965): https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter ( 3965): When the exception was thrown, this was the stack:
I/flutter ( 3965): #2 NavigatorState.initState (package:flutter/src/widgets/navigator.dart:1321:14)
I/flutter ( 3965): #3 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3830:58)
I/flutter ( 3965): #4 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 3965): #5 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 3965): #6 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 3965): #7 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #8 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #9 ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
I/flutter ( 3965): #10 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #11 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
I/flutter ( 3965): #12 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #13 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #14 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #15 StatelessElement.update (package:flutter/src/widgets/framework.dart:3781:5)
I/flutter ( 3965): #16 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #17 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #18 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #19 StatelessElement.update (package:flutter/src/widgets/framework.dart:3781:5)
I/flutter ( 3965): #20 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #21 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #22 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #23 StatelessElement.update (package:flutter/src/widgets/framework.dart:3781:5)
I/flutter ( 3965): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #25 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #26 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #27 ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
I/flutter ( 3965): #28 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #29 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #30 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #31 ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
I/flutter ( 3965): #32 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #33 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
I/flutter ( 3965): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #35 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #36 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #37 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
I/flutter ( 3965): #38 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #39 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #40 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #41 ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
I/flutter ( 3965): #42 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #43 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #44 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #45 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
I/flutter ( 3965): #46 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #47 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #48 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #49 ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
I/flutter ( 3965): #50 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #51 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #52 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #53 ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
I/flutter ( 3965): #54 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #55 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #56 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #57 StatelessElement.update (package:flutter/src/widgets/framework.dart:3781:5)
I/flutter ( 3965): #58 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #59 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #60 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #61 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
I/flutter ( 3965): #62 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #63 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #64 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #65 ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
I/flutter ( 3965): #66 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #67 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #68 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #69 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
I/flutter ( 3965): #70 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #71 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #72 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #73 ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
I/flutter ( 3965): #74 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #75 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #76 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #77 ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
I/flutter ( 3965): #78 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #79 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4585:32)
I/flutter ( 3965): #80 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4975:17)
I/flutter ( 3965): #81 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #82 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #83 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #84 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
I/flutter ( 3965): #85 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #86 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #87 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #88 ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
I/flutter ( 3965): #89 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #90 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #91 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #92 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
I/flutter ( 3965): #93 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #94 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
I/flutter ( 3965): #95 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #96 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #97 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #98 StatelessElement.update (package:flutter/src/widgets/framework.dart:3781:5)
I/flutter ( 3965): #99 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #100 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
I/flutter ( 3965): #101 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #102 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #103 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #104 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
I/flutter ( 3965): #105 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #106 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #107 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #108 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
I/flutter ( 3965): #109 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #110 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #111 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #112 ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
I/flutter ( 3965): #113 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #114 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #115 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #116 ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
I/flutter ( 3965): #117 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #118 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #119 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #120 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
I/flutter ( 3965): #121 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 3965): #122 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 3965): #123 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3965): #124 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2286:33)
I/flutter ( 3965): #125 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:676:20)
I/flutter ( 3965): #126 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:219:5)
I/flutter ( 3965): #127 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
I/flutter ( 3965): #128 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9)
I/flutter ( 3965): #129 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:842:5)
I/flutter ( 3965): #130 _invoke (dart:ui/hooks.dart:154:13)
I/flutter ( 3965): #131 _drawFrame (dart:ui/hooks.dart:143:3)
I/flutter ( 3965): (elided 2 frames from class _AssertionError)
I/flutter ( 3965): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter ( 3965): Another exception was thrown: A GlobalKey was used multiple times inside one widget's child list.
I/flutter ( 3965): Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1321 pos 14: 'observer.navigator == null': is not true.
我不知道问题出在哪里!我已经多次检查了代码,一切似乎都正确!
答案 0 :(得分:0)
我使用了这个地址: multiple navigators with bottomnavigationbar
Main.dart:
import 'package:cookbook/cookbook.dart';
//import 'package:cookbook/UI/App.dart';
void main() => runApp(CookBook());
cookbook.dart:
import 'package:flutter/material.dart';
//import 'package:cookbook/UI/Home.dart';
//import 'package:cookbook/UI/Login.dart';
//import 'package:cookbook/UI/SignUp.dart';
import 'package:cookbook/UI/App.dart';
//import 'package:flutter_localizations/flutter_localizations.dart';
class CookBook extends StatelessWidget{
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
// localizationsDelegates: [
// GlobalMaterialLocalizations.delegate,
// GlobalMaterialLocalizations.delegate,
// ],
// supportedLocales: [
// Locale("en","US"),
// Locale("fa","IR"),
// ],
title: "",
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: new App(),
// initialRoute: '/',
// routes:{
// '/': (context)=> Home(),
// '/login': (context)=> Login(),
// '/SignUp': (context)=> SignUp(),
// '/App': (context)=> App(),
// }
);
}
}
App.dart:
import 'package:flutter/material.dart';
import 'package:cookbook/UI/TabItem.dart';
import 'package:cookbook/UI/nestedRoutes/AppRoutes.dart';
import 'package:cookbook/UI/nestedRoutes/TabHelper.dart';
import 'package:cookbook/UI/MasterPage.dart';
import 'package:cookbook/UI/DetailPage.dart';
import 'package:cookbook/UI/nestedRoutes/BottomNavigation.dart';
class App extends StatefulWidget{
@override
State<StatefulWidget> createState() => _State();
}
class _State extends State<App> with NavigatorObserver{
TabItem currentTab=TabItem.red;
Map<TabItem, RouteState> routes={
TabItem.red: RouteState(),
TabItem.green:RouteState(),
TabItem.blue:RouteState(),
};
void _selectTab(TabItem tabItem){
setState(()=> currentTab=tabItem);
}
void _push() async{
print('push ${TabHelper.description(currentTab)}');
routes[currentTab].name=AppRoutes.details;
await routes[currentTab]
.navigatorKey
.currentState
.pushNamed(AppRoutes.details);
routes[currentTab].name=AppRoutes.root;
print('pop ${TabHelper.description(currentTab)}');
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _biuldBody(),
bottomNavigationBar: BottomNavigation(
currentTab: currentTab,
onSelectTab: _selectTab,
),
);
}
Widget _biuldBody(){
return MaterialApp(
//debugShowCheckedModeBanner: false,
navigatorKey: routes[currentTab].navigatorKey,
navigatorObservers: [this],
theme: ThemeData(
primarySwatch: TabHelper.color(currentTab)
),
initialRoute: routes[currentTab].name,
routes:{
AppRoutes.root:(context)=>MasterPage(
color: TabHelper.color(currentTab),
title: TabHelper.description(currentTab),
onPush: _push,
),
AppRoutes.details:(context)=>DetailPage()
}
);
}
}
AppRoutes.dart:
import 'package:flutter/material.dart';
class AppRoutes{
static const String root='/';
static const String details='/details';
}
class RouteState{
RouteState({this.name: AppRoutes.root});
String name;
//one navigator for each route
final navigatorKey= GlobalKey<NavigatorState>();
}
bottomnavigation.dart:
import 'package:flutter/material.dart';
import 'package:cookbook/UI/TabItem.dart';
import 'package:cookbook/UI/nestedRoutes/TabHelper.dart';
class BottomNavigation extends StatelessWidget{
BottomNavigation({this.currentTab,this.onSelectTab});
final TabItem currentTab;
final ValueChanged<TabItem> onSelectTab;
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: [
_buildItem(tabItem: TabItem.red),
_buildItem(tabItem: TabItem.green),
_buildItem(tabItem: TabItem.blue),
],
onTap: (index)=>onSelectTab(
TabHelper.item(index: index)
),
);
}
BottomNavigationBarItem _buildItem({TabItem tabItem}){
String text=TabHelper.description(tabItem);
IconData icon=TabHelper.icon(tabItem);
return BottomNavigationBarItem(
icon: Icon(
icon,
color: _colorTabMatching(item: tabItem),
),
title: Text(
text,
style: TextStyle(
color: _colorTabMatching(item: tabItem)
),
)
);
}
Color _colorTabMatching({TabItem item}){
return currentTab == item ? TabHelper.color(item): Colors.grey;
}
}
tabhelper.dart:
import 'package:flutter/material.dart';
import 'package:cookbook/UI/TabItem.dart';
class TabHelper{
static TabItem item({int index}){
switch(index){
case 0:
return TabItem.red;
case 1:
return TabItem.green;
case 2:
return TabItem.blue;
}
return TabItem.red;
}
static String description(TabItem tabItem){
switch(tabItem){
case TabItem.red:
return 'red';
case TabItem.green:
return 'green';
case TabItem.blue:
return 'blue';
}
return '';
}
static MaterialColor color(TabItem tabItem){
switch(tabItem){
case TabItem.red:
return Colors.red;
case TabItem.green:
return Colors.green;
case TabItem.blue:
return Colors.blue;
}
return Colors.yellow;
}
static IconData icon(TabItem tabItem){
return Icons.layers;
}
}
detailpage.dart:
import 'package:flutter/material.dart';
class DetailPage extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Container(
color: Theme.of(context).primaryColor,
),
);
}
}
masterpage.dart:
import 'package:flutter/material.dart';
class MasterPage extends StatelessWidget{
MasterPage({this.color,this.title,this.onPush});
final Color color;
final String title;
final VoidCallback onPush;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
backgroundColor: color,
),
body: Container(
color: color,
alignment: Alignment.center,
child: FlatButton(
onPressed: onPush,
child: Text(
'PUSH',
style: TextStyle(
fontSize: 32.0,
color: Colors.white
),
),)
),
);
}
}
在运行模式下,它将以一个顺序显示一个或两个视图,然后显示两个: