flutter - 根据闭包上下文的要求,返回类型“_MainPage”不是“Widget”
问题描述
我使用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(),
},
));
}
解决方案
不应该MyApp
代替_MainPage
吗?_MainPage
不是小部件而是状态。
void main() {
runApp(MaterialApp(
initialRoute: '/Main',
routes: {
'/LogIn': (BuildContext context) => LogInPage(),
'/QRScan': (BuildContext context) => QRCodePage(),
'/LogIn/Registr': (BuildContext context) => RegistrPage(),
'/Main': (BuildContext context) => MyApp(),
},
));
}
为了更清楚起见,您可能应该重命名MyApp
为MainPage
。
此外,您必须定义每个的label
or :title
BottomNavigationBarItem
items: [
BottomNavigationBarItem(
label: 'Home',
icon: Icon(Icons.home),
backgroundColor: Colors.blue
),
...
],
推荐阅读
- flutter - Flutter 部署不适用于 GitHub 页面
- security - Magento 2.4.2 安全扫描错误报告 Visbot 恶意软件
- java - 如何动态实例化一个类型在运行时决定的类
- python - 处理异常的不同返回类型
- r - 如何按位于 R 中的列中的次数拆分行?
- azure-sql-database - 我们如何检查某人在 Azure SQL 中运行查询的频率?
- android - 我无法构建 Flutter apk,因为访问“FlutterEngine”类
- google-chrome-extension - 无法从 Chrome 扩展文件系统加载 html 文件
- .htaccess - htaccess - 隐藏文件扩展名并仅保留变量的值
- vb.net - 使用 .NET 5 的 Visual Basic 项目的高 DPI 模式错误