From e531d7ed30eec10aa87b1d0ac5267b8de56050e0 Mon Sep 17 00:00:00 2001 From: Weller <1812208341@qq.com> Date: Fri, 25 Sep 2020 20:27:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0tab=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/lib/main.dart | 15 ++- lib/pages/home_page/home_page.dart | 110 +++++++++--------- lib/pages/home_page/home_page_bloc.dart | 35 ------ lib/pages/launch_page/launch_page.dart | 36 ++++++ lib/pages/main_page/main_page.dart | 24 ++-- lib/pages/main_page/main_page_bloc.dart | 27 ++--- lib/register.dart | 17 ++- .../home_quick_entry/home_quick_entry_sk.dart | 11 +- 8 files changed, 133 insertions(+), 142 deletions(-) delete mode 100644 lib/pages/home_page/home_page_bloc.dart create mode 100644 lib/pages/launch_page/launch_page.dart diff --git a/example/lib/main.dart b/example/lib/main.dart index 9e5b085..46e4d29 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,10 +1,11 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:zhiying_base_widget/pages/home_page/home_page.dart'; +import 'package:flutter_alibc/flutter_alibc.dart'; import 'package:provider/provider.dart'; +import 'package:zhiying_base_widget/pages/home_page/home_page.dart'; import 'package:zhiying_base_widget/register.dart'; +import 'package:zhiying_comm/pages/launch_page/launch_page.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; -import 'package:flutter_alibc/flutter_alibc.dart'; void main() { FlutterError.onError = (FlutterErrorDetails details) { @@ -26,11 +27,7 @@ class _MyAppState extends State { child: Text( "走神了~\n${error.exceptionAsString()}", style: - Theme - .of(context) - .textTheme - .title - .copyWith(color: Colors.redAccent), + Theme.of(context).textTheme.title.copyWith(color: Colors.redAccent), ), ); } @@ -55,7 +52,9 @@ class _MyAppState extends State { ChangeNotifierProvider.value(value: UserInfoNotifier()), ], child: MaterialApp( - home: HomePage(), + home: LaunchPage( + builder: (context) => HomePage(), + ), ), ); } diff --git a/lib/pages/home_page/home_page.dart b/lib/pages/home_page/home_page.dart index afbea4d..09bfad1 100644 --- a/lib/pages/home_page/home_page.dart +++ b/lib/pages/home_page/home_page.dart @@ -1,12 +1,11 @@ +import 'dart:convert' as convert; + +import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; -import 'package:cached_network_image/cached_network_image.dart'; - -import 'package:zhiying_base_widget/pages/home_page/home_page_bloc.dart'; - +import 'package:zhiying_comm/models/base/base_tab_model.dart'; +import 'package:zhiying_comm/util/image_util.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; -import 'package:zhiying_comm/util/base_bloc.dart'; - class HomePage extends StatefulWidget { HomePage({Key key}) : super(key: key); @@ -16,65 +15,48 @@ class HomePage extends StatefulWidget { } class _HomePageState extends State { - @override - Widget build(BuildContext context) { - print('home_page build'); - return BlocProvider( - bloc: HomePageBloc(), - child: _HomePageContainer(), - ); - } -} - -class _HomePageContainer extends StatefulWidget { - _HomePageContainer({Key key}) : super(key: key); - - @override - _HomePageContainerState createState() => _HomePageContainerState(); -} - -class _HomePageContainerState extends State<_HomePageContainer> { - HomePageBloc _tabBloc; - int _currentIndex = 0; + List> _data = List(); @override void initState() { - _tabBloc = BlocProvider.of(context); - _tabBloc.refresh(); + String data = BaseSettingModel.setting.tab['data']; + try { + List list = convert.jsonDecode(data); + _data = list.map((item) { + return Map.from(item); + }).toList(); + Logger.debug(_data); + } catch (error) { + Logger.error(error); + } + super.initState(); } @override Widget build(BuildContext context) { print('home_page build'); - // 屏幕适配初始化 - ScreenUtil.init(context, width: 750, height: 1334); - return StreamBuilder>>( - stream: _tabBloc.outData, - builder: (BuildContext context, AsyncSnapshot snapshot) { - List> tabs = snapshot.data; - if (tabs == null || tabs.length == 0) { - return Scaffold(); - } + List> tabs = _data; + if (tabs == null || tabs.length == 0) { + return Scaffold(); + } - List contentWidgets = tabs.map((item) { - WidgetModel model = WidgetModel.fromJson(item); - return PageFactory.create(model.modName, item); - }).toList(); - if (_currentIndex >= contentWidgets.length) { - _currentIndex = 0; - } + List contentWidgets = tabs.map((item) { + BaseTabModel model = BaseTabModel.fromJson(item); + return PageFactory.create(model.skipIdentifier, item); + }).toList(); + if (_currentIndex >= contentWidgets.length) { + _currentIndex = 0; + } - return Scaffold( - body: IndexedStack( - index: _currentIndex, - children: contentWidgets, - ), - //底部导航栏 - bottomNavigationBar: createBottomNavigationBar(tabs), - ); - }, + return Scaffold( + body: IndexedStack( + index: _currentIndex, + children: contentWidgets, + ), + //底部导航栏 + bottomNavigationBar: createBottomNavigationBar(tabs), ); } @@ -82,28 +64,40 @@ class _HomePageContainerState extends State<_HomePageContainer> { List items = List(); for (int i = 0; i < tabs.length; i++) { - WidgetModel model = WidgetModel.fromJson(tabs[i]); + BaseTabModel model = BaseTabModel.fromJson(tabs[i]); + String icon = ImageUtil.getUrl(model.icon); + String selectedIcon = ImageUtil.getUrl(model.chooseIcon ?? model.icon); + String textColor = model.fontColor; + String chooseColor = model.chooseColor ?? textColor; items.add(BottomNavigationBarItem( icon: Container( width: 24, height: 24, margin: EdgeInsets.only(bottom: 4), child: CachedNetworkImage( - imageUrl: model.icon, + imageUrl: _currentIndex == i ? selectedIcon : icon, fit: BoxFit.fitWidth, ), ), title: Text( - model.title, - style: TextStyle(fontSize: 11), + model.name, + style: TextStyle( + fontSize: 11, + color: HexColor.fromHex( + _currentIndex == i ? chooseColor : textColor)), ))); } if (items.length < 2) { return Container(); } + String bgColor = '#ffffff'; + if (tabs.first != null) { + BaseTabModel model = BaseTabModel.fromJson(tabs.first); + bgColor = model.bgColor ?? bgColor; + } return BottomNavigationBar( - backgroundColor: Colors.white, + backgroundColor: HexColor.fromHex(bgColor), type: BottomNavigationBarType.fixed, selectedFontSize: 11, unselectedFontSize: 11, diff --git a/lib/pages/home_page/home_page_bloc.dart b/lib/pages/home_page/home_page_bloc.dart deleted file mode 100644 index 4f2d133..0000000 --- a/lib/pages/home_page/home_page_bloc.dart +++ /dev/null @@ -1,35 +0,0 @@ -import 'dart:async'; - -import 'package:zhiying_comm/util/base_bloc.dart'; -import 'package:zhiying_comm/zhiying_comm.dart'; - -class HomePageBloc extends BlocBase { - List> _tabs = List(); - - StreamController>> _tabController = - StreamController>>(); - - Stream>> get outData => _tabController.stream; - - @override - void dispose() { - _tabController.close(); - _tabController = null; - } - - void refresh() { - NetUtil.request('/api/v1/config.json', onCache: (data) { - _refresh(data); - }, onSuccess: (data) { - _refresh(data); - }); - } - - void _refresh(dynamic data) { - List list = data; - _tabs = list.map((item) { - return Map.from(item); - }).toList(); - _tabController.add(_tabs); - } -} diff --git a/lib/pages/launch_page/launch_page.dart b/lib/pages/launch_page/launch_page.dart new file mode 100644 index 0000000..62a7640 --- /dev/null +++ b/lib/pages/launch_page/launch_page.dart @@ -0,0 +1,36 @@ +import 'package:flutter/material.dart'; +import 'package:zhiying_base_widget/pages/home_page/home_page.dart'; +import 'package:zhiying_comm/util/application.dart'; + +// 启动页,页面初始化等操作~跟原生启动页保持一致,防止白屏问题 +class LaunchPage extends StatefulWidget { + final WidgetBuilder builder; + + const LaunchPage({Key key, this.builder}) : super(key: key); + + @override + _LaunchPageState createState() => _LaunchPageState(); +} + +class _LaunchPageState extends State { + @override + void initState() { + Application.init().then((_) { + if (widget.builder != null) { + Navigator.of(context) + .pushReplacement(MaterialPageRoute(builder: widget.builder)); + } else { + Navigator.of(context).pushReplacement( + MaterialPageRoute(builder: (context) => HomePage())); + } + }); + super.initState(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + body: Center(child: Text('这个是启动页,正在初始化...')), + ); + } +} diff --git a/lib/pages/main_page/main_page.dart b/lib/pages/main_page/main_page.dart index 392de7a..40457a5 100644 --- a/lib/pages/main_page/main_page.dart +++ b/lib/pages/main_page/main_page.dart @@ -5,10 +5,10 @@ import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:zhiying_base_widget/pages/main_page/main_page_bloc.dart'; import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_bg_notifier.dart'; import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_notifier.dart'; +import 'package:zhiying_base_widget/widgets/home/home_auth/home_auth.dart'; import 'package:zhiying_base_widget/widgets/others/mine_header_bg_widget.dart'; import 'package:zhiying_comm/util/base_bloc.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; -import 'package:zhiying_base_widget/widgets/home/home_auth/home_auth.dart'; class MainPage extends StatefulWidget { final Map data; @@ -26,15 +26,14 @@ class _MainPageState extends State { @override Widget build(BuildContext context) { return MultiProvider( - providers: [ - ChangeNotifierProvider.value(value: MainPageNotifier()), - ChangeNotifierProvider.value(value: MainPageBgNotifier()), - ], - child: BlocProvider( - bloc: MainPageBloc(), - child: _MainPageContainer(widget.data), - ), - + providers: [ + ChangeNotifierProvider.value(value: MainPageNotifier()), + ChangeNotifierProvider.value(value: MainPageBgNotifier()), + ], + child: BlocProvider( + bloc: MainPageBloc(), + child: _MainPageContainer(widget.data), + ), ); } } @@ -72,8 +71,8 @@ class _MainPageContainerState extends State<_MainPageContainer> { @override void initState() { _bloc = BlocProvider.of(context); - if (widget.data.containsKey('mod_id')) { - _bloc.loadData(widget.data['mod_id']); + if (widget.data.containsKey('skip_identifier')) { + _bloc.loadData(widget.data['skip_identifier']); } _controller.addListener(() { @@ -95,7 +94,6 @@ class _MainPageContainerState extends State<_MainPageContainer> { @override Widget build(BuildContext context) { return Scaffold( - backgroundColor: Color(0xfff9f9f9), floatingActionButton: _floatWidget, floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, diff --git a/lib/pages/main_page/main_page_bloc.dart b/lib/pages/main_page/main_page_bloc.dart index 21f14c9..1d3f57a 100644 --- a/lib/pages/main_page/main_page_bloc.dart +++ b/lib/pages/main_page/main_page_bloc.dart @@ -17,27 +17,22 @@ class MainPageBloc extends BlocBase { _mainController = null; } - void loadData(int id) { - NetUtil.request('/api/v1/mod', - method: NetMethod.POST, - params: Map.from({ - 'ids': [id] - }), onCache: (data) { - _loadData(id, data); + void loadData(String skipIdentifier) { + NetUtil.request('/api/v1/mod/${skipIdentifier}', method: NetMethod.GET, + onCache: (data) { + _loadData(data); }, onSuccess: (data) { - _loadData(id, data); + _loadData(data); }); } - void _loadData(int id, dynamic data) { - String key = id.toString(); + void _loadData(dynamic data) { Map json = Map.from(data); - if (json.containsKey(key)) { - List list = json[key]; - _pageData = list.map((item) { - return Map.from(item); - }).toList(); - } + + List list = json['mod_list']; + _pageData = list.map((item) { + return Map.from(item); + }).toList(); _mainController.add(_pageData); } diff --git a/lib/register.dart b/lib/register.dart index ef3fe19..658d7cd 100644 --- a/lib/register.dart +++ b/lib/register.dart @@ -1,5 +1,5 @@ import 'package:zhiying_base_widget/pages/goods_details_page/goods_details_page.dart'; -import 'package:zhiying_base_widget/pages/home_page/home_page.dart'; +import 'package:zhiying_base_widget/pages/launch_page/launch_page.dart'; import 'package:zhiying_base_widget/pages/main_page/main_page.dart'; import 'package:zhiying_base_widget/pages/mine_detail_page/mine_detail_page.dart'; import 'package:zhiying_base_widget/pages/orders_page/orders_page.dart'; @@ -47,14 +47,21 @@ class BaseWidgetRegister { static void init() { BaseWidgetRegister.registPage(); BaseWidgetRegister.registWidgets(); + BaseWidgetRegister.registInit(); + } + + static void registInit() { + Application.addMethod(() { + return BaseSettingModel.init(); + }); } // 注册页面 static void registPage() { - PageFactory.regist('homePage', (model) => HomePage()); - PageFactory.regist('index', (model) => MainPage(model)); - PageFactory.regist('profile', (model) => MainPage(model)); - PageFactory.regist('category', (model) => WalletPage()); + PageFactory.regist('homePage', (model) => LaunchPage()); + PageFactory.regist('pub.flutter.index', (model) => MainPage(model)); + PageFactory.regist('pub.flutter.profile', (model) => MainPage(model)); + PageFactory.regist('pub.flutter.my_wallet', (model) => WalletPage()); PageFactory.regist('goods_details', (model) => GoodsDetailsPage(model)); PageFactory.regist('sreach', (model) => SreachPage()); PageFactory.regist('sreach_result', (model) => SreachResultPage()); diff --git a/lib/widgets/home/home_quick_entry/home_quick_entry_sk.dart b/lib/widgets/home/home_quick_entry/home_quick_entry_sk.dart index 4f3e285..6e9bc32 100644 --- a/lib/widgets/home/home_quick_entry/home_quick_entry_sk.dart +++ b/lib/widgets/home/home_quick_entry/home_quick_entry_sk.dart @@ -1,6 +1,5 @@ import 'package:flutter/material.dart'; import 'package:shimmer/shimmer.dart'; -import 'package:zhiying_comm/zhiying_comm.dart'; class HomeQuickEntrySkeleton extends StatelessWidget { @override @@ -43,13 +42,11 @@ class HomeQuickEntrySkeletonItem extends StatelessWidget { Container( decoration: BoxDecoration( color: Colors.white, - borderRadius: BorderRadius.circular(40.h) - ), - height: 80.h, - width: 80.h, - + borderRadius: BorderRadius.circular(20)), + height: 40, + width: 40, ), - SizedBox(height: 10.h), + SizedBox(height: 5), // Container(height: 10.h, width: 40.w, color: Colors.white,), ],