我的应用程序获得类型'Home Page'不是类型'bool'错误的子类型。
我的主页代码
import 'package:flutter/cupertino.dart';
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Container();
}
}
My Main code
import 'package:flutter/material.dart';
import 'package:medreminder/home_page.dart';
import 'package:medreminder/profile_page.dart';
import 'package:medreminder/settings_page.dart';
void main() => runApp(MainPage());
class MainPage extends StatefulWidget {
const MainPage({super.key});
@override
State<MainPage> createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
List pages = [
HomePage(),
SettingPage(),
ProfilePage()
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.4,
),
extendBody: pages[0],
bottomNavigationBar: BottomNavigationBar(
selectedItemColor: Colors.black,
items: [
BottomNavigationBarItem(label: "Home", icon: Icon(Icons.home)),
BottomNavigationBarItem(label: "Settings", icon: Icon(Icons.settings)),
BottomNavigationBarItem(label: "Profile", icon: Icon(Icons.account_circle)),
],
),
);
}
}
这里是完整的错误
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _TypeError was thrown building MainPage(dirty, dependencies: [MediaQuery], state:
_MainPageState#e8259):
type 'HomePage' is not a subtype of type 'bool'
The relevant error-causing widget was:
MainPage
MainPage:file:///D:/Kuliah/Mata%20Kuliah/Pemrograman%20Mobile/medicine_reminder/code/lib/main.dart:19:13
When the exception was thrown, this was the stack:
#0 _MainPageState.build (package:medreminder/main_page.dart:28:24)
#1 StatefulElement.build (package:flutter/src/widgets/framework.dart:4992:27)
#2 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4878:15)
#3 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#4 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#5 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4859:5)
#6 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5041:11)
#7 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4853:5)
... Normal element mounting (275 frames)
#282 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3863:16)
#283 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6435:36)
#284 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6447:32)
... Normal element mounting (407 frames)
#691 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3863:16)
#692 Element.updateChild (package:flutter/src/widgets/framework.dart:3592:18)
#693 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1195:16)
#694 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1164:5)
#695 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1111:18)
#696 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2605:19)
#697 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1110:13)
#698 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:945:7)
#699 WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:925:7)
(elided 4 frames from class _RawReceivePortImpl, class _Timer, and dart:async-patch)
════════════════════════════════════════════════════════════════════════════════════════════════════
您需要在extendBody
上传递一个bool
可以是extendBody: true,
你还需要在顶部提供MaterialApp,它可以是
void main() => runApp(MaterialApp(home: MainPage()));
class MainPage extends StatefulWidget {
const MainPage({super.key});
@override
State<MainPage> createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
List<Widget> pages = [HomePage(), Text("s"), Text("ProfilePage")];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.4,
child: pages[0],
),
extendBody: true,
bottomNavigationBar: BottomNavigationBar(
selectedItemColor: Colors.black,
items: [
BottomNavigationBarItem(label: "Home", icon: Icon(Icons.home)),
BottomNavigationBarItem(
label: "Settings", icon: Icon(Icons.settings)),
BottomNavigationBarItem(
label: "Profile", icon: Icon(Icons.account_circle)),
],
),
);
}
}