Flower_gift 簡(jiǎn)單的Flutter實(shí)戰(zhàn)app(三)

0001.jpg
項(xiàng)目git地址:flower_gift
接著把首頁寫完

效果:


3666.gif
第一個(gè)單列ListView
 import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../model/homePage_model_entity.dart';
import 'package:provide/provide.dart';
import '../../provide/homePage_provide.dart';

class HomeFlowerSingleList extends StatelessWidget {
  final int sectionIndex;
  HomeFlowerSingleList(this.sectionIndex);
  @override
  Widget build(BuildContext context) {
   return Provide<HomePageProvide>(
       
        builder: (context,child,val){
          var sectionData;
          if(sectionIndex == 1){
            sectionData = val.homePageModelEntity.section1;
          }else{
            sectionData = val.homePageModelEntity.section2;
          }   
            return Container(
              color: Color.fromRGBO(240, 240, 245, 1.0),
              child: Column(
                children: <Widget>[
                  _titleWidget(sectionData),
                  SizedBox(height: 10.0,),
                  _listView(sectionData,context),
                  _moreBtnWidget(sectionData)
                ],
              )
            );
      }
    );
  }

  //標(biāo)題
  Widget _titleWidget(HomePageModelSection sectionData){
      
      return Container(
         color:Colors.white,
         margin: EdgeInsets.only(top: 10.0),
         height: ScreenUtil().setHeight(80.0),
         alignment: Alignment.center,
         child: Text(
            sectionData.title,
            style: TextStyle(
              fontSize: ScreenUtil().setSp(30.0),
              fontWeight: FontWeight.w600
            ),
         ),
      );
  }

 //查看更多按鈕
 Widget _moreBtnWidget(HomePageModelSection sectionData){
     return Container(
         color:Colors.white,
       
         height: ScreenUtil().setHeight(100.0),
         alignment: Alignment.center,
         child: RaisedButton(
           onPressed: (){
             print("查看更多...");
           },
           child: Text("查看更多"),
         )
      );
 }

  //listView
  Widget _listView(HomePageModelSection sectionData,BuildContext context){
     
      return Padding(
        padding: const EdgeInsets.only(left: 5.0,right: 5.0),
        child: Container(
            color:Color.fromRGBO(240, 240, 245, 1.0),
            child:Container(
                color: Colors.white,
                child: Column(
                    children:_listViewLists(sectionData,context)
                ) ,
              ) 
              
            ),
      ); 
      
  }

  List<Widget> _listViewLists(HomePageModelSection sectionData,BuildContext context){
       List<Widget> list = new List();
     for(var i = 0; i < sectionData.xList.length; i++){
       list.add(
          _listItem(sectionData.xList[i],context) 
       ); 
       
     }
     return list;

  }

  Widget _listItem(HomePageModelSectionList sectionListItem,BuildContext context){
     return Container(
        decoration: BoxDecoration(
           border: Border(bottom: BorderSide(width: 0.5,color: Colors.black26))
        ),
        child: Padding(
       padding: const EdgeInsets.only(top: 10.0,bottom: 15.0),
       child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
             SizedBox(width: 10.0,),
             //左邊圖片
             Container(
               height: MediaQuery.of(context).size.width / 3 + 30.0,
               width: MediaQuery.of(context).size.width / 3 + 10.0,
               child: ClipRRect(
                 borderRadius: BorderRadius.circular(8.0),
                 child: Image.network(sectionListItem.image,fit: BoxFit.cover,),
               ) 
               
             ),
             SizedBox(width: 25.0,),
             //右邊文字
             Container(
               child: Column(
                 crossAxisAlignment: CrossAxisAlignment.start,
                 children: <Widget>[
                    //標(biāo)題
                    Text(sectionListItem.title,
                        textAlign: TextAlign.left,
                           style:TextStyle(fontSize: ScreenUtil().setSp(40.0),
                           fontWeight: FontWeight.w300,
                         )
                      ),
                      SizedBox(height: 10.0,),
                      //詳情
                      Container(
                        width: MediaQuery.of(context).size.width/2 - 20.0,
                        child: Text(sectionListItem.detail,
                            maxLines:2,
                            overflow:TextOverflow.ellipsis
                          ),
                      ),
                       SizedBox(height: 10.0,),
                      //評(píng)語
                      Container(
                        width: MediaQuery.of(context).size.width/2 - 20.0,
                        padding: const EdgeInsets.symmetric(vertical: 10.0),
                        decoration: BoxDecoration(
                          //只添加上下邊框
                          border: Border(top: BorderSide(width: 0.5,color: Colors.black26),bottom: BorderSide(width: 0.5,color: Colors.black26))
                        ),
                          child: Text(sectionListItem.info,
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(fontWeight: FontWeight.w600),
                        ),
                      ),
                      SizedBox(height: 10.0,),
                      //價(jià)格:
                      Row(
                          children: <Widget>[
                            Text("¥${sectionListItem.price}",
                            style: TextStyle(color: Colors.orange,fontSize: ScreenUtil().setSp(36.0)),
                            ),
                            SizedBox(width: 10.0,),
                            Text("¥${sectionListItem.linePrice}",
                                    style: TextStyle(
                                        color: Colors.black26,
                                        decoration: TextDecoration.lineThrough
                                    ),
                             ),
                          ],
                       ),
                       SizedBox(height: 10.0,),
                       //銷量和購物車
                       Container(
                         width: MediaQuery.of(context).size.width/2,
                         child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Text("已銷售${sectionListItem.salesRank}件",
                                          style: TextStyle(
                                          color: Colors.black12,
                                      ),
                              ),
                              InkWell(
                                onTap: (){
                                  print("點(diǎn)擊加入購物車");
                                },
                                child:Icon(Icons.shopping_cart)
                              )
                            ],
                          ),
                       )
                   ],
               ),
             )
          ],
       ),
     ),
  ); 
      
     
    
  }
 //左邊圖片
  Widget _leftImage(HomePageModelSectionList sectionListItem){
    return ClipRRect(
      borderRadius: BorderRadius.circular(8.0),
      child: Container(
            width: 100.0,
            child: Image.network(sectionListItem.image,
            fit: BoxFit.cover,
            
            ),
        ),
    );
    
    
  }
  //右邊文字布局
  Widget _rightText(HomePageModelSectionList sectionListItem){
        return Container(
            padding: EdgeInsets.only(left: 30.0,right: 20.0,top: 10.0),
            width: ScreenUtil().setWidth(400.0),
            child: AspectRatio(
              aspectRatio: 0.9,
              child: Column(
               children: <Widget>[
                  Container(
                    alignment: Alignment.centerLeft,
                    child:Text(sectionListItem.title,
                        textAlign: TextAlign.left,
                        style:TextStyle(fontSize: ScreenUtil().setSp(36.0),
                          fontWeight: FontWeight.w300,
                         )
                      ),
                  ),
                  
                  Container(
                    margin: EdgeInsets.only(top: 15.0),
                    child:  Text(sectionListItem.detail,
                        maxLines:2,
                        overflow:TextOverflow.ellipsis
                      ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 10.0),
                    padding: EdgeInsets.only(top: 12.0,bottom: 12.0),
                    decoration: BoxDecoration(
                      //只添加上下邊框
                      border: Border(top: BorderSide(width: 0.5,color: Colors.black26),bottom: BorderSide(width: 0.5,color: Colors.black26))
                    ),
                     alignment: Alignment.centerLeft,
                     child: Text(sectionListItem.info,
                       maxLines: 1,
                       overflow: TextOverflow.ellipsis,
                       style: TextStyle(fontWeight: FontWeight.w600),
                     ),
                  ),
                  
                  Container(
                    margin: EdgeInsets.only(top: 10.0),
                    child: Row(
                      children: <Widget>[
                        Text("¥${sectionListItem.price}",
                         style: TextStyle(color: Colors.orange,fontSize: ScreenUtil().setSp(35.0)),
                        ),
                        Container(
                           margin: EdgeInsets.only(left: 10.0),
                           child:  Text("¥${sectionListItem.linePrice}",
                                style: TextStyle(
                                    color: Colors.black26,
                                    decoration: TextDecoration.lineThrough
                                ),
                            ) ,
                        ),
                        
                      ],
                    ),
                  ),
                  
                   Container(
                     alignment: Alignment.centerLeft,
                     margin: EdgeInsets.only(top: 10.0),
                     child: Text("已銷售${sectionListItem.salesRank}件",
                        style: TextStyle(
                          color: Colors.black12,
                          fontSize: ScreenUtil().setSp(22.0)
                        ),
                      ),
                   )  
                 ],
              ),
            ) 
       );
  }
}

這種單列ListView,然后又分為左右兩部分的情況,注意這個(gè)布局方式就好,我的思路是左邊圖片我就讓它寬度為屏幕的一半(去掉間隔),高度就為寬度的1.2倍左右。 再有就是右邊文字布局,這種就只需要給Container限定寬度,然后Column單列布局下來,就讓其中的widget自動(dòng)將這個(gè)這個(gè)ListViewItem高度撐起來。比以前iOS中用這個(gè)xib做布局,要簡(jiǎn)單些。

第二個(gè)雙列ListView
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../model/homePage_model_entity.dart';
import 'package:provide/provide.dart';
import '../../provide/homePage_provide.dart';

class HomeFlowerDoubleList extends StatelessWidget {
  final int sectionIndex;
  HomeFlowerDoubleList(this.sectionIndex);

  @override
  Widget build(BuildContext context) {
    return Provide<HomePageProvide>(
       
        builder: (context,child,val){
          var sectionData;
          if(sectionIndex == 3){
            sectionData = val.homePageModelEntity.section3;
          }else if(sectionIndex == 4){
            sectionData = val.homePageModelEntity.section4;
          }else if(sectionIndex == 5){
            sectionData = val.homePageModelEntity.section5;
          }else{
            sectionData = val.homePageModelEntity.section6;
          }   
            return Container(
              color: Color.fromRGBO(240, 240, 245, 1.0),
              child: Column(
                children: <Widget>[
                  _titleWidget(sectionData),
                  _listView(sectionData),
                  _moreBtnWidget(sectionData)
                ],
              )
            );
      }
    );
  }

  //標(biāo)題
  Widget _titleWidget(HomePageModelSection sectionData){
      
      return Container(
         color:Colors.white,
         margin: EdgeInsets.only(top: 10.0),
         height: ScreenUtil().setHeight(80.0),
         alignment: Alignment.center,
         child: Text(
            sectionData.title,
            style: TextStyle(
              fontSize: ScreenUtil().setSp(30.0),
              fontWeight: FontWeight.w600
            ),
         ),
      );
  }

 //查看更多按鈕
 Widget _moreBtnWidget(HomePageModelSection sectionData){
     return Container(
         color:Colors.white,
         margin: EdgeInsets.only(top: 10.0),
         height: ScreenUtil().setHeight(100.0),
         alignment: Alignment.center,
         child: RaisedButton(
           onPressed: (){
             print("查看更多...");
           },
           child: Text("查看更多"),
         )
      );
 }

  //listView
  Widget _listView(HomePageModelSection sectionData){
     
      return Container(
         color:Color.fromRGBO(240, 240, 245, 1.0),
         
         child: Wrap(
            children:_listViewLists(sectionData)
         )
      );
  }

  List<Widget> _listViewLists(HomePageModelSection sectionData){
       List<Widget> list = new List();
     for(var i = 0; i < sectionData.xList.length; i++){
       list.add(
          _listItem(sectionData.xList[i]) 
       ); 
       
     }
     return list;

  }

  Widget _listItem(HomePageModelSectionList sectionListItem){
     return InkWell(
        onTap: (){
          print("點(diǎn)擊了列表Item");
        },
        
          child: Container(
              color: Colors.white,
              padding: EdgeInsets.all(10.0),
              margin: EdgeInsets.only(top: 5.0),
              height: ScreenUtil().setHeight(600.0),
              child: Column(
                  children: <Widget>[
                    _topImage(sectionListItem),
                    Stack(
                       alignment: const FractionalOffset(1, 1),
                       children: <Widget>[
                           _bottomText(sectionListItem),
                           InkWell(
                             onTap: (){
                               print("點(diǎn)擊加入購物車");
                             },
                            child:Icon(Icons.shopping_cart)
                           )
                       ],
                    )
                    
                  ],
              ),
          )
         
     );
  }
 //左邊圖片
  Widget _topImage(HomePageModelSectionList sectionListItem){
    return ClipRRect(
      borderRadius: BorderRadius.circular(8.0),
      child: Container(
            width: ScreenUtil().setWidth(330),
            child: Image.network(sectionListItem.image,
            fit: BoxFit.fitWidth,
            width: ScreenUtil().setWidth(330),
            ),
        ),
    );
    
    
  }
  //右邊文字布局
  Widget _bottomText(HomePageModelSectionList sectionListItem){
         bool isShowLabel;
         isShowLabel = sectionListItem.label.length > 0 ? true : false;
        return Container(
           // padding: EdgeInsets.only(left: 30.0,right: 20.0,top: 10.0),
            width: ScreenUtil().setWidth(330.0),
            child: Column(
               children: <Widget>[
                  Container(
                    margin: EdgeInsets.only(top: 5.0),
                    alignment: Alignment.centerLeft,
                    child:Text(sectionListItem.detail,
                        textAlign: TextAlign.left,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style:TextStyle(fontSize: ScreenUtil().setSp(28.0),
                          fontWeight: FontWeight.w300,
                         )
                      ),
                  ),
                  //是否顯示小標(biāo)簽
                  isShowLabel ? ClipRRect(
                     borderRadius: BorderRadius.circular(20.0),
                     child: Container(
                          padding: EdgeInsets.all(5.0),
                          alignment: Alignment.center,
                          width: ScreenUtil().setWidth(160.0),
                          height: ScreenUtil().setHeight(50.0),
                          margin: EdgeInsets.only(top: 10.0),
                          decoration: BoxDecoration(
                            border: Border.all(color: Colors.orange,width: 1.0)
                          ),
                          child: Text(sectionListItem.label.first,
                           style: TextStyle(color: Colors.orange,fontSize: ScreenUtil().setSp(22.0)),
                          ),
                      ) ,
                    )
                  : Container(
                     height: ScreenUtil().setHeight(5.0),
                     color: Colors.white,
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 10.0),
                    child: Row(
                      children: <Widget>[
                        Text("¥${sectionListItem.price}",
                         style: TextStyle(color: Colors.orange,fontSize: ScreenUtil().setSp(35.0)),
                        ),
                        Container(
                           margin: EdgeInsets.only(left: 10.0),
                           child:  Text("¥${sectionListItem.linePrice}",
                                style: TextStyle(
                                    color: Colors.black26,
                                    decoration: TextDecoration.lineThrough
                                ),
                            ) ,
                        ),
                        
                      ],
                    ),
                  ),
                  
                   Container(
                     alignment: Alignment.centerLeft,
                     margin: EdgeInsets.only(top: 10.0),
                     child: Text("已銷售${sectionListItem.salesRank}件",
                        style: TextStyle(
                          color: Colors.black12,
                          fontSize: ScreenUtil().setSp(22.0)
                        ),
                      ),
                   )  
                 ],
            ),
        );
  }

}

這就沒什么好說的了,Row加入到ListView中就好了。

接下來就是第二個(gè)tabBar界面分類界面的編寫。分類界面
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,572評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,071評(píng)論 3 414
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,409評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,569評(píng)論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,360評(píng)論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,895評(píng)論 1 321
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,979評(píng)論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,123評(píng)論 0 286
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,643評(píng)論 1 333
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,559評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,742評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,250評(píng)論 5 356
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 43,981評(píng)論 3 346
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,363評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,622評(píng)論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,354評(píng)論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,707評(píng)論 2 370