Flutter:BottomNavigationBar文件给出上下文错误



我创建了一个bottomnavigatiobar文件,并在需要的地方调用它。所以,我需要管理单个文件。

这是我的密码。

import 'package:DTG/global_var.dart';
import 'package:DTG/routes/router.gr.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class BottomNavigationTabBarView extends StatelessWidget {
var _currentIndex = 0;
Function onTabChange;
bool dacType = false;
int dtgAcType;
BottomNavigationTabBarView(this._currentIndex, {this.onTabChange});
@override
Widget build(BuildContext context) {
return bottomNavigationTabBarView();
}

void onTabTapped(int index) {
_currentIndex = index;
final dtgAcType = Glob().g_acType ?? 0;
onTabChange(index);
if (_currentIndex == 0) {
if (dtgAcType == 1) {
ExtendedNavigator.of(context).push(
Routes.thome,
);
} else {
ExtendedNavigator.of(context).push(
Routes.dhome,
);
}
} else if (_currentIndex == 1) {
ExtendedNavigator.of(context).push(
Routes.search,
);
} else if (_currentIndex == 2) {
ExtendedNavigator.of(context).push(
Routes.search,
);
} else {
ExtendedNavigator.of(context).push(
Routes.settings,
);
}
}

BottomNavigationBar bottomNavigationTabBarView() {
const iconSize = 25.0;
return BottomNavigationBar(
currentIndex: _currentIndex,
backgroundColor: Colors.black.withOpacity(0.9),
selectedItemColor: Colors.pink[300],
unselectedItemColor: Colors.white,
elevation: 0.5,
onTap: onTabTapped,

type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: SizedBox.shrink(),
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: SizedBox.shrink(),
),
/*BottomNavigationBarItem(
icon: Icon(Icons.notification_important),
title: SizedBox.shrink(),
),*/
BottomNavigationBarItem(
icon: new Stack(
children: <Widget>[
new Icon(Icons.notifications),
new Positioned(
right: 0,
child: new Container(
padding: EdgeInsets.all(1),
decoration: new BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(6),
),
constraints: BoxConstraints(
minWidth: 12,
minHeight: 12,
),
child: new Text(
(Glob().g_acType ?? 0).toString(), //'600',
style: new TextStyle(
color: Colors.white,
fontSize: 9,
),
textAlign: TextAlign.center,
),
),
)
],
),
title: SizedBox.shrink(),
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
title: SizedBox.shrink(),
),
],
);
}
}

出于某种原因,它在上下文中出现了错误。未定义的名称"context"。请尝试将名称更正为已定义的名称,或定义名称。

例如

ExtendedNavigator.of(context).push(
Routes.settings,
);

我不知道如何解决这个问题。

解决方案1:传递context并将onTabTapped移动/内联到onTap

bottomNavigationTabBarView(context);
...
BottomNavigationBar bottomNavigationTabBarView(BuildContext context)
...
onTap: (int index) {
_currentIndex = index;
final dtgAcType = Glob().g_acType ?? 0;
onTabChange(index);
if (_currentIndex == 0) {

解决方案1 的代码片段

class BottomNavigationTabBarView extends StatelessWidget {
var _currentIndex = 0;
Function onTabChange;
bool dacType = false;
int dtgAcType;
BottomNavigationTabBarView(this._currentIndex, {this.onTabChange});
@override
Widget build(BuildContext context) {
return bottomNavigationTabBarView(context);
}
BottomNavigationBar bottomNavigationTabBarView(BuildContext context) {
const iconSize = 25.0;
return BottomNavigationBar(
currentIndex: _currentIndex,
backgroundColor: Colors.black.withOpacity(0.9),
selectedItemColor: Colors.pink[300],
unselectedItemColor: Colors.white,
elevation: 0.5,
onTap: (int index) {
_currentIndex = index;
final dtgAcType = Glob().g_acType ?? 0;
onTabChange(index);
if (_currentIndex == 0) {
if (dtgAcType == 1) {
ExtendedNavigator.of(context).push(
Routes.thome,
);
} else {
ExtendedNavigator.of(context).push(
Routes.dhome,
);
}
} else if (_currentIndex == 1) {
ExtendedNavigator.of(context).push(
Routes.search,
);
} else if (_currentIndex == 2) {
ExtendedNavigator.of(context).push(
Routes.search,
);
} else {
ExtendedNavigator.of(context).push(
Routes.settings,
);
}
},
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: SizedBox.shrink(),
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: SizedBox.shrink(),
),
/*BottomNavigationBarItem(
icon: Icon(Icons.notification_important),
title: SizedBox.shrink(),
),*/
BottomNavigationBarItem(
icon: new Stack(
children: <Widget>[
new Icon(Icons.notifications),
new Positioned(
right: 0,
child: new Container(
padding: EdgeInsets.all(1),
decoration: new BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(6),
),
constraints: BoxConstraints(
minWidth: 12,
minHeight: 12,
),
child: new Text(
(Glob().g_acType ?? 0).toString(), //'600',
style: new TextStyle(
color: Colors.white,
fontSize: 9,
),
textAlign: TextAlign.center,
),
),
)
],
),
title: SizedBox.shrink(),
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
title: SizedBox.shrink(),
),
],
);
}
}

解决方案2:将StatelessWidget转换为StatefulWidget,无需通过context

class BottomNavigationTabBarView extends StatefulWidget {
var _currentIndex = 0;
Function onTabChange;
BottomNavigationTabBarView(this._currentIndex, {this.onTabChange});
@override
_BottomNavigationTabBarViewState createState() => _BottomNavigationTabBarViewState();
}
class _BottomNavigationTabBarViewState extends State<BottomNavigationTabBarView> {
bool dacType = false;
int dtgAcType;
@override
Widget build(BuildContext context) {
return bottomNavigationTabBarView();
}
void onTabTapped(int index) {
widget._currentIndex = index;
final dtgAcType = Glob().g_acType ?? 0;
widget.onTabChange(index);
if (widget._currentIndex == 0) {
if (dtgAcType == 1) {
ExtendedNavigator.of(context).push(
Routes.thome,
);
} else {
ExtendedNavigator.of(context).push(
Routes.dhome,
);
}
} else if (widget._currentIndex == 1) {
ExtendedNavigator.of(context).push(
Routes.search,
);
} else if (widget._currentIndex == 2) {
ExtendedNavigator.of(context).push(
Routes.search,
);
} else {
ExtendedNavigator.of(context).push(
Routes.settings,
);
}
}
BottomNavigationBar bottomNavigationTabBarView() {
const iconSize = 25.0;
return BottomNavigationBar(
currentIndex: widget._currentIndex,
backgroundColor: Colors.black.withOpacity(0.9),
selectedItemColor: Colors.pink[300],
unselectedItemColor: Colors.white,
elevation: 0.5,
onTap: onTabTapped,
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: SizedBox.shrink(),
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: SizedBox.shrink(),
),
/*BottomNavigationBarItem(
icon: Icon(Icons.notification_important),
title: SizedBox.shrink(),
),*/
BottomNavigationBarItem(
icon: new Stack(
children: <Widget>[
new Icon(Icons.notifications),
new Positioned(
right: 0,
child: new Container(
padding: EdgeInsets.all(1),
decoration: new BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(6),
),
constraints: BoxConstraints(
minWidth: 12,
minHeight: 12,
),
child: new Text(
(Glob().g_acType ?? 0).toString(), //'600',
style: new TextStyle(
color: Colors.white,
fontSize: 9,
),
textAlign: TextAlign.center,
),
),
)
],
),
title: SizedBox.shrink(),
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
title: SizedBox.shrink(),
),
],
);
}
}

最新更新