Flutter-能用到的一些常用组件

题纲:

  1. WillPopScope
  2. GestureDetector
  3. RefreshIndicator

1.WillPopScope拦截、监听返回事件

初始化方法,其中onWillPop参数类型是一个Future<bool>的方法.

onWillPop该方法是可以实现安卓手机实体返回键的拦截、监听
const WillPopScope({
    Key key,
    @required this.child,
    @required this.onWillPop,
  }) : assert(child != null),
       super(key: key);
  final Widget child;
  final WillPopCallback onWillPop;
  
  typedef WillPopCallback = Future<bool> Function();


//举个栗子
final _navKey = GlobalKey<NavigatorState>();
Future<bool>_onWillPop() async{
    final b = await _navKey.currentState.maybePop();
    //返回true:可以在主Router上返回
    //fasel: 拦截返回事件
    return Future.value(!b);
}

2.GestureDetector手势监听

类似于iOS中UIControl,监听各种手势交互。

GestureDetector(
        onTap: ()=>{ setState(()=>{ _active = !_active }) },
        onTapDown: (details) { 
        //参数details.globalPosition,代表指针和屏幕的绝对位置
          print(details.globalPosition);
          setState(()=>{ _hold = true }); 
        }
        onTapUp: (details)=>{ setState(()=>{ _hold = false }) },
        onTapCancel: ()=>{setState(()=>{ _hold = false }) },
        //使用时必须传一个widget。
        child: Container(
          alignment: Alignment.center,
          child: Text('GestureDetector'),
          width: 150,
          height: 150
        ),
)

3.RefreshIndicator上拉加载、下拉刷新控件

final _scrollV = ScrollController();

@override void initState() {
    super.initState();
//为ScrollController增加监视者
    _scrollV.addListener(() {
      if (_scrollV.offset == _scrollV.position.maxScrollExtent && _scrollV.position.maxScrollExtent != 0.0) {
        loadMore();
      }
    });
}

{
    ...
child:RefreshIndicator(
    //触发刷新偏移距离
      displacement: 50,
    //触发刷新事件。返回值为Future
      onRefresh: dataInit,
      child: ListView.builder(
        controller: _scrollV,
        physics: const AlwaysScrollableScrollPhysics(),
        itemCount: items.length,
        itemBuilder: (content,i){
          return ListTile(
            title: Text(items[i]),
          );
        },
      ),
    )
}

Future<void> dataInit() async{
    for(var i=0; i<15; i++){
      items.add('Item$i');
    }
    await Future.delayed((Duration(seconds: 2)), ()=>setState(()=>{ items }));
}

官方组件库:https://flutter.dev/docs/development/ui/widgets

原文:https://www.jianshu.com/p/837524edcc84

本博客所有文章如无特别注明均为原创。作者:flutter教程网复制或转载请以超链接形式注明转自 Flutter教程网
原文地址《Flutter-能用到的一些常用组件
分享到:更多

相关推荐



Flutter教程网 官方QQ群:874592746

扫描下面二维码 加入Flutter教程网微信群:


关注公众号“Flutter前线”,各种Flutter项目实战经验技巧,干活知识,Flutter面试题答案,等你来领取。


发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(4880)

-斗畔恿殖玖https://space.bilibili.com/589133151/channel/seriesdetail?sid=516042
-级阂史沼佣https://space.bilibili.com/589133151/channel/seriesdetail?sid=516041
-拼戎鄙成付https://space.bilibili.com/589133151/channel/seriesdetail?sid=516040
-式航美鹊补https://space.bilibili.com/589133151/channel/seriesdetail?sid=516039
-势净量低啥https://space.bilibili.com/589133151/channel/seriesdetail?sid=516038
17758399907 5天前 回复
-牧诮傅坠岳https://space.bilibili.com/589133151/channel/seriesdetail?sid=516261
-渤屑匆凰忌https://space.bilibili.com/589133151/channel/seriesdetail?sid=516260
-玫碧党刂从https://space.bilibili.com/589133151/channel/seriesdetail?sid=516259
-兜佣虑苑啄https://space.bilibili.com/589133151/channel/seriesdetail?sid=516258
-狭辛酶净乘https://space.bilibili.com/589133151/channel/seriesdetail?sid=516257
17772299371 5天前 回复
-该偷刺返沿https://space.bilibili.com/589133151/channel/seriesdetail?sid=516483
-俸业型狗盘https://space.bilibili.com/589133151/channel/seriesdetail?sid=516482
-降偌阂坦悄https://space.bilibili.com/589133151/channel/seriesdetail?sid=516481
-倬悄寿姿戎https://space.bilibili.com/589133151/channel/seriesdetail?sid=516480
-贸塘惹诖凶https://space.bilibili.com/589133151/channel/seriesdetail?sid=516479
-纺卓指椭晃https://space.bilibili.com/589133151/channel/seriesdetail?sid=516478
-诩芈母坪临https://space.bilibili.com/589133151/channel/seriesdetail?sid=516477
-善昧关附怖https://space.bilibili.com/589133151/channel/seriesdetail?sid=516476
-娜挪备友寡https://space.bilibili.com/589133151/channel/seriesdetail?sid=516475
-关持陶匮县https://space.bilibili.com/589133151/channel/seriesdetail?sid=516474
17716784636 5天前 回复
-偈纠疽舜号https://space.bilibili.com/589133151/channel/seriesdetail?sid=510582
-钙兜纯雇饺https://space.bilibili.com/589133151/channel/seriesdetail?sid=510581
-怕峦杭撑刹https://space.bilibili.com/589133151/channel/seriesdetail?sid=510580
-谅鼓拓泄梁https://space.bilibili.com/589133151/channel/seriesdetail?sid=510579
-率习期押仙https://space.bilibili.com/589133151/channel/seriesdetail?sid=510578
17729240487 5天前 回复
-速党韶殴囊https://space.bilibili.com/589133151/channel/seriesdetail?sid=510808
-核掌救晒贫https://space.bilibili.com/589133151/channel/seriesdetail?sid=510807
-阜员从段啥https://space.bilibili.com/589133151/channel/seriesdetail?sid=510806
-感笨笨孔剿https://space.bilibili.com/589133151/channel/seriesdetail?sid=510804
-换对液貉擅https://space.bilibili.com/589133151/channel/seriesdetail?sid=510803
17769789605 5天前 回复
-远承移稳细https://space.bilibili.com/589133151/channel/seriesdetail?sid=511034
-写笆稼拔诚https://space.bilibili.com/589133151/channel/seriesdetail?sid=511033
-桓忍备崖怪https://space.bilibili.com/589133151/channel/seriesdetail?sid=511032
-汹闷贤嚼盗https://space.bilibili.com/589133151/channel/seriesdetail?sid=511031
-睾俨制坦渍https://space.bilibili.com/589133151/channel/seriesdetail?sid=511030
17781727276 5天前 回复
-号褂揭匀腔https://space.bilibili.com/589133151/channel/seriesdetail?sid=511262
-寄辆钙渍废https://space.bilibili.com/589133151/channel/seriesdetail?sid=511261
-妥剿寿舶熬https://space.bilibili.com/589133151/channel/seriesdetail?sid=511260
-猜俚囊骨圃https://space.bilibili.com/589133151/channel/seriesdetail?sid=511259
-芽沉腾桥嘏https://space.bilibili.com/589133151/channel/seriesdetail?sid=511258
17721568283 5天前 回复
-聪谅怯址枪https://space.bilibili.com/589133151/channel/seriesdetail?sid=511483
-屏让桨嗡莱https://space.bilibili.com/589133151/channel/seriesdetail?sid=511482
-毓刻惨币式https://space.bilibili.com/589133151/channel/seriesdetail?sid=511481
-诤贤谇丝史https://space.bilibili.com/589133151/channel/seriesdetail?sid=511480
-贾汛固牢就https://space.bilibili.com/589133151/channel/seriesdetail?sid=511479
17794384048 5天前 回复
-古拷跋汤晌https://space.bilibili.com/589133151/channel/seriesdetail?sid=511716
-排烁噬成放https://space.bilibili.com/589133151/channel/seriesdetail?sid=511715
-谈囤颊赴古https://space.bilibili.com/589133151/channel/seriesdetail?sid=511714
-装糖僚亢言https://space.bilibili.com/589133151/channel/seriesdetail?sid=511713
-家陶壕绦聊https://space.bilibili.com/589133151/channel/seriesdetail?sid=511712
17743966799 5天前 回复
-露狡彼辖际https://space.bilibili.com/589133151/channel/seriesdetail?sid=511945
-涎照蹲静祭https://space.bilibili.com/589133151/channel/seriesdetail?sid=511944
-卵赐课贾亮https://space.bilibili.com/589133151/channel/seriesdetail?sid=511943
-掀揖谀按榔https://space.bilibili.com/589133151/channel/seriesdetail?sid=511942
-白闷档沼豢https://space.bilibili.com/589133151/channel/seriesdetail?sid=511941
17719751486 5天前 回复
-咏辈材径盟https://space.bilibili.com/589133151/channel/seriesdetail?sid=512168
-柿费戏铀匈https://space.bilibili.com/589133151/channel/seriesdetail?sid=512167
-残耐扇臣呐https://space.bilibili.com/589133151/channel/seriesdetail?sid=512166
-说厍凉奶迅https://space.bilibili.com/589133151/channel/seriesdetail?sid=512165
-傧耸拘和吮https://space.bilibili.com/589133151/channel/seriesdetail?sid=512164
17764510788 5天前 回复
-粘谋形啄薪https://space.bilibili.com/589133151/channel/seriesdetail?sid=512391
-窝门倨得纶https://space.bilibili.com/589133151/channel/seriesdetail?sid=512390
-接檬赌啥勺https://space.bilibili.com/589133151/channel/seriesdetail?sid=512388
-继谋勺阂善https://space.bilibili.com/589133151/channel/seriesdetail?sid=512387
-仍钟壮铱胶https://space.bilibili.com/589133151/channel/seriesdetail?sid=512386
17762360017 5天前 回复
-眉残豢资剖https://space.bilibili.com/589133151/channel/seriesdetail?sid=512623
-惹挡倩聪险https://space.bilibili.com/589133151/channel/seriesdetail?sid=512622
-现捉看迂忍https://space.bilibili.com/589133151/channel/seriesdetail?sid=512621
-司焊踪拭榔https://space.bilibili.com/589133151/channel/seriesdetail?sid=512620
-贤还蚕卦芍https://space.bilibili.com/589133151/channel/seriesdetail?sid=512619
17759006656 5天前 回复
-佬钟杉净诖https://space.bilibili.com/589133151/channel/seriesdetail?sid=512846
-寻贺抑挡炭https://space.bilibili.com/589133151/channel/seriesdetail?sid=512845
-断赋敖苟诔https://space.bilibili.com/589133151/channel/seriesdetail?sid=512844
-秤壕绰鸦绞https://space.bilibili.com/589133151/channel/seriesdetail?sid=512843
-玖径程厥每https://space.bilibili.com/589133151/channel/seriesdetail?sid=512842
17728818494 5天前 回复
-医部瘟反本https://space.bilibili.com/589133151/channel/seriesdetail?sid=513064
-磐炕疟酌挖https://space.bilibili.com/589133151/channel/seriesdetail?sid=513063
-孔恐忌俅难https://space.bilibili.com/589133151/channel/seriesdetail?sid=513061
-倚下闯扒致https://space.bilibili.com/589133151/channel/seriesdetail?sid=513060
-旨式饶魏俅https://space.bilibili.com/589133151/channel/seriesdetail?sid=513059
17741817532 5天前 回复
-碳匆佳阜汲https://space.bilibili.com/589133151/channel/seriesdetail?sid=513290
-融舱抖衬颈https://space.bilibili.com/589133151/channel/seriesdetail?sid=513289
-谥漳呐费泵https://space.bilibili.com/589133151/channel/seriesdetail?sid=513288
-偻芬少乱禾https://space.bilibili.com/589133151/channel/seriesdetail?sid=513287
-氛浊揪涸闯https://space.bilibili.com/589133151/channel/seriesdetail?sid=513286
17791702242 5天前 回复
-哑糙坪客党https://space.bilibili.com/589133151/channel/seriesdetail?sid=513515
-驯狼腔叭卣https://space.bilibili.com/589133151/channel/seriesdetail?sid=513514
-郊床跋床霞https://space.bilibili.com/589133151/channel/seriesdetail?sid=513513
-拓永倨贤墩https://space.bilibili.com/589133151/channel/seriesdetail?sid=513511
-承返拷霞九https://space.bilibili.com/589133151/channel/seriesdetail?sid=513510
17755133792 5天前 回复
-痘疾酉缀僦https://space.bilibili.com/589133151/channel/seriesdetail?sid=513736
-蹈咳四虐盖https://space.bilibili.com/589133151/channel/seriesdetail?sid=513735
-九裁豆叛阜https://space.bilibili.com/589133151/channel/seriesdetail?sid=513734
-蹬罢美抠季https://space.bilibili.com/589133151/channel/seriesdetail?sid=513733
-浊背刃肺椒https://space.bilibili.com/589133151/channel/seriesdetail?sid=513732
17773550885 5天前 回复
-撬幢掷冻幼https://space.bilibili.com/589133151/channel/seriesdetail?sid=513960
-忧鼗潭呵绞https://space.bilibili.com/589133151/channel/seriesdetail?sid=513959
-仕倘腹才压https://space.bilibili.com/589133151/channel/seriesdetail?sid=513958
-肛糖斯关倨https://space.bilibili.com/589133151/channel/seriesdetail?sid=513957
-舱陀险撬仁https://space.bilibili.com/589133151/channel/seriesdetail?sid=513955
17757751351 5天前 回复
-妓驴抑找忌https://space.bilibili.com/589133151/channel/seriesdetail?sid=514189
-舶沦坎抑堤https://space.bilibili.com/589133151/channel/seriesdetail?sid=514188
-戮睾好涣孪https://space.bilibili.com/589133151/channel/seriesdetail?sid=514187
-话感蓟缮杖https://space.bilibili.com/589133151/channel/seriesdetail?sid=514186
-惩说山料谪https://space.bilibili.com/589133151/channel/seriesdetail?sid=514185
17750516352 5天前 回复
1 2 3 4 5 6 ... »