欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Flutter 重構(gòu)屬性透?jìng)骷昂瘮?shù)透?jìng)魇褂檬纠?/h1>
 更新時(shí)間:2023年01月08日 15:22:01   作者:SoaringHeart  
這篇文章主要為大家介紹了Flutter 重構(gòu)屬性透?jìng)骷昂瘮?shù)透?jìng)魇褂檬纠斀猓行枰呐笥芽梢越梃b參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

一、來(lái)源

今天在研究 flutter 相冊(cè)庫(kù) wechat_assets_picker 遇到一個(gè)問題:(我需要在第三方庫(kù)基礎(chǔ)上封裝一個(gè)組件,供項(xiàng)目?jī)?nèi)部調(diào)用,組件內(nèi)封裝一些公共邏輯。)但是 AssetPicker.pickAssets 的屬性太多了,一個(gè)個(gè)傳遞實(shí)在太麻煩,就想是否有 vue 中那種數(shù)據(jù)透?jìng)鞯慕鉀Q方法呢(已知 flutter 中目前不支持這種屬性透?jìng)??苦苦思索5分鐘之后,靈光一閃:

函數(shù)透?jìng)?/h3>
/// pickAssets 方法源碼:
static Future<List<AssetEntity>?> pickAssets(
  BuildContext context, {
  List<AssetEntity>? selectedAssets,
  int maxAssets = 9,
  int pageSize = 80,
  int gridThumbSize = Constants.defaultGridThumbSize,
  int pathThumbSize = 80,
  int gridCount = 4,
  RequestType requestType = RequestType.image,
  List<int>? previewThumbSize,
  SpecialPickerType? specialPickerType,
  Color? themeColor,
  ThemeData? pickerTheme,
  SortPathDelegate<AssetPathEntity>? sortPathDelegate,
  AssetsPickerTextDelegate? textDelegate,
  FilterOptionGroup? filterOptions,
  WidgetBuilder? specialItemBuilder,
  IndicatorBuilder? loadingIndicatorBuilder,
  SpecialItemPosition specialItemPosition = SpecialItemPosition.none,
  bool allowSpecialItemWhenEmpty = false,
  AssetSelectPredicate<AssetEntity>? selectPredicate,
  bool? shouldRevertGrid,
  bool useRootNavigator = true,
  Curve routeCurve = Curves.easeIn,
  Duration routeDuration = const Duration(milliseconds: 300),
}) async {
...

二、 WechatPhotoPicker 使用示例

class WechatPhotoPickerDemo extends StatefulWidget {
  WechatPhotoPickerDemo({ Key? key, this.title}) : super(key: key);
  final String? title;
  @override
  _WechatPhotoPickerDemoState createState() => _WechatPhotoPickerDemoState();
}
class _WechatPhotoPickerDemoState extends State<WechatPhotoPickerDemo> {
  int maxCount = 9;
  List<AssetEntity> entitys = [];
  GlobalKey<WechatPhotoPickerState> _globalKey = GlobalKey();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title ?? "$widget"),
          actions: ['選擇',].map((e) => TextButton(
            child: Text(e,
              style: TextStyle(color: Colors.white),
            ),
            onPressed: onPicker,
          )).toList(),
        ),
        body: Column(
          children: [
            WechatPhotoPicker(
              key: _globalKey,
              selectedAssets: entitys,
              onChanged: (List<AssetEntity> selectedAssets) {
                print("selectedAssets: ${selectedAssets.length}");
              },
              onPicker: () => AssetPicker.pickAssets(
                  context,
                  maxAssets: 8,
                  selectedAssets: entitys,
                ),
            )
          ],
        )
    );
  }
  onPicker() async {
    _globalKey.currentState?.onPicker();
    print(entitys.length);
  }
}

二、 WechatPhotoPicker 組件源碼

/// 基于 wechat_assets_picker 的圖片選擇組件
class WechatPhotoPicker extends StatefulWidget {
  WechatPhotoPicker({
    Key? key,
    this.selectedAssets = const [],
    this.maxCount = 9,
    this.rowCount = 3,
    this.spacing = 10,
    this.decoration,
    this.addBuilder,
    required this.onChanged,
    this.onPicker,
  }) : super(key: key);
  /// 媒體對(duì)象數(shù)組
  List<AssetEntity> selectedAssets;
  /// 最大個(gè)數(shù)
  int maxCount;
  /// 每行元素個(gè)數(shù)
  int rowCount;
  /// 元素間距
  double spacing;
  /// 元素修飾器
  BoxDecoration? decoration;
  /// 添加圖片
  Widget Function(BuildContext context, double itemWidth)? addBuilder;
  /// 確認(rèn)選擇回調(diào)函數(shù)
  void Function(List<AssetEntity> selectedAssets) onChanged;
  /// 解決flutter數(shù)據(jù)無(wú)法透?jìng)鞯膯栴}(透?jìng)?AssetPicker.pickAssets 方法)
  Future<List<AssetEntity>?> Function()? onPicker;
  @override
  WechatPhotoPickerState createState() => WechatPhotoPickerState();
}
class WechatPhotoPickerState extends State<WechatPhotoPicker> {
  @override
  Widget build(BuildContext context) {
    return photoSection(
      selectedAssets: widget.selectedAssets,
      maxCount: widget.maxCount,
      rowCount: widget.rowCount,
      spacing: widget.spacing,
    );
  }
  photoSection({
    List<AssetEntity> selectedAssets = const [],
    int maxCount = 9,
    int rowCount = 3,
    double spacing = 10,
  }) {
    return LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints){
          double itemWidth = ((constraints.maxWidth - spacing * (rowCount - 1))/rowCount).truncateToDouble();
          // print("itemWidth: $itemWidth");
          return Wrap(
              spacing: spacing,
              runSpacing: spacing,
              children: [
                ...selectedAssets.map((e) => Container(
                  clipBehavior: Clip.antiAlias,
                  decoration: widget.decoration ?? BoxDecoration(
                    // border: Border.all(width: 2),
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                  ),
                  child: FadeInImage(
                    width: itemWidth,
                    height: itemWidth,
                    placeholder: AssetImage('images/img_placeholder.png'),
                    image: AssetEntityImageProvider(e, isOriginal: false),
                    fit: BoxFit.cover,
                  ),
                )).toList(),
                if (selectedAssets.length < maxCount)
                  InkWell(
                    onTap: () {
                      onPicker();
                    },
                    child: Container(
                      width: itemWidth,
                      height: itemWidth,
                      decoration: BoxDecoration(
                        color: Colors.black.withOpacity(0.1),
                        // border: Border.all(width: 1),
                        borderRadius: BorderRadius.all(Radius.circular(4)),
                      ),
                      child: widget.addBuilder != null ? widget.addBuilder!(context, itemWidth) : Icon(
                        Icons.add, 
                        size: itemWidth/3, 
                        color: Colors.black.withOpacity(0.3),
                      ),
                    ),
                  )
              ]
          );
        }
    );
  }
  onPicker() async {
    List<AssetEntity>? result = widget.onPicker != null ? await widget.onPicker!() :
    await AssetPicker.pickAssets(
      context,
      maxAssets: widget.maxCount,
      selectedAssets: widget.selectedAssets,
    );
    widget.selectedAssets = result ?? [];
    widget.onChanged(widget.selectedAssets);
    setState(() { });
  }
}

總結(jié)

1、onPicker 參數(shù)需要和調(diào)用方法搭配使用,即實(shí)現(xiàn)了函數(shù)透?jìng)?,函?shù)里的參數(shù)直接暴露給外部使用者,做二次定制開發(fā);如果默認(rèn)參數(shù)(可以適量添加)能夠滿足通用需求,則無(wú)需使用 onPicker 可選參數(shù);

  onPicker: () => AssetPicker.pickAssets(
      context,
      maxAssets: 8,
      selectedAssets: entitys,
    ),
    List<AssetEntity>? result = widget.onPicker != null ? await widget.onPicker!() :
    await AssetPicker.pickAssets(
      context,
      maxAssets: widget.maxCount,
      selectedAssets: widget.selectedAssets,
    );

2、WechatPhotoPickerState,沒有使用下?lián)Q線(私有)實(shí)現(xiàn)是為了向外部暴露 State, 可以通過 GlobalKey 獲取 State 實(shí)例對(duì)象,進(jìn)而調(diào)用一些封裝方法;達(dá)到更高的代碼復(fù)用;

聲明 GlobalKey:

 GlobalKey:<WechatPhotoPickerState> _globalKey = GlobalKey();

調(diào)用 State 方法:

_globalKey.currentState?.onPicker();

3、所有自定義組件原則上都要支持 key 屬性,才是一個(gè)完整的組件 Widget;

無(wú)論是移動(dòng)原生、前端 h5 或者 flutter 跨平臺(tái),各種數(shù)據(jù)透?jìng)鞯乃枷胧窍嘟谝欢巳〉猛黄浦?,其他端基本都是平移?shí)現(xiàn),這些可以減少代碼量又不損失功能,而且維護(hù)性和擴(kuò)展性更優(yōu)的實(shí)現(xiàn)方式就是代碼重構(gòu)的本質(zhì)。

以上就是Flutter 重構(gòu)屬性透?jìng)骷昂瘮?shù)透?jìng)魇褂檬纠脑敿?xì)內(nèi)容,更多關(guān)于Flutter 重構(gòu)屬性函數(shù)透?jìng)鞯馁Y料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

  • Android自定義豎排TextView實(shí)現(xiàn)實(shí)例

    Android自定義豎排TextView實(shí)現(xiàn)實(shí)例

    這篇文章主要介紹了Android自定義豎排TextView實(shí)現(xiàn)實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 詳解Flutter如何使用Completer實(shí)現(xiàn)防抖功能

    詳解Flutter如何使用Completer實(shí)現(xiàn)防抖功能

    防抖是用于確保時(shí)間內(nèi)的所有觸發(fā)被合并成單一請(qǐng)求,在Flutter中,我們可以使用Completer 來(lái)實(shí)現(xiàn)防抖功能,下面我們就來(lái)看看具體實(shí)現(xiàn)方法吧
    2024-03-03
  • Android播放視頻的三種方式

    Android播放視頻的三種方式

    這篇文章主要為大家詳細(xì)介紹了Android播放視頻的三種方式,使用其自帶的播放器、VideoView、MediaPlayer類和SurfaceView來(lái)實(shí)現(xiàn),感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android搜索框通用版

    Android搜索框通用版

    這篇文章主要為大家詳細(xì)介紹了Android搜索框通用版的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • android實(shí)現(xiàn)多線程下載文件(支持暫停、取消、斷點(diǎn)續(xù)傳)

    android實(shí)現(xiàn)多線程下載文件(支持暫停、取消、斷點(diǎn)續(xù)傳)

    本篇文章主要介紹了androids實(shí)現(xiàn)多線程下載文件,主要包括暫停、取消、斷點(diǎn)續(xù)傳等功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-02-02
  • Android實(shí)現(xiàn)圓角矩形和圓形ImageView的方式

    Android實(shí)現(xiàn)圓角矩形和圓形ImageView的方式

    這篇文章主要為大家詳細(xì)介紹了Android中實(shí)現(xiàn)圓角矩形和圓形的方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android自定義錄制視頻功能

    Android自定義錄制視頻功能

    這篇文章主要為大家詳細(xì)介紹了Android自定義錄制視頻功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android 通知欄的使用方法

    Android 通知欄的使用方法

    不同版本通知欄的創(chuàng)建方式不盡相同,當(dāng)前官方推薦使用 NotificationCompat 相關(guān)的API,兼容到Android 4.0,但是部分新功能,比如內(nèi)嵌回復(fù)操作,舊版本是無(wú)法支持的。
    2021-05-05
  • 最新評(píng)論