Flutter Go 源碼分析(五)

- (7)CollectionPage--組件收藏頁面

CollectionPage頁面相對比較簡單沸移,就一個列表頁面痪伦,開始吧。
初始化方法:

_CollectionPageState() {
    final eventBus = new EventBus();
    ApplicationEvent.event = eventBus;
  }
  CollectionControlModel _collectionControl = new CollectionControlModel();
  List<Collection> _collectionList = [];
  ScrollController _scrollController = new ScrollController();
  var _icons;

  @override
  void initState() {
    super.initState();
    _getList();
    ApplicationEvent.event.on<CollectionEvent>().listen((event) {//訂閱
      _getList();
    });
  }
  1. ApplicationEvent
    ApplicationEvent是通過event_bus三方進行組件間事件傳遞的工具雹锣。event_bus這個庫大家有興趣可以自行去了解网沾。
    訂閱:
ApplicationEvent.event.on<CollectionEvent>().listen((event) {//訂閱
      _getList();
    });

發(fā)送通知:

ApplicationEvent.event
                .fire(CollectionEvent(widget.title, _router, true));
  1. CollectionControlModel
    CollectionControlModel負責收藏數(shù)據(jù)的存取,和上面的CatControlModel差不多:
final String table = 'collection';
  Sql sql;

  CollectionControlModel() {//set 表名
    sql = Sql.setTable(table);
  }

  // 獲取所有的收藏

  // 插入新收藏
  Future insert(Collection collection) {
    var result =
        sql.insert({'name': collection.name, 'router': collection.router});
    return result;
  }

  // 獲取全部的收藏
  Future<List<Collection>> getAllCollection() async {
    List list = await sql.getByCondition();
    List<Collection> resultList = [];
    list.forEach((item){
      print(item);
      resultList.add(Collection.fromJSON(item));
    });
    return resultList;
  }

  // 通過收藏名獲取router
  Future getRouterByName(String name) async {
    List list = await sql.getByCondition(conditions: {'name': name});
    return list;
  }

  // 刪除
  Future deleteByName(String name) async{
    return await sql.delete(name,'name');
  }
  1. build函數(shù)
    build的主要內(nèi)容在_renderList方法里面:
Widget _renderList(context, index) {
    if (index == 0) {
      return Container(//頭部標題
        height: 40.0,
        padding: const EdgeInsets.only(left: 10.0),
        child: Row(
          children: <Widget>[
            Icon(
              Icons.warning,
              size: 22.0,
            ),
            SizedBox(
              width: 5.0,
            ),
            Text('模擬器重新運行會丟失收藏'),
          ],
        ),
      );
    }
    if (_collectionList[index - 1].router.contains('http')) {
      if (_collectionList[index - 1].name.endsWith('Doc')) {
        _icons = Icons.library_books;
      } else {
        _icons = Icons.language;
      }
    } else {
      _icons = Icons.extension;
    }
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 5.0),
      margin: const EdgeInsets.only(bottom: 7.0),
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          new BoxShadow(
            color: const Color(0xFFd0d0d0),
            blurRadius: 1.0,
            spreadRadius: 2.0,
            offset: Offset(3.0, 2.0),
          ),
        ],
      ),
      child: ListTile(
        leading: Icon(
          _icons,
          size: 30.0,
          color: Theme.of(context).primaryColor,
        ),
        title: Text(
          Uri.decodeComponent(_collectionList[index - 1].name),
          overflow: TextOverflow.ellipsis,
          style: TextStyle(fontSize: 17.0),
        ),
        trailing:
            Icon(Icons.keyboard_arrow_right, color: Colors.grey, size: 30.0),
        onTap: () {
          if (_collectionList[index - 1].router.contains('http')) {//如果是網(wǎng)頁就直接跳轉(zhuǎn)
            // 注意這里title已經(jīng)轉(zhuǎn)義過了
            Application.router.navigateTo(context,
                '${Routes.webViewPage}?title=${_collectionList[index - 1].name}&url=${Uri.encodeComponent(_collectionList[index - 1].router)}');
          } else {//不是網(wǎng)頁就跳轉(zhuǎn)相應(yīng)的頁面
            Application.router
                .navigateTo(context, "${_collectionList[index - 1].router}");
          }
        },
      ),
    );
  }
- (8)FourthPage--第四個頁面

這個頁面看似比較復(fù)雜蕊爵,但是實際層級結(jié)構(gòu)還是很清晰的:

Widget build(BuildContext context) {
    return new Stack(
      children: [
        new Page(
          // page 的主要內(nèi)容
          viewModel: pages[activeIndex],
          percentVisible: 1.0,
        ),
        new PageReveal(//動畫page
          revealPercent: slidePercent,
          child: new Page(
            viewModel: pages[nextPageIndex],
            percentVisible: slidePercent,
          ),
        ),
        new PagerIndicator(//指示
          viewModel: new PagerIndicatorViewModel(
            pages,
            activeIndex,
            slideDirection,
            slidePercent,
          ),
        ),
        new PageDragger(//負責滑動-->手勢
          canDragLeftToRight: activeIndex > 0,
          canDragRightToLeft: activeIndex < pages.length - 1,
          slideUpdateStream: this.slideUpdateStream,
        )
      ],
    );
  }

代碼我已經(jīng)盡量添加了詳細的注釋辉哥,可以看到分成四個部分:Page(主要內(nèi)容的顯示頁面activePage)、PageReveal(用于操作nextPage執(zhí)行滑動動畫)攒射、PagerIndicator(滑動指示條)醋旦、PageDragger(滑動手勢)

  1. Page(主要內(nèi)容的顯示頁面)
    初始化相關(guān):
final PageViewModel viewModel;
  final double percentVisible;
  Page({
    this.viewModel,//頁面信息
    this.percentVisible = 1.0,//滑動百分百
  });

build函數(shù)

Widget build(BuildContext context) {
    return Stack(
        //alignment: const Alignment(1.2, 0.6),
        children: [
          Container(
              width: double.infinity,
              /// height:MediaQuery.of(context).size.height-200.0,
              color: viewModel.color,
              padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
              child: Opacity(
                opacity: percentVisible,
                child: ListView(
                  children: <Widget>[
                    layout(context),
                  ],
                ),
              )
          ),
          Positioned(//右上角GitHub 按鈕
              right: -5.0,
              top: 2.0,
              child: creatButton(context, 'GitHub', Icons.arrow_forward, 'goGithub')
          ),
        ]
    );
  }

build函數(shù)中我們可以看到結(jié)構(gòu)分為兩部分,layout(主要視圖)会放、creatButton(左上角github按鈕)饲齐。

  • layout
Column layout(BuildContext context) {
    return Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Transform(//這個動畫效果是 向上慢慢移動的效果
            transform: Matrix4.translationValues(
                0.0, 50.0 * (1.0 - percentVisible), 0.0),
            child: Padding(
              padding: EdgeInsets.only(top: 20.0, bottom: 10.0),
              child: Image.asset(viewModel.heroAssetPath,
                  width: 160.0, height: 160.0),
            ),
          ),
          Transform(//這個動畫效果是 向上慢慢移動的效果
            transform: Matrix4.translationValues(
                0.0, 30.0 * (1.0 - percentVisible), 0.0),
            child: Padding(
              padding: EdgeInsets.only(top: 10.0, bottom: 10.0),
              child: Text(
                viewModel.title,
                style: TextStyle(
                  color: Colors.white,
                  fontFamily: 'FlamanteRoma',
                  fontSize: 28.0,
                ),
              ),
            ),
          ),
          Transform(//這個動畫效果是 向上慢慢移動的效果
            transform: Matrix4.translationValues(
                0.0, 30.0 * (1.0 - percentVisible), 0.0),
            child: Padding(
              padding: EdgeInsets.only(bottom: 10.0),
              child: Text(
                viewModel.body,
                textAlign: TextAlign.center,
                style: TextStyle(
                  height: 1.2,
                  color: Colors.white,
                  fontFamily: 'FlamanteRomaItalic',
                  fontSize: 18.0,
                ),
              ),
            ),
          ),
//          ButtonBar(
//            alignment: MainAxisAlignment.center,
//            children: <Widget>[
//              creatButton(context, '開始使用', Icons.add_circle_outline, 'start'),
//              creatButton(context, 'GitHub', Icons.arrow_forward, 'goGithub'),
//            ],
//          )
        ]);
  }
  • creatButton
Widget creatButton(
      BuildContext context, String txt, IconData iconName, String type) {
    return RaisedButton.icon(
        onPressed: () async {
          if (type == 'start') {//這是看代碼應(yīng)該是關(guān)閉 歡迎頁
            await SpUtil.getInstance()
              ..putBool(SharedPreferencesKeys.showWelcome, false);
            _goHomePage(context);
          } else if (type == 'goGithub') {//跳轉(zhuǎn) github
            Application.router.navigateTo(context,
                '${Routes.webViewPage}?title=${Uri.encodeComponent(txt)} Doc&&url=${Uri.encodeComponent("https://github.com/alibaba/flutter-go")}');
          }
        },
        elevation: 10.0,
        color: Colors.black26,
        // shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.horizontal(left: Radius.circular(20.0))),
        //如果不手動設(shè)置icon和text顏色,則默認使用foregroundColor顏色
        icon: Icon(iconName, color: Colors.white, size: 14.0),
        label: Text(
          txt,
          maxLines: 1,
          style: TextStyle(
              color: Colors.white, fontSize: 14, fontWeight: FontWeight.w700),
        ));
  }

這里我都在原來的基礎(chǔ)上加上了一些注釋,可以結(jié)合看咧最,我不多說了捂人。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市窗市,隨后出現(xiàn)的幾起案子先慷,更是在濱河造成了極大的恐慌,老刑警劉巖咨察,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件论熙,死亡現(xiàn)場離奇詭異,居然都是意外死亡摄狱,警方通過查閱死者的電腦和手機脓诡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進店門无午,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人祝谚,你說我怎么就攤上這事宪迟。” “怎么了交惯?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵次泽,是天一觀的道長。 經(jīng)常有香客問我席爽,道長意荤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任只锻,我火速辦了婚禮玖像,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘齐饮。我一直安慰自己捐寥,他們只是感情好,可當我...
    茶點故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布祖驱。 她就那樣靜靜地躺著握恳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪羹膳。 梳的紋絲不亂的頭發(fā)上睡互,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天,我揣著相機與錄音陵像,去河邊找鬼就珠。 笑死,一個胖子當著我的面吹牛醒颖,可吹牛的內(nèi)容都是我干的妻怎。 我是一名探鬼主播,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼泞歉,長吁一口氣:“原來是場噩夢啊……” “哼逼侦!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起腰耙,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤榛丢,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后挺庞,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體晰赞,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了掖鱼。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片然走。...
    茶點故事閱讀 39,722評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖戏挡,靈堂內(nèi)的尸體忽然破棺而出芍瑞,到底是詐尸還是另有隱情,我是刑警寧澤褐墅,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布拆檬,位于F島的核電站,受9級特大地震影響掌栅,放射性物質(zhì)發(fā)生泄漏秩仆。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一猾封、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧噪珊,春花似錦晌缘、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至阵难,卻和暖如春岳枷,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背呜叫。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工空繁, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人朱庆。 一個月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓盛泡,卻偏偏與公主長得像,于是被迫代替她去往敵國和親娱颊。 傳聞我的和親對象是個殘疾皇子傲诵,可洞房花燭夜當晚...
    茶點故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,401評論 0 17
  • 前端開發(fā)面試題 面試題目: 根據(jù)你的等級和職位的變化箱硕,入門級到專家級拴竹,廣度和深度都會有所增加。 題目類型: 理論知...
    怡寶丶閱讀 2,580評論 0 7
  • 『和橋講堂』百貨剧罩、餐飲栓拜、超市等各類業(yè)態(tài)的開店要求和選址條件 http://maxonesoft.com/onesh...
    波波叔閱讀 373評論 0 0
  • 11.12。昨天是星期天,蔣美美連續(xù)在學(xué)校補課兩天菱属,于是上午的古琴課又暫停了钳榨,這離我上次去琴館一晃都一個月了,萬老...
    張漪紋閱讀 777評論 0 1