Skip to content

Commit

Permalink
完善主页面的显示 (#64)
Browse files Browse the repository at this point in the history
* 修改默认主页时,不会重新刷新当前页面

* 统一不同主页的显示

* AppBar 上不显示头像
  • Loading branch information
he0119 authored Aug 8, 2020
1 parent f8dade9 commit f9c0f96
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 124 deletions.
2 changes: 1 addition & 1 deletion lib/blocs/tab/tab_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:smart_home/models/models.dart';

part 'tab_event.dart';
Expand Down
3 changes: 2 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ class MyApp extends StatelessWidget {
return SplashPage();
}
return BlocProvider<TabBloc>(
create: (context) => TabBloc(defaultTab: state.defaultPage),
create: (context) => TabBloc(defaultTab: state.defaultPage)
..add(TabChanged(state.defaultPage)),
child: HomePage(),
);
},
Expand Down
74 changes: 33 additions & 41 deletions lib/pages/blog/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:smart_home/blocs/blocs.dart';
import 'package:smart_home/models/app_tab.dart';
import 'package:smart_home/models/grobal_keys.dart';
import 'package:smart_home/pages/blog/setting_page.dart';
import 'package:smart_home/utils/launch_url.dart';
import 'package:smart_home/widgets/tab_selector.dart';
import 'package:smart_home/widgets/home_page.dart';
import 'package:webview_flutter/webview_flutter.dart';

enum BlogMenu { admin, setting }
Expand All @@ -31,41 +30,39 @@ class _BlogHomePageState extends State<BlogHomePage> {
future: _controller.future,
builder: (BuildContext context,
AsyncSnapshot<WebViewController> controller) =>
Scaffold(
key: scaffoldKey,
appBar: AppBar(
title: Text('博客'),
actions: [
PopupMenuButton(
onSelected: (value) async {
if (value == BlogMenu.admin && state.blogAdminUrl != null) {
if (kIsWeb) {
await launchUrl(state.blogAdminUrl);
} else if (controller.hasData) {
controller.data.loadUrl(state.blogAdminUrl);
}
} else {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => BlogSettingPage(
blogUrl: state.blogUrl,
blogAdminUrl: state.blogAdminUrl,
),
));
MyHomePage(
title: '博客',
activeTab: AppTab.blog,
actions: [
PopupMenuButton(
onSelected: (value) async {
if (value == BlogMenu.admin && state.blogAdminUrl != null) {
if (kIsWeb) {
await launchUrl(state.blogAdminUrl);
} else if (controller.hasData) {
controller.data.loadUrl(state.blogAdminUrl);
}
},
itemBuilder: (context) => <PopupMenuItem<BlogMenu>>[
PopupMenuItem(
value: BlogMenu.admin,
child: Text('进入后台'),
),
PopupMenuItem(
value: BlogMenu.setting,
child: Text('设置网址'),
),
],
)
],
),
} else {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => BlogSettingPage(
blogUrl: state.blogUrl,
blogAdminUrl: state.blogAdminUrl,
),
));
}
},
itemBuilder: (context) => <PopupMenuItem<BlogMenu>>[
PopupMenuItem(
value: BlogMenu.admin,
child: Text('进入后台'),
),
PopupMenuItem(
value: BlogMenu.setting,
child: Text('设置网址'),
),
],
)
],
body: !kIsWeb
? WillPopScope(
onWillPop: () async {
Expand Down Expand Up @@ -94,11 +91,6 @@ class _BlogHomePageState extends State<BlogHomePage> {
),
)
: SettingButton(),
bottomNavigationBar: TabSelector(
activeTab: AppTab.blog,
onTabSelected: (tab) =>
BlocProvider.of<TabBloc>(context).add(TabChanged(tab)),
),
floatingActionButton: controller.hasData
? FloatingActionButton(
child: Icon(Icons.open_in_new),
Expand Down
23 changes: 4 additions & 19 deletions lib/pages/board/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,22 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:smart_home/blocs/blocs.dart';
import 'package:smart_home/blocs/board/blocs.dart';
import 'package:smart_home/models/app_tab.dart';
import 'package:smart_home/models/grobal_keys.dart';
import 'package:smart_home/pages/board/topic_edit_page.dart';
import 'package:smart_home/pages/board/widgets/topic_list.dart';
import 'package:smart_home/pages/error_page.dart';
import 'package:smart_home/pages/loading_page.dart';
import 'package:smart_home/widgets/gravatar.dart';
import 'package:smart_home/widgets/tab_selector.dart';
import 'package:smart_home/widgets/home_page.dart';

class BoardHomePage extends StatelessWidget {
const BoardHomePage({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return BlocBuilder<AuthenticationBloc, AuthenticationState>(
builder: (context, state) => Scaffold(
key: scaffoldKey,
appBar: AppBar(
leading: state is AuthenticationSuccess
? IconButton(
icon: CircleGravatar(email: state.currentUser.email),
onPressed: null,
)
: null,
title: Text('留言板'),
),
builder: (context, state) => MyHomePage(
title: '留言板',
activeTab: AppTab.board,
body: _BoardHomeBody(),
bottomNavigationBar: TabSelector(
activeTab: AppTab.board,
onTabSelected: (tab) =>
BlocProvider.of<TabBloc>(context).add(TabChanged(tab)),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.create),
onPressed: () async {
Expand Down
16 changes: 13 additions & 3 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,21 @@ class _HomePage extends StatelessWidget {
],
);
}
return BlocBuilder<TabBloc, AppTab>(
return BlocConsumer<TabBloc, AppTab>(
listener: (context, activeTab) {
switch (activeTab) {
case AppTab.storage:
BlocProvider.of<StorageHomeBloc>(context).add(StorageHomeStarted());
break;
case AppTab.board:
BlocProvider.of<BoardHomeBloc>(context).add(BoardHomeStarted());
break;
case AppTab.blog:
case AppTab.iot:
}
},
builder: (context, activeTab) {
if (activeTab == AppTab.storage) {
BlocProvider.of<StorageHomeBloc>(context).add(StorageHomeStarted());
return StorageHomePage();
}
if (activeTab == AppTab.blog) {
Expand All @@ -123,7 +134,6 @@ class _HomePage extends StatelessWidget {
if (activeTab == AppTab.iot) {
return IotHomePage();
}
BlocProvider.of<BoardHomeBloc>(context).add(BoardHomeStarted());
return BoardHomePage();
},
);
Expand Down
17 changes: 4 additions & 13 deletions lib/pages/iot/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:smart_home/blocs/blocs.dart';
import 'package:smart_home/blocs/iot/blocs.dart';
import 'package:smart_home/models/app_tab.dart';
import 'package:smart_home/models/grobal_keys.dart';
import 'package:smart_home/models/iot.dart';
import 'package:smart_home/pages/loading_page.dart';
import 'package:smart_home/repositories/iot_repository.dart';
import 'package:smart_home/widgets/home_page.dart';
import 'package:smart_home/widgets/show_snack_bar.dart';
import 'package:smart_home/widgets/tab_selector.dart';
import 'package:smart_home/utils/date_format_extension.dart';

class IotHomePage extends StatelessWidget {
Expand All @@ -36,18 +35,10 @@ class IotHomePage extends StatelessWidget {
),
),
],
child: Scaffold(
key: scaffoldKey,
appBar: AppBar(
title: Text('IOT'),
actions: [],
),
child: MyHomePage(
title: 'IOT',
activeTab: AppTab.iot,
body: _IotHomeBody(),
bottomNavigationBar: TabSelector(
activeTab: AppTab.iot,
onTabSelected: (tab) =>
BlocProvider.of<TabBloc>(context).add(TabChanged(tab)),
),
),
);
},
Expand Down
65 changes: 21 additions & 44 deletions lib/pages/storage/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,69 +1,46 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_sticky_header/flutter_sticky_header.dart';
import 'package:smart_home/blocs/authentication/authentication_bloc.dart';
import 'package:smart_home/blocs/storage/storage_home/storage_home_bloc.dart';
import 'package:smart_home/blocs/tab/tab_bloc.dart';
import 'package:smart_home/models/grobal_keys.dart';
import 'package:smart_home/models/models.dart';
import 'package:smart_home/pages/storage/item_datail_page.dart';
import 'package:smart_home/pages/storage/search_page.dart';
import 'package:smart_home/pages/storage/storage_datail_page.dart';
import 'package:smart_home/utils/date_format_extension.dart';
import 'package:smart_home/pages/error_page.dart';
import 'package:smart_home/widgets/drawer.dart';
import 'package:smart_home/widgets/gravatar.dart';
import 'package:smart_home/pages/loading_page.dart';
import 'package:smart_home/widgets/tab_selector.dart';
import 'package:smart_home/widgets/home_page.dart';

class StorageHomePage extends StatelessWidget {
const StorageHomePage({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return BlocBuilder<AuthenticationBloc, AuthenticationState>(
builder: (context, state) => Scaffold(
key: scaffoldKey,
drawer: AppDrawer(),
appBar: AppBar(
leading: state is AuthenticationSuccess
? IconButton(
icon: CircleGravatar(email: state.currentUser.email),
onPressed: () {
scaffoldKey.currentState.openDrawer();
},
)
: null,
title: Text('物品管理'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (_) => SearchPage()),
);
},
),
],
),
body: _StorageHomeBody(),
bottomNavigationBar: TabSelector(
activeTab: AppTab.storage,
onTabSelected: (tab) =>
BlocProvider.of<TabBloc>(context).add(TabChanged(tab)),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.storage),
onPressed: () async {
return MyHomePage(
title: '物品管理',
activeTab: AppTab.storage,
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => StorageDetailPage(),
),
MaterialPageRoute(builder: (_) => SearchPage()),
);
},
),
],
body: _StorageHomeBody(),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.storage),
onPressed: () async {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => StorageDetailPage(),
),
);
},
),
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'package:smart_home/repositories/repositories.dart';
import 'package:smart_home/widgets/gravatar.dart';
import 'package:version/version.dart';

class AppDrawer extends StatelessWidget {
const AppDrawer({Key key}) : super(key: key);
class MyDrawer extends StatelessWidget {
const MyDrawer({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
Expand Down
42 changes: 42 additions & 0 deletions lib/widgets/home_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:smart_home/blocs/blocs.dart';
import 'package:smart_home/models/app_tab.dart';
import 'package:smart_home/models/grobal_keys.dart';
import 'package:smart_home/widgets/drawer.dart';
import 'package:smart_home/widgets/tab_selector.dart';

class MyHomePage extends StatelessWidget {
final String title;
final List<Widget> actions;
final Widget body;
final Widget floatingActionButton;
final AppTab activeTab;

const MyHomePage({
@required this.title,
@required this.activeTab,
@required this.body,
this.actions,
this.floatingActionButton,
});

@override
Widget build(BuildContext context) {
return Scaffold(
key: scaffoldKey,
drawer: MyDrawer(),
appBar: AppBar(
title: Text(title),
actions: actions,
),
body: body,
bottomNavigationBar: TabSelector(
activeTab: activeTab,
onTabSelected: (tab) =>
BlocProvider.of<TabBloc>(context).add(TabChanged(tab)),
),
floatingActionButton: floatingActionButton,
);
}
}

0 comments on commit f9c0f96

Please sign in to comment.