【发布时间】:2021-05-04 02:27:31
【问题描述】:
我用 bottom_nav_bar 创建页面,但现在我无法在模拟器中打开它,bcs 错误错误:无法将“_MainPage”类型的值分配给“Widget”类型的变量。
这里的问题:'/Main': (BuildContext context) => _MainPage(),
这里代码:
import 'package:flutter/material.dart';
class LogInPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Log In")),
body: Center(
child: Row(
children: <Widget>[
RaisedButton(
onPressed: () {
Navigator.pushNamed(context, '/Main');
},
child: Text("Log In")),
RaisedButton(
onPressed: () {
Navigator.pushNamed(context, '/LogIn/Registr');
},
child: Text("Registration")),
],
)),
);
}
}
class QRCodePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("QR Scan")),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pushNamed(context, '/Main');
},
child: Text("Scan")),
));
}
}
class RegistrPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Registration")),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pushNamed(context, '/Main');
},
child: Text("Log In")),
));
}
}
class MyApp extends StatefulWidget {
@override
_MainPage createState() => _MainPage();
}
class _MainPage extends State<MyApp> {
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(title: Text("Main")),
body: Container(),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home), backgroundColor: Colors.blue),
BottomNavigationBarItem(
icon: Icon(Icons.qr_code), backgroundColor: Colors.blue),
BottomNavigationBarItem(
icon: Icon(Icons.restaurant_menu), backgroundColor: Colors.blue),
BottomNavigationBarItem(
icon: Icon(Icons.shopping_basket), backgroundColor: Colors.blue),
],
onTap: (index){
setState(() {
_currentIndex = index;
});
},
),
);
}
}
void main()
{
runApp(MaterialApp(
initialRoute: '/LogIn',
routes: {
'/LogIn': (BuildContext context) => LogInPage(),
'/QRScan': (BuildContext context) => QRCodePage(),
'/LogIn/Registr': (BuildContext context) => RegistrPage(),
'/Main': (BuildContext context) => _MainPage(),
},
));
}
【问题讨论】:
-
好吧,主页不是小部件,请使用以 StatelessWidget 或 stateFul 小部件结尾的名称。
-
如果我设置了 StatelessWidget 或 StatefulWidget,那么在 bottom_nav_bar 中的图标之间导航将不起作用,因为我们在 StatelessWidget 或 StatefulWidge 中没有 setState 方法