Skip to content

Commit

Permalink
migrate gallery to null safety (#90)
Browse files Browse the repository at this point in the history
* migrate gallery to null safety

* change the type error

* remove @

* change String? to String
  • Loading branch information
Nayuta403 authored Jan 25, 2022
1 parent 8629bb0 commit 04432c6
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 211 deletions.
24 changes: 11 additions & 13 deletions lib/src/components/gallery/config/brn_basic_gallery_config.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
// @dart=2.9

import 'package:flutter/material.dart';

abstract class BrnBasicGroupConfig {
//每一个组的标题
String title;
/// 每一个组的标题
String? title;

List<BrnBasicItemConfig> configList;
List<BrnBasicItemConfig>? configList;

BrnBasicGroupConfig({this.title, @required this.configList});
BrnBasicGroupConfig({this.title, required this.configList});
}

//每一个item的配置接口,定制化的需求可以自己实现相关接口
/// 每一个 item 的配置接口,定制化的需求可以自己实现相关接口
abstract class BrnBasicItemConfig {
//该配置的groupIndex
int groupIndex;
/// 该配置的 groupIndex
int? groupIndex;

//改配置的type信息
String type;
/// 该配置的 type 信息
String? type;

//构建查看详情页的widget
/// 构建查看详情页的 widget
Widget buildDetailWidget(BuildContext context,
List<BrnBasicGroupConfig> allConfig, int groupId, int index);

//构建简略页的widget
/// 构建简略页的 widget
Widget buildSummaryWidget(BuildContext context,
List<BrnBasicGroupConfig> allConfig, int groupId, int index);
}
46 changes: 22 additions & 24 deletions lib/src/components/gallery/config/brn_bottom_card.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
// @dart=2.9

import 'dart:math';

import 'package:bruno/src/constants/brn_asset_constants.dart';
import 'package:bruno/src/theme/configs/brn_gallery_detail_config.dart';
import 'package:bruno/src/utils/brn_tools.dart';
import 'package:flutter/material.dart';

/// fold 收起状态
/// unfold 展开状态
/// cantFold 不可折叠的状态,描述信息直接展开
/// [fold] 收起状态
/// [unfold] 展开状态
/// [cantFold] 不可折叠的状态,描述信息直接展开
enum PhotoBottomCardState { fold, unFold, cantFold }

// ignore: must_be_immutable
class BrnPhotoBottomCard extends StatefulWidget {
final String name;
final String des;
final String? name;
final String? des;
final PhotoBottomCardState model;
final double contentHeight;
BrnGalleryDetailConfig themeData;
BrnGalleryDetailConfig? themeData;

BrnPhotoBottomCard(
{Key key,
{Key? key,
this.name,
this.des,
this.model = PhotoBottomCardState.cantFold,
Expand All @@ -35,7 +33,7 @@ class BrnPhotoBottomCard extends StatefulWidget {

class _BrnPhotoBottomCardState extends State<BrnPhotoBottomCard>
with TickerProviderStateMixin {
PhotoBottomCardState state;
PhotoBottomCardState? state;

@override
void initState() {
Expand All @@ -47,14 +45,14 @@ class _BrnPhotoBottomCardState extends State<BrnPhotoBottomCard>
Widget build(BuildContext context) {
return Container(
width: double.infinity,
color: widget.themeData.bottomBackgroundColor,
color: widget.themeData!.bottomBackgroundColor,
child: state == PhotoBottomCardState.cantFold
? buildCantFoldWidget()
: buildFoldableWidget(),
);
}

//构建可折叠的card
/// 构建可折叠的card
Widget buildFoldableWidget() {
if (state == PhotoBottomCardState.fold) {
return Container(
Expand All @@ -64,7 +62,7 @@ class _BrnPhotoBottomCardState extends State<BrnPhotoBottomCard>
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(widget.name ?? "",
style: widget.themeData.titleStyle?.generateTextStyle()),
style: widget.themeData!.titleStyle.generateTextStyle()),
GestureDetector(
onTap: () {
setState(() {
Expand All @@ -78,12 +76,12 @@ class _BrnPhotoBottomCardState extends State<BrnPhotoBottomCard>
padding: EdgeInsets.only(right: 4),
child: Text('展开',
style:
widget.themeData.actionStyle?.generateTextStyle()),
widget.themeData!.actionStyle.generateTextStyle()),
),
Transform.rotate(
angle: pi,
child: BrunoTools.getAssetImageWithColor(
BrnAsset.iconUpArrow, widget.themeData.iconColor),
BrnAsset.iconUpArrow, widget.themeData!.iconColor),
)
],
),
Expand All @@ -102,7 +100,7 @@ class _BrnPhotoBottomCardState extends State<BrnPhotoBottomCard>
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(widget.name ?? "",
style: widget.themeData.titleStyle?.generateTextStyle()),
style: widget.themeData!.titleStyle.generateTextStyle()),
GestureDetector(
onTap: () {
setState(() {
Expand All @@ -115,11 +113,11 @@ class _BrnPhotoBottomCardState extends State<BrnPhotoBottomCard>
Padding(
padding: EdgeInsets.only(right: 4),
child: Text('收起',
style: widget.themeData.actionStyle
?.generateTextStyle()),
style: widget.themeData!.actionStyle
.generateTextStyle()),
),
BrunoTools.getAssetImageWithColor(
BrnAsset.iconUpArrow, widget.themeData.iconColor)
BrnAsset.iconUpArrow, widget.themeData!.iconColor)
],
),
)
Expand All @@ -128,15 +126,15 @@ class _BrnPhotoBottomCardState extends State<BrnPhotoBottomCard>
),
Text(
widget.des ?? "",
style: widget.themeData.contentStyle?.generateTextStyle(),
style: widget.themeData!.contentStyle.generateTextStyle(),
)
],
),
);
}
}

//构建不可折叠的card,content是一个ScrollView
/// 构建不可折叠的 card, content 是一个 ScrollView
Widget buildCantFoldWidget() {
return Column(
mainAxisAlignment: MainAxisAlignment.start,
Expand All @@ -148,18 +146,18 @@ class _BrnPhotoBottomCardState extends State<BrnPhotoBottomCard>
padding: EdgeInsets.only(top: 16, bottom: 12, left: 20, right: 20),
child: Text(
widget.name ?? "",
style: widget.themeData.titleStyle?.generateTextStyle(),
style: widget.themeData!.titleStyle.generateTextStyle(),
),
),
),
Container(
height: widget.contentHeight ?? 150,
height: widget.contentHeight,
child: SingleChildScrollView(
physics: BouncingScrollPhysics(),
child: Padding(
child: Text(
widget.des ?? "",
style: widget.themeData.contentStyle?.generateTextStyle(),
style: widget.themeData!.contentStyle.generateTextStyle(),
),
padding: EdgeInsets.only(left: 20, right: 20)),
))
Expand Down
3 changes: 1 addition & 2 deletions lib/src/components/gallery/config/brn_controller.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @dart=2.9


import 'package:flutter/material.dart';

Expand All @@ -11,7 +11,6 @@ class BrnGalleryController extends ChangeNotifier {
/// [groupId] 第几组图片
/// [indexId] 组内的第几张
void refresh(int groupId, int indexId) {
assert(groupId != null && indexId != null);
this.groupId = groupId;
this.indexId = indexId;
notifyListeners();
Expand Down
83 changes: 42 additions & 41 deletions lib/src/components/gallery/config/brn_photo_gallery_config.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @dart=2.9

import 'package:bruno/src/components/gallery/config/brn_basic_gallery_config.dart';
import 'package:bruno/src/components/gallery/config/brn_bottom_card.dart';
import 'package:bruno/src/components/loading/brn_loading.dart';
Expand All @@ -10,61 +8,64 @@ import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';

class BrnPhotoGroupConfig extends BrnBasicGroupConfig {
final List<String> urls;
final String title;
List<BrnBasicItemConfig> configList;
final BrnGalleryDetailConfig themeData;

//通过url列表生成配置
BrnPhotoGroupConfig.url({this.title, @required this.urls, this.themeData}) {
configList = List();
urls.forEach((item) =>
configList.add(BrnPhotoItemConfig(url: item, themeData: themeData)));
}

//自定义配置列表
BrnPhotoGroupConfig({this.urls, this.title, this.configList, this.themeData});
final List<String>? urls;
final String? title;
List<BrnBasicItemConfig>? configList;
final BrnGalleryDetailConfig? themeData;

/// 通过 [urls] 列表生成配置
BrnPhotoGroupConfig.url({this.title, required this.urls, this.themeData})
: super(
title: title,
configList: urls
?.map((item) =>
BrnPhotoItemConfig(url: item, themeData: themeData))
.toList());

/// 自定义配置列表
BrnPhotoGroupConfig({this.urls, this.title, this.configList, this.themeData})
: super(title: title, configList: configList);
}

//图片类的配置
/// 图片类的配置
class BrnPhotoItemConfig extends BrnBasicItemConfig {
//图片url
/// 图片url
final String url;

//图片的展示模式
/// 图片的展示模式
final BoxFit fit;

//展位图
/// 占位图
final String placeHolder;

//图片名称 用于详情页展示
final String name;
/// 图片名称 用于详情页展示
final String? name;

//图片描述公 用于详情页展示
final String des;
/// 图片描述公 用于详情页展示
final String? des;

//详情页图片点击回调
final VoidCallback onTap;
/// 详情页图片点击回调
final VoidCallback? onTap;

//详情页双击回调
final VoidCallback onDoubleTap;
/// 详情页双击回调
final VoidCallback? onDoubleTap;

//详情页长按回调
final VoidCallback onLongPress;
/// 详情页长按回调
final VoidCallback? onLongPress;

//详情页是否展示底部卡片,需要提供name和des信息
/// 详情页是否展示底部卡片,需要提供name和des信息
final bool showBottom;

//底部展示卡片的模式// 0 表示 展开不可收起 1 收起可展开 2、 展开可收起
/// [PhotoBottomCardState] 底部展示卡片的模式
final PhotoBottomCardState bottomCardModel;

//指定展开不可收起下 content的高度
/// 指定展开不可收起下 content的高度
final double bottomContentHeight;

BrnGalleryDetailConfig themeData;
BrnGalleryDetailConfig? themeData;

BrnPhotoItemConfig({
@required this.url,
required this.url,
this.fit = BoxFit.cover,
this.placeHolder =
"packages/${BrnStrings.flutterPackageName}/assets/icons/grey_place_holder.png",
Expand All @@ -80,7 +81,7 @@ class BrnPhotoItemConfig extends BrnBasicItemConfig {
}) {
this.themeData ??= BrnGalleryDetailConfig();
this.themeData = BrnThemeConfigurator.instance
.getConfig(configId: this.themeData.configId)
.getConfig(configId: this.themeData!.configId)
.galleryDetailConfig
.merge(this.themeData);
}
Expand Down Expand Up @@ -117,23 +118,23 @@ class BrnPhotoItemConfig extends BrnBasicItemConfig {
bottom: 0,
child: GestureDetector(
onTap: () {
if (onTap != null) onTap();
onTap?.call();
},
onDoubleTap: () {
if (onDoubleTap != null) onDoubleTap();
onDoubleTap?.call();
},
onLongPress: () {
if (onLongPress != null) onLongPress();
onLongPress?.call();
},
child: Container(
color: Colors.white,
child: PhotoView(
backgroundDecoration:
BoxDecoration(color: themeData.pageBackgroundColor),
BoxDecoration(color: themeData!.pageBackgroundColor),
loadingBuilder: (context, event) {
return Container(
child: BrnLoadingDialog(),
color: themeData.pageBackgroundColor,
color: themeData!.pageBackgroundColor,
);
},
imageProvider: NetworkImage(url),
Expand Down
Loading

0 comments on commit 04432c6

Please sign in to comment.