Kaynağa Gözat

添加商品列表组件数据加载

tags/0.0.1
Weller 4 yıl önce
ebeveyn
işleme
67b7d2b417
13 değiştirilmiş dosya ile 284 ekleme ve 157 silme
  1. +13
    -1
      .dart_tool/package_config.json
  2. +1
    -1
      example/lib/main.dart
  3. +0
    -1
      lib/pages/home_page/home_page.dart
  4. +0
    -63
      lib/pages/main_page.dart
  5. +100
    -0
      lib/pages/main_page/main_page.dart
  6. +15
    -0
      lib/pages/main_page/main_page_notifier.dart
  7. +3
    -3
      lib/register.dart
  8. +0
    -16
      lib/widgets/goods_list/goods_list.dart
  9. +0
    -72
      lib/widgets/goods_list/goods_list_creater.dart
  10. +48
    -0
      lib/widgets/home_goods/home_goods.dart
  11. +103
    -0
      lib/widgets/home_goods/home_goods_creater.dart
  12. +0
    -0
     
  13. +1
    -0
      pubspec.yaml

+ 13
- 1
.dart_tool/package_config.json Dosyayı Görüntüle

@@ -169,6 +169,12 @@
"packageUri": "lib/",
"languageVersion": "1.12"
},
{
"name": "nested",
"rootUri": "file:///Users/fnuser/.pub-cache/hosted/pub.flutter-io.cn/nested-0.0.4",
"packageUri": "lib/",
"languageVersion": "2.2"
},
{
"name": "package_info",
"rootUri": "file:///Users/fnuser/.pub-cache/hosted/pub.flutter-io.cn/package_info-0.4.3",
@@ -235,6 +241,12 @@
"packageUri": "lib/",
"languageVersion": "2.0"
},
{
"name": "provider",
"rootUri": "file:///Users/fnuser/.pub-cache/hosted/pub.flutter-io.cn/provider-4.0.5+1",
"packageUri": "lib/",
"languageVersion": "2.2"
},
{
"name": "quiver",
"rootUri": "file:///Users/fnuser/.pub-cache/hosted/pub.flutter-io.cn/quiver-2.0.5",
@@ -362,7 +374,7 @@
"languageVersion": "2.1"
}
],
"generated": "2020-09-03T03:05:08.224004Z",
"generated": "2020-09-03T11:25:12.333713Z",
"generator": "pub",
"generatorVersion": "2.7.2"
}

+ 1
- 1
example/lib/main.dart Dosyayı Görüntüle

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

import 'package:zhiying_base_widget/pages/home_page.dart';
import 'package:zhiying_base_widget/pages/home_page/home_page.dart';
import 'package:zhiying_base_widget/register.dart';

void main() => runApp(MyApp());


lib/pages/home_page.dart → lib/pages/home_page/home_page.dart Dosyayı Görüntüle

@@ -1,7 +1,6 @@
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/main_page.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class HomePage extends StatefulWidget {

+ 0
- 63
lib/pages/main_page.dart Dosyayı Görüntüle

@@ -1,63 +0,0 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class MainPage extends StatefulWidget {
MainPage({Key key}) : super(key: key);

@override
_MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
WidgetType _type = WidgetType.skeleton;

@override
void initState() {
_reload();
super.initState();
}

void _reload() {
setState(() {
_type = WidgetType.skeleton;
});
Future.delayed(Duration(seconds: 3), () {
setState(() {
_type = WidgetType.normal;
});
});
}

List<Widget> _createContent() {
List<Widget> list = List();
list.add(SliverToBoxAdapter(
child: RaisedButton(
onPressed: _reload,
child: Text('重置'),
),
));
list.addAll(
WidgetFactory.create('home_banner', type: _type, isSliver: true));
list.addAll(
WidgetFactory.create('home_goods', type: _type, isSliver: true));
list.addAll(
WidgetFactory.create('home_goods', type: _type, isSliver: true));
list.addAll(
WidgetFactory.create('home_goods', type: _type, isSliver: true));
return list;
}

@override
Widget build(BuildContext context) {
print('build');
return Scaffold(
backgroundColor: Color(0xfff9f9f9),
body: SafeArea(
child: Container(
child: CustomScrollView(
slivers: _createContent(),
))),
);
}
}

+ 100
- 0
lib/pages/main_page/main_page.dart Dosyayı Görüntüle

@@ -0,0 +1,100 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/pages/main_page/main_page_notifier.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:provider/provider.dart';

class MainPage extends StatefulWidget {
MainPage({Key key}) : super(key: key);

@override
_MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xfff9f9f9),
body: ChangeNotifierProvider(
create: (context) => MainPageNotifier(),
child: MainPageContainer(),
),
);
}
}

class MainPageContainer extends StatefulWidget {
MainPageContainer({Key key}) : super(key: key);

@override
_MainPageContainerState createState() => _MainPageContainerState();
}

class _MainPageContainerState extends State<MainPageContainer> {
WidgetType _type = WidgetType.normal;
bool _isEnded = false;
ScrollController _controller = ScrollController();

@override
void initState() {
_reload();
_controller.addListener(() {
// print('${_controller.offset} ${_controller.position.maxScrollExtent}');
if (_controller.offset >= _controller.position.maxScrollExtent &&
!_isEnded) {
// 滑动到底部
_isEnded = true;
Provider.of<MainPageNotifier>(context, listen: false).loadMore();
} else if (_controller.offset < _controller.position.maxScrollExtent &&
_isEnded) {
_isEnded = false;
Provider.of<MainPageNotifier>(context, listen: false).reset();
}
});
super.initState();
}

void _reload() {
setState(() {
_type = WidgetType.skeleton;
});
Future.delayed(Duration(seconds: 3), () {
setState(() {
_type = WidgetType.normal;
});
});
}

List<Widget> _createContent(BuildContext context) {
List<Widget> list = List();
list.add(SliverToBoxAdapter(
child: RaisedButton(
onPressed: () {
Provider.of<MainPageNotifier>(context, listen: false).loadMore();
_reload();
},
child: Text('重置'),
),
));
list.addAll(
WidgetFactory.create('home_banner', type: _type, isSliver: true));
list.addAll(
WidgetFactory.create('home_goods', type: _type, isSliver: true));
// list.addAll(
// WidgetFactory.create('home_goods', type: _type, isSliver: true));
// list.addAll(
// WidgetFactory.create('home_goods', type: _type, isSliver: true));
return list;
}

@override
Widget build(BuildContext context) {
return SafeArea(
child: CustomScrollView(
controller: _controller,
slivers: _createContent(context),
),
);
}
}

+ 15
- 0
lib/pages/main_page/main_page_notifier.dart Dosyayı Görüntüle

@@ -0,0 +1,15 @@
import 'package:flutter/material.dart';

class MainPageNotifier with ChangeNotifier {
bool scrollEnd = false;

void loadMore() {
scrollEnd = true;
notifyListeners();
}

void reset() {
scrollEnd = false;
notifyListeners();
}
}

+ 3
- 3
lib/register.dart Dosyayı Görüntüle

@@ -1,8 +1,8 @@
import 'package:zhiying_base_widget/pages/main_page.dart';
import 'package:zhiying_base_widget/widgets/goods_list/goods_list_creater.dart';
import 'package:zhiying_base_widget/pages/main_page/main_page.dart';
import 'package:zhiying_base_widget/widgets/home_goods/home_goods_creater.dart';
import 'package:zhiying_base_widget/widgets/home_banner/home_banner_creater.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:zhiying_base_widget/pages/home_page.dart';
import 'package:zhiying_base_widget/pages/home_page/home_page.dart';

class BaseWidgetRegister {
/// 初始化方法


+ 0
- 16
lib/widgets/goods_list/goods_list.dart Dosyayı Görüntüle

@@ -1,16 +0,0 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class GoodsList extends StatefulWidget {
GoodsList({Key key}) : super(key: key);

@override
_GoodsListState createState() => _GoodsListState();
}

class _GoodsListState extends State<GoodsList> {
@override
Widget build(BuildContext context) {
return Container();
}
}

+ 0
- 72
lib/widgets/goods_list/goods_list_creater.dart Dosyayı Görüntüle

@@ -1,72 +0,0 @@
import 'package:flutter/material.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class GoodsListCreater extends WidgetCreater {
@override
List<Widget> createWidgets(Map<String, dynamic> model) {
return [
_createHeader(),
_createBody(),
];
}

@override
List<Widget> createSkeleton(Map<String, dynamic> model) {
return [];
}

Widget _createHeader() {
return SliverPersistentHeader(
pinned: true,
floating: false,
delegate: MySliverPersistentHeaderDelegate(),
);
}

Widget _createBody() {
// return SliverGrid(
// gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
// crossAxisCount: 3, crossAxisSpacing: 5, mainAxisSpacing: 3),
// delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
// return Container(
// color: Colors.primaries[index % Colors.primaries.length],
// );
// }, childCount: 20),
// );

return SliverList(
delegate: SliverChildListDelegate(List<Widget>.generate(30, (index) {
return Container(
width: double.infinity,
height: 60,
color: Colors.primaries[index % Colors.primaries.length],
);
})));
}

@override
bool isSliverChild() {
return true;
}
}

class MySliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return Container(
color: Colors.red,
alignment: Alignment.center,
child: Text('222222', style: TextStyle(color: Colors.white)));
}

@override
double get maxExtent => 100.0;

@override
double get minExtent => 100.0;

@override
bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) =>
false; // 如果内容需要更新,设置为true
}

+ 48
- 0
lib/widgets/home_goods/home_goods.dart Dosyayı Görüntüle

@@ -0,0 +1,48 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:zhiying_base_widget/pages/main_page/main_page_notifier.dart';

class HomeGoods extends StatefulWidget {
HomeGoods({Key key}) : super(key: key);

@override
_HomeGoodsState createState() => _HomeGoodsState();
}

class _HomeGoodsState extends State<HomeGoods> {
@override
void initState() {
super.initState();
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
print('didChangeDependencies');
bool isNeedLoadMore = Provider.of<MainPageNotifier>(context).scrollEnd;
if (isNeedLoadMore) {
print('HomeGoods loadmore...');
}
}

@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: 20,
itemBuilder: (context, index) {
return Container(
width: double.infinity,
height: 70,
color: Colors.primaries[index % Colors.primaries.length],
child: Text(
Provider.of<MainPageNotifier>(context).scrollEnd.toString()),
);
}),
);
}
}

+ 103
- 0
lib/widgets/home_goods/home_goods_creater.dart Dosyayı Görüntüle

@@ -0,0 +1,103 @@
import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/home_goods/home_goods.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class GoodsListCreater extends WidgetCreater {
@override
List<Widget> createWidgets(Map<String, dynamic> model) {
TabController controller =
TabController(initialIndex: 0, length: 6, vsync: ScrollableState());
return [
_createHeader(controller),
_createBody(controller),
];
}

@override
List<Widget> createSkeleton(Map<String, dynamic> model) {
return [];
}

Widget _createHeader(TabController controller) {
return SliverPersistentHeader(
pinned: true,
floating: false,
delegate: GoodsHeaderDelegate(controller),
);
}

Widget _createBody(TabController controller) {
// return SliverToBoxAdapter(
// child: Container(
// width: double.infinity,
// height: 200,
// // child: Expanded(child: Container()),
// child: TabBarView(
// controller: controller,
// children: <Widget>[
// Icon(Icons.directions_bike, size: 128.0, color: Colors.black12),
// ListView.builder(
// itemCount: 20,
// itemBuilder: (context, index) {
// return Container(
// width: double.infinity,
// height: 70,
// color: Colors.primaries[index % Colors.primaries.length],
// );
// }),
// Icon(Icons.directions_bike, size: 128.0, color: Colors.black12),
// Icon(Icons.local_florist, size: 128.0, color: Colors.black12),
// Icon(Icons.change_history, size: 128.0, color: Colors.black12),
// Icon(Icons.directions_bike, size: 128.0, color: Colors.black12),
// ],
// ),
// ),
// );
return SliverToBoxAdapter(
child: HomeGoods(),
);
}

@override
bool isSliverChild() {
return true;
}
}

class GoodsHeaderDelegate extends SliverPersistentHeaderDelegate {
final TabController controller;

GoodsHeaderDelegate(this.controller);

@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
// print(shrinkOffset.toString());
return Container(
color: Colors.red,
alignment: Alignment.center,
child: TabBar(
controller: controller,
isScrollable: true,
tabs: <Widget>[
Tab(icon: Icon(Icons.local_florist)),
Tab(icon: Icon(Icons.change_history)),
Tab(icon: Icon(Icons.directions_bike)),
Tab(icon: Icon(Icons.local_florist)),
Tab(icon: Icon(Icons.change_history)),
Tab(icon: Icon(Icons.directions_bike)),
],
),
);
}

@override
double get maxExtent => 60.0;

@override
double get minExtent => 60.0;

@override
bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) =>
false; // 如果内容需要更新,设置为true
}


+ 1
- 0
pubspec.yaml Dosyayı Görüntüle

@@ -13,6 +13,7 @@ dependencies:

shimmer: ^1.1.1
flutter_swiper : ^1.1.6
provider: ^4.0.0

dev_dependencies:
flutter_test:


Yükleniyor…
İptal
Kaydet