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

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

效果:


3666.gif
第一個單列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,),
                      //評語
                      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,然后又分為左右兩部分的情況法严,注意這個布局方式就好究抓,我的思路是左邊圖片我就讓它寬度為屏幕的一半(去掉間隔)坚弱,高度就為寬度的1.2倍左右炮车。 再有就是右邊文字布局,這種就只需要給Container限定寬度苟鸯,然后Column單列布局下來袱讹,就讓其中的widget自動將這個這個ListViewItem高度撐起來疲扎。比以前iOS中用這個xib做布局,要簡單些捷雕。

第二個雙列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中就好了。

接下來就是第二個tabBar界面分類界面的編寫救巷。分類界面
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末壶熏,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子浦译,更是在濱河造成了極大的恐慌棒假,老刑警劉巖,帶你破解...
    沈念sama閱讀 210,978評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件精盅,死亡現(xiàn)場離奇詭異帽哑,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)叹俏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,954評論 2 384
  • 文/潘曉璐 我一進(jìn)店門妻枕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人粘驰,你說我怎么就攤上這事屡谐。” “怎么了蝌数?”我有些...
    開封第一講書人閱讀 156,623評論 0 345
  • 文/不壞的土叔 我叫張陵康嘉,是天一觀的道長。 經(jīng)常有香客問我籽前,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,324評論 1 282
  • 正文 為了忘掉前任枝哄,我火速辦了婚禮肄梨,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘挠锥。我一直安慰自己众羡,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,390評論 5 384
  • 文/花漫 我一把揭開白布蓖租。 她就那樣靜靜地躺著粱侣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蓖宦。 梳的紋絲不亂的頭發(fā)上齐婴,一...
    開封第一講書人閱讀 49,741評論 1 289
  • 那天,我揣著相機(jī)與錄音稠茂,去河邊找鬼柠偶。 笑死,一個胖子當(dāng)著我的面吹牛睬关,可吹牛的內(nèi)容都是我干的诱担。 我是一名探鬼主播,決...
    沈念sama閱讀 38,892評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼电爹,長吁一口氣:“原來是場噩夢啊……” “哼蔫仙!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起丐箩,我...
    開封第一講書人閱讀 37,655評論 0 266
  • 序言:老撾萬榮一對情侶失蹤摇邦,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后雏蛮,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體涎嚼,經(jīng)...
    沈念sama閱讀 44,104評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,451評論 2 325
  • 正文 我和宋清朗相戀三年挑秉,在試婚紗的時候發(fā)現(xiàn)自己被綠了法梯。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,569評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡犀概,死狀恐怖立哑,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情姻灶,我是刑警寧澤铛绰,帶...
    沈念sama閱讀 34,254評論 4 328
  • 正文 年R本政府宣布,位于F島的核電站产喉,受9級特大地震影響捂掰,放射性物質(zhì)發(fā)生泄漏敢会。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,834評論 3 312
  • 文/蒙蒙 一这嚣、第九天 我趴在偏房一處隱蔽的房頂上張望鸥昏。 院中可真熱鬧,春花似錦姐帚、人聲如沸吏垮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,725評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽膳汪。三九已至,卻和暖如春九秀,著一層夾襖步出監(jiān)牢的瞬間遗嗽,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,950評論 1 264
  • 我被黑心中介騙來泰國打工颤霎, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留媳谁,地道東北人。 一個月前我還...
    沈念sama閱讀 46,260評論 2 360
  • 正文 我出身青樓友酱,卻偏偏與公主長得像晴音,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子缔杉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,446評論 2 348