|
- import 'dart:convert' as convert;
- import 'dart:io';
-
- import 'package:cached_network_image/cached_network_image.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:permission_handler/permission_handler.dart';
- import 'package:provider/provider.dart';
- import 'package:zhiying_base_widget/dialog/global_dialog/notification_setting_dialog/notification_setting_dialog.dart';
- import 'package:zhiying_base_widget/dialog/global_dialog/policy_dialog/policy_dialog.dart';
- import 'package:zhiying_base_widget/utils/contants.dart';
- import 'package:zhiying_comm/models/base/base_tab_model.dart';
- import 'package:zhiying_comm/util/image_util.dart';
- import 'package:zhiying_comm/util/shared_prefe_util.dart';
- import 'package:zhiying_comm/util/update/app_update_util.dart';
- import 'package:zhiying_comm/zhiying_comm.dart';
-
- class HomePage extends StatefulWidget {
- HomePage({Key key}) : super(key: key);
-
- @override
- _HomePageState createState() => _HomePageState();
- }
-
- class _HomePageState extends State<HomePage> {
- int _currentIndex = 0;
- List<Map<String, dynamic>> _data = List();
-
- @override
- void initState() {
- String data = BaseSettingModel.setting.tab['data'];
- try {
- List list = convert.jsonDecode(data);
- _data = list.map((item) {
- return Map<String, dynamic>.from(item);
- }).toList();
- Logger.debug(_data);
- } catch (error) {
- Logger.error(error);
- }
- Constants.isShowIntellectDialog = false;
- AppUpdateUtil.updateApp(context);
- TaobaoAuth.initAuth(context);
-
- SharedPreferencesUtil.getStringValue(Constants.isShowPolicy).then((value) {
- Logger.debug(value);
- if (value == null || value == '') {
- _showPolicy();
- }
- });
-
- super.initState();
- }
-
- @override
- void didChangeDependencies() {
- super.didChangeDependencies();
- }
-
- @override
- Widget build(BuildContext context) {
- ScreenUtil.init(context, width: 750, height: 1334);
- print('home_page build');
- List<Map<String, dynamic>> tabs = _data;
- if (tabs == null || tabs.length == 0) {
- return Scaffold();
- }
-
- List<Widget> 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),
- );
- }
-
- Widget createBottomNavigationBar(List<Map<String, dynamic>> tabs) {
- List<BottomNavigationBarItem> items = List<BottomNavigationBarItem>();
-
- for (int i = 0; i < tabs.length; 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: _currentIndex == i ? selectedIcon : icon,
- fit: BoxFit.fitWidth,
- ),
- ),
- title: Text(
- 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: HexColor.fromHex(bgColor),
- type: BottomNavigationBarType.fixed,
- selectedFontSize: 11,
- unselectedFontSize: 11,
- currentIndex: _currentIndex,
- elevation: 0,
- onTap: ((index) async {
- BaseTabModel model = BaseTabModel.fromJson(tabs[index]);
- if (await _checkLimit(model)) {
- setState(() {
- _currentIndex = index;
- });
- }
- }),
- //底部导航栏
- items: items);
- }
-
- Future<bool> _checkLimit(BaseTabModel model) async {
- if (model.requiredLogin == '1') {
- UserInfoModel user =
- await Provider.of<UserInfoNotifier>(context, listen: false)
- .getUserInfoModel();
- print(user.toString());
- if (user?.token == null || user.token == '') {
- print('need login...');
- RouterUtil.goLogin(context);
- return false;
- }
- }
- return true;
- }
-
- void _showPolicy() async {
- bool isAccept = await PolicyDialog.show(context);
- if (!isAccept) {
- exit(0);
- }
-
- PermissionStatus status = await Permission.notification.status;
- if (status != PermissionStatus.granted) {
- await showCupertinoDialog(
- context: context, builder: (_) => NotificationSettingDialog());
- }
- }
- }
|