From 67b7d2b41796b342ab2a551621162909efcd1b6c Mon Sep 17 00:00:00 2001 From: Weller <1812208341@qq.com> Date: Fri, 4 Sep 2020 09:25:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=95=86=E5=93=81=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E7=BB=84=E4=BB=B6=E6=95=B0=E6=8D=AE=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dart_tool/package_config.json | 14 ++- example/lib/main.dart | 2 +- lib/pages/{ => home_page}/home_page.dart | 1 - lib/pages/main_page.dart | 63 ----------- lib/pages/main_page/main_page.dart | 100 +++++++++++++++++ lib/pages/main_page/main_page_notifier.dart | 15 +++ lib/register.dart | 6 +- lib/widgets/goods_list/goods_list.dart | 16 --- .../goods_list/goods_list_creater.dart | 72 ------------ lib/widgets/home_goods/home_goods.dart | 48 ++++++++ .../home_goods/home_goods_creater.dart | 103 ++++++++++++++++++ lib/widgets/home_goods/home_goods_header.dart | 0 pubspec.yaml | 1 + 13 files changed, 284 insertions(+), 157 deletions(-) rename lib/pages/{ => home_page}/home_page.dart (97%) delete mode 100644 lib/pages/main_page.dart create mode 100644 lib/pages/main_page/main_page.dart create mode 100644 lib/pages/main_page/main_page_notifier.dart delete mode 100644 lib/widgets/goods_list/goods_list.dart delete mode 100644 lib/widgets/goods_list/goods_list_creater.dart create mode 100644 lib/widgets/home_goods/home_goods.dart create mode 100644 lib/widgets/home_goods/home_goods_creater.dart create mode 100644 lib/widgets/home_goods/home_goods_header.dart diff --git a/.dart_tool/package_config.json b/.dart_tool/package_config.json index c76c4f9..641e6e9 100644 --- a/.dart_tool/package_config.json +++ b/.dart_tool/package_config.json @@ -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" } diff --git a/example/lib/main.dart b/example/lib/main.dart index 811c643..a14e048 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -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()); diff --git a/lib/pages/home_page.dart b/lib/pages/home_page/home_page.dart similarity index 97% rename from lib/pages/home_page.dart rename to lib/pages/home_page/home_page.dart index d0f2673..2faae69 100644 --- a/lib/pages/home_page.dart +++ b/lib/pages/home_page/home_page.dart @@ -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 { diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart deleted file mode 100644 index 0db5b2e..0000000 --- a/lib/pages/main_page.dart +++ /dev/null @@ -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 { - 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 _createContent() { - List 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(), - ))), - ); - } -} diff --git a/lib/pages/main_page/main_page.dart b/lib/pages/main_page/main_page.dart new file mode 100644 index 0000000..e7c9018 --- /dev/null +++ b/lib/pages/main_page/main_page.dart @@ -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 { + @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 { + 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(context, listen: false).loadMore(); + } else if (_controller.offset < _controller.position.maxScrollExtent && + _isEnded) { + _isEnded = false; + Provider.of(context, listen: false).reset(); + } + }); + super.initState(); + } + + void _reload() { + setState(() { + _type = WidgetType.skeleton; + }); + Future.delayed(Duration(seconds: 3), () { + setState(() { + _type = WidgetType.normal; + }); + }); + } + + List _createContent(BuildContext context) { + List list = List(); + list.add(SliverToBoxAdapter( + child: RaisedButton( + onPressed: () { + Provider.of(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), + ), + ); + } +} diff --git a/lib/pages/main_page/main_page_notifier.dart b/lib/pages/main_page/main_page_notifier.dart new file mode 100644 index 0000000..dc9d2ae --- /dev/null +++ b/lib/pages/main_page/main_page_notifier.dart @@ -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(); + } +} diff --git a/lib/register.dart b/lib/register.dart index 06ed58f..dc370a0 100644 --- a/lib/register.dart +++ b/lib/register.dart @@ -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 { /// 初始化方法 diff --git a/lib/widgets/goods_list/goods_list.dart b/lib/widgets/goods_list/goods_list.dart deleted file mode 100644 index 47ef519..0000000 --- a/lib/widgets/goods_list/goods_list.dart +++ /dev/null @@ -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 { - @override - Widget build(BuildContext context) { - return Container(); - } -} diff --git a/lib/widgets/goods_list/goods_list_creater.dart b/lib/widgets/goods_list/goods_list_creater.dart deleted file mode 100644 index 74349e4..0000000 --- a/lib/widgets/goods_list/goods_list_creater.dart +++ /dev/null @@ -1,72 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:zhiying_comm/zhiying_comm.dart'; - -class GoodsListCreater extends WidgetCreater { - @override - List createWidgets(Map model) { - return [ - _createHeader(), - _createBody(), - ]; - } - - @override - List createSkeleton(Map 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.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 -} diff --git a/lib/widgets/home_goods/home_goods.dart b/lib/widgets/home_goods/home_goods.dart new file mode 100644 index 0000000..dd7916c --- /dev/null +++ b/lib/widgets/home_goods/home_goods.dart @@ -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 { + @override + void initState() { + super.initState(); + } + + @override + void didChangeDependencies() { + super.didChangeDependencies(); + print('didChangeDependencies'); + bool isNeedLoadMore = Provider.of(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(context).scrollEnd.toString()), + ); + }), + ); + } +} diff --git a/lib/widgets/home_goods/home_goods_creater.dart b/lib/widgets/home_goods/home_goods_creater.dart new file mode 100644 index 0000000..aeb19e7 --- /dev/null +++ b/lib/widgets/home_goods/home_goods_creater.dart @@ -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 createWidgets(Map model) { + TabController controller = + TabController(initialIndex: 0, length: 6, vsync: ScrollableState()); + return [ + _createHeader(controller), + _createBody(controller), + ]; + } + + @override + List createSkeleton(Map 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: [ + // 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: [ + 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 +} diff --git a/lib/widgets/home_goods/home_goods_header.dart b/lib/widgets/home_goods/home_goods_header.dart new file mode 100644 index 0000000..e69de29 diff --git a/pubspec.yaml b/pubspec.yaml index 5cb396c..5f65d33 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -13,6 +13,7 @@ dependencies: shimmer: ^1.1.1 flutter_swiper : ^1.1.6 + provider: ^4.0.0 dev_dependencies: flutter_test: