【发布时间】:2021-06-13 16:35:10
【问题描述】:
我正在制作一个带有页面视图和自定义底部导航栏 CustomBottomNavigator 的应用。更改页面时图标会更改颜色,但是每当我为单个 CustomBottomNavigatorItem 实现 Ontap 方法时,它都会提供一个用于空值的空检查运算符。错误附加在代码之后。我不明白即使在观看了几个视频并查看了文档之后也没有任何安全性
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart'as firebase_auth;
import 'package:flutter_fashion/main.dart';
class CustomBottomNavigator extends StatefulWidget {
final int tab;
final Function(int) tabPressed;
const CustomBottomNavigator({Key? key, required this.tab, required this.tabPressed}) : super(key: key);
@override
_CustomBottomNavigatorState createState() => _CustomBottomNavigatorState();
}
class _CustomBottomNavigatorState extends State<CustomBottomNavigator> {
int _selectedTab=0;
@override
Widget build(BuildContext context) {
_selectedTab=widget.tab;
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(12), topRight: Radius.circular(12)),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.05),
spreadRadius: 1.0,
blurRadius: 30.0)
]),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
CustomBottomNavigatorItem(
icon: Icons.home_outlined,
selected: _selectedTab==0?true:false,
onPressed: () {
widget.tabPressed(0);
},
),
CustomBottomNavigatorItem(
icon: Icons.code_rounded,
selected: _selectedTab==1?true:false,
onPressed: () {
widget.tabPressed(1);
},
),
CustomBottomNavigatorItem(
icon: Icons.bookmark_border_rounded,
selected: _selectedTab==2?true:false,
onPressed: () {
widget.tabPressed(2);
},
),
CustomBottomNavigatorItem(
icon: Icons.logout_rounded,
selected: _selectedTab==3?true:false,
onPressed: () {
firebase_auth.FirebaseAuth.instance.signOut();
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (builder) => MyApp()),
(route) => false);
},
),
],
),
);
}
}
class CustomBottomNavigatorItem extends StatelessWidget {
final IconData icon;
final bool selected;
final Function onPressed;
CustomBottomNavigatorItem(
{required this.icon, required this.selected, required this.onPressed,});
@override
Widget build(BuildContext context) {
bool _selected = selected;
return GestureDetector(
onTap: () => onPressed,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 28),
decoration: BoxDecoration(
border: Border(
top: BorderSide(
color: _selected
? Theme.of(context).accentColor
: Colors.transparent,
width: 2.0))),
child: Icon(
icon,
size: 24,
color: _selected ? Theme.of(context).accentColor : Colors.black,
),
),
);
}
}
主页
import 'package:flutter/material.dart';
import 'package:flutter_fashion/Views/Widgets/bottom_navigator.dart';
import 'package:flutter_fashion/Views/Widgets/page_view_tabs/ml_page_tab.dart';
import 'package:flutter_fashion/Views/Widgets/page_view_tabs/saved_page_tab.dart';
import 'package:flutter_fashion/Views/Widgets/page_view_tabs/home_page_tab.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late final PageController _tabPageController;
late int _selectedTab=0;
@override
void initState() {
_tabPageController=PageController();
super.initState();
}
@override
void dispose() {
_tabPageController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: PageView(
controller: _tabPageController,
onPageChanged: (num){
setState(() {
_selectedTab=num;
});
},
children: [
HomeTab(),
MachineLearningTab(),
SavedTab(),
],
),
),
CustomBottomNavigator(tab: _selectedTab,tabPressed: (num){
_tabPageController.animateToPage(num, duration: Duration(milliseconds: 300), curve: Curves.easeOutCubic);
},)
],
),
),
);
}
}
【问题讨论】:
-
问题似乎不在于您发布的代码,而在于您的主页代码,可能来自使用某种自定义滚动小部件。自 null-safety 以来,我发现 Flutter 库中出现了类似的错误,并且必须找到使用 Flutter 提供的可滚动小部件来完成工作的方法。有关详细信息,请参阅此问题:github.com/flutter/flutter/issues/66250
-
看到我在错误信息之后添加了主页我认为它不是由于主页而发生的,如果您注意到应用程序也注销了,那么所有手势检测器都在初始化时被点击
标签: flutter dart flutter-layout flutter-state