Browse Source

更新tab结构

tags/0.0.1
Weller 4 years ago
parent
commit
e531d7ed30
8 changed files with 133 additions and 142 deletions
  1. +7
    -8
      example/lib/main.dart
  2. +52
    -58
      lib/pages/home_page/home_page.dart
  3. +0
    -35
      lib/pages/home_page/home_page_bloc.dart
  4. +36
    -0
      lib/pages/launch_page/launch_page.dart
  5. +11
    -13
      lib/pages/main_page/main_page.dart
  6. +11
    -16
      lib/pages/main_page/main_page_bloc.dart
  7. +12
    -5
      lib/register.dart
  8. +4
    -7
      lib/widgets/home/home_quick_entry/home_quick_entry_sk.dart

+ 7
- 8
example/lib/main.dart View File

@@ -1,10 +1,11 @@
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.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:provider/provider.dart';
import 'package:zhiying_base_widget/pages/home_page/home_page.dart';
import 'package:zhiying_base_widget/register.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:zhiying_comm/zhiying_comm.dart';
import 'package:flutter_alibc/flutter_alibc.dart';


void main() { void main() {
FlutterError.onError = (FlutterErrorDetails details) { FlutterError.onError = (FlutterErrorDetails details) {
@@ -26,11 +27,7 @@ class _MyAppState extends State<MyApp> {
child: Text( child: Text(
"走神了~\n${error.exceptionAsString()}", "走神了~\n${error.exceptionAsString()}",
style: 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<MyApp> {
ChangeNotifierProvider.value(value: UserInfoNotifier()), ChangeNotifierProvider.value(value: UserInfoNotifier()),
], ],
child: MaterialApp( child: MaterialApp(
home: HomePage(),
home: LaunchPage(
builder: (context) => HomePage(),
),
), ),
); );
} }


+ 52
- 58
lib/pages/home_page/home_page.dart View File

@@ -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/cupertino.dart';
import 'package:flutter/material.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/zhiying_comm.dart';
import 'package:zhiying_comm/util/base_bloc.dart';



class HomePage extends StatefulWidget { class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key); HomePage({Key key}) : super(key: key);
@@ -16,65 +15,48 @@ class HomePage extends StatefulWidget {
} }


class _HomePageState extends State<HomePage> { class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
print('home_page build');
return BlocProvider<HomePageBloc>(
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; int _currentIndex = 0;
List<Map<String, dynamic>> _data = List();


@override @override
void initState() { void initState() {
_tabBloc = BlocProvider.of<HomePageBloc>(context);
_tabBloc.refresh();
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);
}

super.initState(); super.initState();
} }


@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
print('home_page build'); print('home_page build');
// 屏幕适配初始化
ScreenUtil.init(context, width: 750, height: 1334);
return StreamBuilder<List<Map<String, dynamic>>>(
stream: _tabBloc.outData,
builder: (BuildContext context, AsyncSnapshot snapshot) {
List<Map<String, dynamic>> tabs = snapshot.data;
if (tabs == null || tabs.length == 0) {
return Scaffold();
}
List<Map<String, dynamic>> tabs = _data;
if (tabs == null || tabs.length == 0) {
return Scaffold();
}


List<Widget> contentWidgets = tabs.map((item) {
WidgetModel model = WidgetModel.fromJson(item);
return PageFactory.create(model.modName, item);
}).toList();
if (_currentIndex >= contentWidgets.length) {
_currentIndex = 0;
}
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),
);
},
return Scaffold(
body: IndexedStack(
index: _currentIndex,
children: contentWidgets,
),
//底部导航栏
bottomNavigationBar: createBottomNavigationBar(tabs),
); );
} }


@@ -82,28 +64,40 @@ class _HomePageContainerState extends State<_HomePageContainer> {
List<BottomNavigationBarItem> items = List<BottomNavigationBarItem>(); List<BottomNavigationBarItem> items = List<BottomNavigationBarItem>();


for (int i = 0; i < tabs.length; i++) { 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( items.add(BottomNavigationBarItem(
icon: Container( icon: Container(
width: 24, width: 24,
height: 24, height: 24,
margin: EdgeInsets.only(bottom: 4), margin: EdgeInsets.only(bottom: 4),
child: CachedNetworkImage( child: CachedNetworkImage(
imageUrl: model.icon,
imageUrl: _currentIndex == i ? selectedIcon : icon,
fit: BoxFit.fitWidth, fit: BoxFit.fitWidth,
), ),
), ),
title: Text( 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) { if (items.length < 2) {
return Container(); return Container();
} }
String bgColor = '#ffffff';
if (tabs.first != null) {
BaseTabModel model = BaseTabModel.fromJson(tabs.first);
bgColor = model.bgColor ?? bgColor;
}
return BottomNavigationBar( return BottomNavigationBar(
backgroundColor: Colors.white,
backgroundColor: HexColor.fromHex(bgColor),
type: BottomNavigationBarType.fixed, type: BottomNavigationBarType.fixed,
selectedFontSize: 11, selectedFontSize: 11,
unselectedFontSize: 11, unselectedFontSize: 11,


+ 0
- 35
lib/pages/home_page/home_page_bloc.dart View File

@@ -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<Map<String, dynamic>> _tabs = List();

StreamController<List<Map<String, dynamic>>> _tabController =
StreamController<List<Map<String, dynamic>>>();

Stream<List<Map<String, dynamic>>> 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<String, dynamic>.from(item);
}).toList();
_tabController.add(_tabs);
}
}

+ 36
- 0
lib/pages/launch_page/launch_page.dart View File

@@ -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<LaunchPage> {
@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('这个是启动页,正在初始化...')),
);
}
}

+ 11
- 13
lib/pages/main_page/main_page.dart View File

@@ -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/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_bg_notifier.dart';
import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_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_base_widget/widgets/others/mine_header_bg_widget.dart';
import 'package:zhiying_comm/util/base_bloc.dart'; import 'package:zhiying_comm/util/base_bloc.dart';
import 'package:zhiying_comm/zhiying_comm.dart'; import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:zhiying_base_widget/widgets/home/home_auth/home_auth.dart';


class MainPage extends StatefulWidget { class MainPage extends StatefulWidget {
final Map<String, dynamic> data; final Map<String, dynamic> data;
@@ -26,15 +26,14 @@ class _MainPageState extends State<MainPage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MultiProvider( return MultiProvider(
providers: [
ChangeNotifierProvider.value(value: MainPageNotifier()),
ChangeNotifierProvider.value(value: MainPageBgNotifier()),
],
child: BlocProvider<MainPageBloc>(
bloc: MainPageBloc(),
child: _MainPageContainer(widget.data),
),

providers: [
ChangeNotifierProvider.value(value: MainPageNotifier()),
ChangeNotifierProvider.value(value: MainPageBgNotifier()),
],
child: BlocProvider<MainPageBloc>(
bloc: MainPageBloc(),
child: _MainPageContainer(widget.data),
),
); );
} }
} }
@@ -72,8 +71,8 @@ class _MainPageContainerState extends State<_MainPageContainer> {
@override @override
void initState() { void initState() {
_bloc = BlocProvider.of<MainPageBloc>(context); _bloc = BlocProvider.of<MainPageBloc>(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(() { _controller.addListener(() {
@@ -95,7 +94,6 @@ class _MainPageContainerState extends State<_MainPageContainer> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(

backgroundColor: Color(0xfff9f9f9), backgroundColor: Color(0xfff9f9f9),
floatingActionButton: _floatWidget, floatingActionButton: _floatWidget,
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,


+ 11
- 16
lib/pages/main_page/main_page_bloc.dart View File

@@ -17,27 +17,22 @@ class MainPageBloc extends BlocBase {
_mainController = null; _mainController = null;
} }


void loadData(int id) {
NetUtil.request('/api/v1/mod',
method: NetMethod.POST,
params: Map<String, dynamic>.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) { }, onSuccess: (data) {
_loadData(id, data);
_loadData(data);
}); });
} }


void _loadData(int id, dynamic data) {
String key = id.toString();
void _loadData(dynamic data) {
Map<String, dynamic> json = Map<String, dynamic>.from(data); Map<String, dynamic> json = Map<String, dynamic>.from(data);
if (json.containsKey(key)) {
List<dynamic> list = json[key];
_pageData = list.map((item) {
return Map<String, dynamic>.from(item);
}).toList();
}

List<dynamic> list = json['mod_list'];
_pageData = list.map((item) {
return Map<String, dynamic>.from(item);
}).toList();


_mainController.add(_pageData); _mainController.add(_pageData);
} }


+ 12
- 5
lib/register.dart View File

@@ -1,5 +1,5 @@
import 'package:zhiying_base_widget/pages/goods_details_page/goods_details_page.dart'; 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/main_page/main_page.dart';
import 'package:zhiying_base_widget/pages/mine_detail_page/mine_detail_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'; import 'package:zhiying_base_widget/pages/orders_page/orders_page.dart';
@@ -47,14 +47,21 @@ class BaseWidgetRegister {
static void init() { static void init() {
BaseWidgetRegister.registPage(); BaseWidgetRegister.registPage();
BaseWidgetRegister.registWidgets(); BaseWidgetRegister.registWidgets();
BaseWidgetRegister.registInit();
}

static void registInit() {
Application.addMethod(() {
return BaseSettingModel.init();
});
} }


// 注册页面 // 注册页面
static void registPage() { 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('goods_details', (model) => GoodsDetailsPage(model));
PageFactory.regist('sreach', (model) => SreachPage()); PageFactory.regist('sreach', (model) => SreachPage());
PageFactory.regist('sreach_result', (model) => SreachResultPage()); PageFactory.regist('sreach_result', (model) => SreachResultPage());


+ 4
- 7
lib/widgets/home/home_quick_entry/home_quick_entry_sk.dart View File

@@ -1,6 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:shimmer/shimmer.dart'; import 'package:shimmer/shimmer.dart';
import 'package:zhiying_comm/zhiying_comm.dart';


class HomeQuickEntrySkeleton extends StatelessWidget { class HomeQuickEntrySkeleton extends StatelessWidget {
@override @override
@@ -43,13 +42,11 @@ class HomeQuickEntrySkeletonItem extends StatelessWidget {
Container( Container(
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.white, 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,), // Container(height: 10.h, width: 40.w, color: Colors.white,),
], ],


Loading…
Cancel
Save