我的应用程序有BottomNavigationBar
两个BottomNavigationBarItem
:HomePage()
和UserPage()
.
main()
运行App()
并App()
返回FutureBuilder
以处理颤振初始化错误和加载 gif。然后它返回MaterialApp
第一页,AuthPage()
或者Navigation()
取决于用户登录状态。Navigation()
有BottomNavigationBar
.
这是我的颤振检查器图。
如您所见,我的导航中有两个页面(HomePage()
和),但仅显示在我的检查器中。UserPage()
HomePage()
真正的问题是Theme.of(context).textTheme.headline1
适用于HomePage()
但不适用于UserPage()
. 我ThemeData
在 中指定MaterialApp
。为什么我不能跟踪上下文并ThemeData
从中检索UserPage()
?
我还没有尝试过,但很可能这Theme.of(context).textTheme.headline1
也行不通AuthPage()
。
代码如下。
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(App());
}
class App extends StatelessWidget {
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _initialization,
builder: (context, snapshot) {
if(snapshot.connectionState != ConnectionState.done) {
return LoadingGif();
} else if(snapshot.hasError) {
print(snapshot.error);
return ErrorPage(context);
}
return MaterialApp(
title: 'My App',
theme: theme, //ThemeData theme is defined in another file
home: StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Navigation();
} else {
return AuthPage();
}
},
),
routes: {
'auth': (context) => AuthPage(),
'home': (context) => Navigation(),
},
);
},
);
}
}
class Navigation extends StatefulWidget {
const Navigation({Key? key}) : super(key: key);
@override
_NavigationState createState() => _NavigationState();
}
class _NavigationState extends State<Navigation> {
int _selectedIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: _widgetOptions.elementAt(_selectedIndex),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
label: 'Home',
icon: Icon(Icons.home_outlined, size: 36)
),
BottomNavigationBarItem(
label: 'My Page',
icon: Icon(Icons.account_circle_outlined, size: 36)
)
],
onTap: (int index) {
setState(() {
_selectedIndex = index;
});
},
),
);
}
List _widgetOptions = [
HomePage(),
UserPage()
];
}
这是UserPage()
.
class UserPage extends StatelessWidget {
UserPage({Key? key}) : super(key: key);
UserProfileHandler _userProfileHandler = new UserProfileHandler();
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
return Scaffold(
appBar: TopBar(
pageTitle: '내 정보',
actionList: <Widget>[
IconButton(
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => SettingsPage()));
},
icon: Icon(Icons.settings),
)
]
),
body: SingleChildScrollView(
child: Column(
children: [
UserTapBody(),
Divider(),
]
),
)
);
}
Widget UserTapBody() {
return StreamBuilder(
stream: _userProfileHandler.userProfileStream(),
builder: (context, snapshot) {
//error, loading
if (snapshot.hasError) {
print(snapshot.error);
return Center(child: ErrorPage(context));
} else if (snapshot.connectionState == ConnectionState.waiting) {
return LoadingGif();
}
UserProfile userProfile = snapshot.data as UserProfile;
return DefaultTabController(
length: 3, // length of tabs
initialIndex: 0,
child: ListView(
shrinkWrap: true,
children: <Widget>[
const TabBar(
labelColor: brandColor,
labelStyle: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold
),
indicatorColor: brandColor,
unselectedLabelColor: Colors.black,
unselectedLabelStyle: TextStyle(fontWeight: FontWeight.normal),
tabs: [
Tab(text: '회원 정보'),
Tab(text: '만남 내역'),
Tab(text: '멘토 정보'),
],
),
Container(
height: 500, //height of TabBarView
child: TabBarView(
children: <Widget>[
UserDetail(context, userProfile),
Container(
child: Center(
child: Text('만남 내역', style: TextStyle(
fontSize: 22, fontWeight: FontWeight.bold)),
),
),
Container(
child: Center(
child: Text('멘토 정보', style: TextStyle(
fontSize: 22, fontWeight: FontWeight.bold)),
),
),
]
)
)
]
)
);
}
);
}
Widget UserDetail(context, userProfile) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Row(
children: [ // These are the ones that Theme.of(context) doesn't work
Text('자기소개', style: Theme.of(context).textTheme.caption),
Spacer(),
EditTextButton(context: context, editPage: editIntro(userProfile: userProfile))
],
),
Html(data: userProfile.intro ?? ""),
Text('이력 및 경험', style: Theme.of(context).textTheme.caption,),
Html(data: userProfile.exp ?? ""),
Text('저서', style: Theme.of(context).textTheme.caption,),
Html(data: userProfile.pub ?? ""),
]
),
);
}
}