Flutter-仿京東項目總結

1、tab與路由配置

在routers/router.dart中, 配置路由:

import 'dart:js';

import 'package:flutter/material.dart';
import 'package:newsmth/pages/test/tabs/tabs.dart';

// 配置路由
final routes = {
  '/': (context) => const Tabs(),
  '/search': (context) => const SearchPage(),
};

//固定寫法
var onGenerateRoute = (RouteSettings settings) {
  //統一處理
  final String? name = settings.name;
  final Function pageContentBuilder = routes[name] as Function;
  if (pageContentBuilder != null) {
    if (settings.arguments != null) {
      final Route route = MaterialPageRoute(
          builder: (context) =>
              pageContentBuilder(context, arguments: settings.arguments));
      return route;
    } else {
      final Route route =
          MaterialPageRoute(builder: (context) => pageContentBuilder(context));
      return route;
    }
  }
};

在tabs/tabs.dart中, 設置tab:

import 'package:flutter/material.dart';
import 'package:newsmth/pages/favorite/index.dart';
import 'package:newsmth/pages/home/index.dart';
import 'package:newsmth/pages/message/index.dart';
import 'package:newsmth/pages/my/index.dart';

class Tabs extends StatefulWidget {
  const Tabs({Key? key}) : super(key: key);

  @override
  _TabsState createState() => _TabsState();
}

class _TabsState extends State<Tabs> {
  int _currentIndex = 0;
  final List _pageList = [
    const HomeView(),
    const FavoriteView(),
    const MessageView(),
    const MyView(),
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("jdshop"),
      ),
      body: _pageList[_currentIndex], //tab對應頁面
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index) => {
          setState(() {
            _currentIndex = index;
          })
        },
        type: BottomNavigationBarType.fixed, //此配置可以實現展示多個tab
        fixedColor: Colors.red,//tab選中顏色
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: "首頁",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.category),
            label: "分類",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.shopping_cart_outlined),
            label: "購物車",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.people),
            label: "我的",
          )
        ],
      ),
    );
  }
}

在main.dart中使用Tab和路由配置:

import 'routers/router.dart';
...
class _myAppState extends State<MyApp>{
  @override
  Widget build(BuildContext context){
    return MaterialApp(
      initialRoute: '/', //初始路由
        onGenerateRoute: onGenerateRoute
    )
  }
}

在homeView.dart中, 測試路由跳轉:

RaisedButton(
    child: Text("跳轉到搜索"),
  onPressed:(){
    Navigator.pushName(context, '/search');
  }
)

2、首頁布局

使用插件flutter_swiper, 來實現輪播圖

dependencies:
    flutter_swiper: ^1.16 # 輪播圖
  flutter_screenutil: ^5.0.3 # 屏幕適配

在home.dart 中, 使用

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class CartPage extends StatefulWidget {
  const CartPage({Key? key}) : super(key: key);

  @override
  _CartPageState createState() => _CartPageState();
}

class _CartPageState extends State<CartPage> {
  //輪播圖
  Widget _swiperWidget() {
    List<Map> imgList = [
      {"url": "https://www.ityiing.com/images/flutter/slide01.jpg"},
      {"url": "https://www.ityiing.com/images/flutter/slide02.jpg"},
      {"url": "https://www.ityiing.com/images/flutter/slide03.jpg"},
    ];
    return Container(
      child: AspectRatio(
        aspectRatio: 2 / 1, //設置寬高比
        child: Swiper(
          itemBuilder: (BuildContext context, int index) {
            return Image.network(
              imgList[index]["url"],
              fit: BoxFit.fill,
            );
          },
          itemCount: imgList.length,
          pagination: const SwiperPagination(),
          control: const SwiperControl(),
          autoplay: true, //自動輪播
        ),
      ),
    );
  }

  Widget _titleWidget(value) {
    return Container(
      height: 34.h,
      margin:  EdgeInsets.only(left: 10.w),
      padding: EdgeInsets.only(left: 10.w),
      //設置左側邊框
      decoration:  BoxDecoration(
        border: Border(
          left: BorderSide(
            color: Colors.red,
            width: 10.w,
          ),
        ),
      ),
      child: Text(
        value,
        style: const TextStyle(
          color: Colors.black54,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        _swiperWidget(),
        _titleWidget("猜你喜歡"),
        SizedBox(height: 10.sp),
        _titleWidget("熱門推薦"),
        SizedBox(height: 10.sp),
      ],
    );
  }
}

3、封裝適配庫以實現左右滑動Listview

首頁-熱門商品

//熱門商品
  Widget _hotProductList() {
    return Container(
      height: 240.h,
      // width: double.infinity, //要占用整個寬度或高度
      padding: EdgeInsets.all(20.w),
      child: ListView.builder(
        scrollDirection: Axis.horizontal, //水平滾動
        itemBuilder: (context, index) {
          return Column(
            children: [
              Container(
                height: 140.h,
                width: 140.w,
                margin: EdgeInsets.only(right: 21.w),
                child: Image.network(
                  "https://www.itying.com/images/flutter/hot${index + 1}.jpg",
                  fit: BoxFit.cover, //圖片適配容器寬高
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 10.h),
                child: Text("第$index條"),
                height: 44.h,
              )
            ],
          );
        },
        itemCount: 80,
      ),
    );
  }

4、網格布局

首頁商品列表

    //推薦商品
  Widget _recProductItemWidget() {
    var itemWidth = (1.sw - 30) / 2;
    //沒設置高度, 高度會自適應
    return Container(
      padding: const EdgeInsets.all(5),
      width: itemWidth,
      decoration: BoxDecoration(
          //邊框
          border: Border.all(
        color: Colors.black12,
        width: 1,
      )),
      child: Column(
        children: [
          SizedBox(
            width: double.infinity,
            child: AspectRatio(
              aspectRatio: 1/1,//防止服務器返回圖片寬度不一致,導致高度不一致
              child: Image.network(
                "https://www.itying.com/images/flutter/list1.jpg",
                fit: BoxFit.cover, //圖片適配容器寬高
              ),
            ),
          ),
          Padding(
            padding: EdgeInsets.only(top: 10.h),
            child: const Text(
              "2019夏季新款",
              maxLines: 2,
              overflow: TextOverflow.ellipsis, //超出限制...
              style: TextStyle(color: Colors.black54),
            ),
          ),
          Padding(
            padding: EdgeInsets.only(top: 20.h),
            //左右布局
            child: Stack(
              children: [
                Align(
                  alignment: Alignment.centerLeft,
                  child: Text(
                    "¥188.0",
                    style: TextStyle(color: Colors.red, fontSize: 16.sp),
                  ),
                ),
                Align(
                  alignment: Alignment.centerRight,
                  child: Text(
                    "¥198.0",
                    style: TextStyle(
                        color: Colors.black54,
                        fontSize: 14.sp,
                        decoration: TextDecoration.lineThrough),//配置一個下劃線
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

    Widget _recProductListWidget() {
    return Container(
      padding: EdgeInsets.all(10),
      child: Wrap(
        runSpacing: 10, //主軸間距
        spacing: 10, //副軸間距
        children: [
          _recProductItemWidget(),
        ],
      ),
    );
  }

5、JSON轉對象

一、在線方式

1、JSON to Dart
2、quicktype (推薦)

二、插件工具 (推薦)

1、FlutterJsonBeanFactory
2、json_serializable 和 build_runner

7、商品分類頁面布局--左右菜單

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class Category extends StatefulWidget {
  Category({Key? key}) : super(key: key);

  @override
  State<Category> createState() => _CategoryState();
}

class _CategoryState extends State<Category> {
  @override
  Widget build(BuildContext context) {
    var _selectedIndex = 0;

    //計算左側寬度
    var leftWidth = 1.sw / 4;
    //右側每一項寬度=(總寬度-左測寬度-GridView外側原生左右的Padding值-GridView中間的間距)/3
    var rightItemWidth = (1.sw - leftWidth - 20 - 20) / 3;
    rightItemWidth = rightItemWidth.w;
    var rightItemHeight = rightItemWidth + 28.h;

    return Row(children: [
      //左側視圖
      Container(
        width: leftWidth,
        height: double.infinity,
        color: Colors.red,
        child: ListView.builder(
          itemCount: 18,
          itemBuilder: (context, index) {
            return Column(
              children: [
                // InkWell當做"按鈕"組件用
                InkWell(
                  onTap: () {
                    setState(() {
                      _selectedIndex = index;
                    });
                  },
                  child: Container(
                    width: double.infinity,
                    height: 84.h,
                    padding: EdgeInsets.only(top: 24.h),
                    child: Text("第$index條", textAlign: TextAlign.center),
                    color: _selectedIndex == index ? Colors.red : Colors.white,
                  ),
                ),
                const Divider(height: 1) //如果不設置高度, 默認高度是16
              ],
            );
          },
        ),
      ),
      //右側視圖
      Expanded(
        flex: 1,
        child: Container(
          padding: EdgeInsets.all(10.h),
          height: double.infinity,
          color: const Color.fromRGBO(240, 240, 240, 0.9),
          child: GridView.builder(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3, //每行數量
              childAspectRatio: rightItemWidth / rightItemHeight, //寬高比
              crossAxisSpacing: 10, //縱軸間距
              mainAxisSpacing: 10, //主軸間距
            ),
            itemBuilder: (context, index) {
              return Container(
                child: Column(
                  children: [
                    AspectRatio(
                      aspectRatio: 1 / 1,
                      child: Image.network(
                        "https://www.itying.com/images/flutter/list1.jpg",
                        fit: BoxFit.cover,
                      ),
                    ),
                    Container(
                      height: 28.h,
                      child:  Text("女裝"),
                    )
                  ],
                ),
              );
            },
            itemCount: 18,
          ),
        ),
      ),
    ]);
  }
}

9、底部Tab切換保持頁面狀態

IndexedStack 保持頁面狀態

IndexedStack 和 Stack 一樣,都是層布局控件, 可以在一個控件上面放置另一 個控件,但唯一不同的是 IndexedStack 在同一時刻只能顯示子控件中的一個控 件,通過 Index 屬性來設置顯示的控件。

IndexedStack 來保持頁面狀態的優點就是配置簡單。IndexedStack 保持頁面狀 態的缺點就是不方便單獨控制每個頁面的狀態。

IndexedStack 用法:

Container(
  width: double.infinity,
  height: double.infinity,
  child: new IndexedStack(
    index: 0,
    alignment: Alignment.center,
    children: <Widget>[
      Image.network(
        "https://www.itying.com/images/flutter/list1.jpg",
        fit: BoxFit.cover,
      ),
      Image.network("https://www.itying.com/images/flutter/list2.jpg",
          fit: BoxFit.cover)
    ],
  ),
);

注: IndexedStack會一次性加載所有頁面, 不方便控制所有頁面狀態

AutomaticKeepAliveClientMixin 保持頁面狀態 ?

AutomaticKeepAliveClientMixin 結合 tab 切換保持頁面狀態相比 IndexedStack 而言配置起來稍 微有些復雜。它結合底部 BottomNavigationBar 保持頁面狀態的時候需要進行如下配置。

import 'package:flutter/material.dart';
import 'package:newsmth/pages/favorite/index.dart';
import 'package:newsmth/pages/home/index.dart';
import 'package:newsmth/pages/message/index.dart';
import 'package:newsmth/pages/my/index.dart';

class Tabs extends StatefulWidget {
  const Tabs({Key? key}) : super(key: key);

  @override
  _TabsState createState() => _TabsState();
}

class _TabsState extends State<Tabs> {
  //sp1.初始化PageController
  late PageController _pageController;
  int _currentIndex = 0;
  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: _currentIndex);
  }

  final List<Widget> _pageList = [
    const HomeView(),
    const FavoriteView(),
    const MessageView(),
    const MyView(),
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("jdshop"),
      ),
      body: PageView(//sp2.設置tab對應頁面
        controller: _pageController,
        children: _pageList,
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index) => {
          setState(() {
            _currentIndex = index;
            _pageController.jumpToPage(index);//sp3.設置頁面切換
          })
        },
        type: BottomNavigationBarType.fixed, //此配置可以實現展示多個tab
        fixedColor: Colors.red, //選中顏色
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: "首頁",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.category),
            label: "分類",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.shopping_cart_outlined),
            label: "購物車",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.people),
            label: "我的",
          )
        ],
      ),
    );
  }
}

需要持久化的頁面加入如下代碼:

class HomePage extends StatefulWidget {
  HomePage({Key? key}) : super(key: key);
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin { //sp5.繼承AutomaticKeepAliveClientMixin 來保持狀態
  @override
  bool get wantKeepAlive => true;//sp6. 返回true
}

10、商品列表布局

注意:如果Container里面加上decoration屬性, 這個時候color屬性必須放在BoxDecoration里面

class _ProductListPageState extends State<ProductListPage> {
  Widget _productListWidget() {
    return Container(
      padding: EdgeInsets.all(10),
      margin: EdgeInsets.only(top: 80.h),
      child: ListView.builder(
        itemBuilder: (context, index) {
          //每一個元素
          return Column(
            children: [
              //左邊圖片
              Container(
                width: 180.w,
                height: 180.h,
                child: Image.network("圖片地址"),
              ),
              //右側內容
              Expanded(
                  flex: 1,
                  child: Container(
                    height: 180.h, //此時要設置高度, 否則子組件內容無法撐滿容器
                    margin: EdgeInsets.only(left: 10),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [],
                    ),
                  ))
            ],
          );
        },
        itemCount: 10,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("商品列表"),
        ),
        body: _productListWidget()
    );
  }
}

11、商品列表頁面二級篩選導航布局

自定義Tab導航:

  Widget _subHeaderWidget() {
    return Positioned(
      top: 0,
      height: 80.h,
      width: 750.w,
      child: Container(
        height: 80.h,
        width: 750.w,
        color: Colors.red,
        child: Row(
          children: [
            Expanded(
              flex: 1,
              child: InkWell(
                child: Padding(
                  padding: EdgeInsets.fromLTRB(0, 16.h, 0, 16.h),
                  child: Text("綜合", textAlign: TextAlign.center),
                ),
                onTap: () {},
              ),
            ),
            Expanded(
              flex: 1,
              child: InkWell(
                child: Padding(
                  padding: EdgeInsets.fromLTRB(0, 16.h, 0, 16.h),
                  child: Text("價格", textAlign: TextAlign.center),
                ),
                onTap: () {},
              ),
            ),
            Expanded(
              flex: 1,
              child: InkWell(
                child: Padding(
                  padding: EdgeInsets.fromLTRB(0, 16.h, 0, 16.h),
                  child: Text("篩選", textAlign: TextAlign.center),
                ),
                onTap: () {},
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("商品列表"),
      ),
      body: Stack(
        children: [
          _productListWidget(),
          _subHeaderWidget(),
        ],
      ),
    );
  }

實現篩選功能--側邊欄彈框:

final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();   //sp1
@override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,//sp2
      appBar: AppBar(
        title: Text("商品列表"),
        actions: [
          Text("")//sp4.去掉導航欄默認添加的調出側邊欄按鈕
        ],
      ),
      endDrawer: Drawer(//sp3.側邊欄彈框
        child: Container(
          child: Text("側邊欄彈框內容布局"),
        ),
      ),
      body: Stack(
        children: [
          _productListWidget(),
          _subHeaderWidget(),
        ],
      ),
    );
  }

通過事件打開側邊欄

InkWell(
  onTap: () {
  //注意:新版本的 Flutter 中 ScaffoldState? 為可空類型 注意判斷 
  if(_scaffoldKey.currentState!=null){
    _scaffoldKey.currentState!.openEndDrawer();
  },
  child: Text("篩選", textAlign: TextAlign.center),
),

12、上拉刷新監聽

class _ProductListPageState extends State<ProductListPage> {
  Widget _productListWidget() {
    return Container(
      padding: EdgeInsets.all(10),
      margin: EdgeInsets.only(top: 80.h),
      child: ListView.builder(
        controller: _scrollController, //sp1.
        ...
   }
        
        
   final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  //sp2.用于上拉分頁
  ScrollController _scrollController = ScrollController(); //listview的控制器
  //分頁
  int _page = 1;
  //數據
  List _productList = [];
  //排序
  String _sort = "";
  //解決重復請求的問題
  bool flag = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _scrollController.addListener(() {//sp3.監聽滾動
      //_scrollController.position.pixels //獲取滾動條滾動高度
      //_scrollController.position.maxScrollExtent //獲取頁面高度
      if (_scrollController.position.pixels >
          _scrollController.position.maxScrollExtent - 20) {
        if (flag) {
          _getProductListData();
        }
      }
    });
  }
}

注: _scrollController.jumpTo(0); //回到頂部

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,247評論 6 543
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,520評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,362評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,805評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,541評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,896評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,887評論 3 447
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,062評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,608評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,356評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,555評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,077評論 5 364
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,769評論 3 349
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,175評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,489評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,289評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,516評論 2 379

推薦閱讀更多精彩內容