【Flutter for OpenHarmony 】第三方库鸿蒙电商实战|首页模块完整实现[特殊字符]

📱 Flutter for OpenHarmony 鸿蒙电商实战|首页模块完整实现🔥

欢迎加入开源鸿蒙跨平台社区:https://openharmonyeressplatform.csdn.net

哈喽各位小伙伴👋!我是正在肝Flutter跨鸿蒙开发 的大学生!

上一篇给大家肝完了鸿蒙电商搜索栏 ,这篇直接带来电商APP灵魂------首页模块的完整源码✨

这篇文章纯原生Flutter、无冗余依赖、完美适配OpenHarmony,代码直接复制就能跑,


🎯 先看实现效果!1:1还原设计图

本次实现的鸿蒙电商首页功能超齐全👇

  • 顶部渐变搜索栏(带城市+相机图标)🔍
  • 炫酷轮播Banner(新人优惠+渐变背景)📢
  • 10大商品分类(图标+文字,支持点击)📦
  • 为你推荐商品流(双列网格+销量+价格)🛒
  • 底部导航栏(首页/分类/购物车/我的)🧭
  • 全页面鸿蒙适配,无卡顿、无溢出、超流畅!

🧰 开发环境(大学生零门槛)

  • Flutter 3.x + Flutter for OpenHarmony
  • DevEco Studio(鸿蒙专属IDE)
  • 鸿蒙模拟器 / 鸿蒙真机
  • Dart基础语法(会复制就能跑!)

🔥 完整源码(可直接运行)

我把代码拆成5大模块 ,注释超详细,大学生一看就懂~

新建文件:lib/pages/home_page.dart

dart 复制代码
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // 当前选中的底部导航索引
  int _selectedIndex = 0;

  // 轮播图数据(模拟)
  final List<Map<String, dynamic>> _banners = [
    {'color': Colors.orange.shade400, 'text': '限时特惠'},
    {'color': Colors.blue.shade400, 'text': '新品上市'},
    {'color': Colors.green.shade400, 'text': '热卖推荐'},
  ];

  // 商品分类数据(10个,还原设计图)
  final List<Map<String, dynamic>> _categories = [
    {'name': '数码', 'icon': Icons.phone_android, 'color': Colors.blue},
    {'name': '服饰', 'icon': Icons.checkroom, 'color': Colors.purple},
    {'name': '美食', 'icon': Icons.restaurant, 'color': Colors.orange},
    {'name': '美妆', 'icon': Icons.face, 'color': Colors.pink},
    {'name': '母婴', 'icon': Icons.child_care, 'color': Colors.amber},
    {'name': '家电', 'icon': Icons.tv, 'color': Colors.teal},
    {'name': '箱包', 'icon': Icons.work, 'color': Colors.brown},
    {'name': '生鲜', 'icon': Icons.apple, 'color': Colors.green},
    {'name': '图书', 'icon': Icons.book, 'color': Colors.indigo},
    {'name': '更多', 'icon': Icons.more_horiz, 'color': Colors.grey},
  ];

  // 推荐商品数据(模拟)
  final List<Map<String, dynamic>> _products = [
    {"name": "鸿蒙旗舰手机", "price": "3999", "sales": "2311"},
    {"name": "无线蓝牙耳机", "price": "399", "sales": "1202"},
    {"name": "智能手表", "price": "1299", "sales": "890"},
    {"name": "平板电脑", "price": "2999", "sales": "1567"},
  ];

  // 底部导航点击事件
  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 主体内容(可滚动,防止溢出)
      body: SingleChildScrollView(
        child: Column(
          children: [
            // 1️⃣ 顶部渐变搜索栏 🔥
            _buildSearchHeader(),

            // 2️⃣ 轮播Banner区域 📢
            _buildBanner(),

            // 3️⃣ 商品分类网格 📦
            _buildCategoryGrid(),

            // 4️⃣ 为你推荐商品列表 🛒
            _buildRecommendSection(),
          ],
        ),
      ),

      // 5️⃣ 底部导航栏 🧭
      bottomNavigationBar: _buildBottomNav(),
    );
  }

1️⃣ 顶部渐变搜索栏(城市+搜索框+相机)

dart 复制代码
  // 顶部搜索栏
  Widget _buildSearchHeader() {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.fromLTRB(15, 40, 15, 20),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: [Colors.orangeAccent, Colors.orange],
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
        ),
      ),
      child: Row(
        children: [
          // 城市选择
          Row(
            children: const [
              Text(
                "深圳",
                style: TextStyle(color: Colors.white, fontSize: 16),
              ),
              Icon(Icons.arrow_drop_down, color: Colors.white),
            ],
          ),
          const SizedBox(width: 10),

          // 搜索框
          Expanded(
            child: Container(
              height: 38,
              padding: const EdgeInsets.symmetric(horizontal: 10),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20),
              ),
              child: Row(
                children: const [
                  Icon(Icons.search, color: Colors.grey),
                  SizedBox(width: 5),
                  Text("搜索商品", style: TextStyle(color: Colors.grey)),
                ],
              ),
            ),
          ),
          const SizedBox(width: 10),

          // 相机图标
          const Icon(Icons.camera_alt, color: Colors.white),
        ],
      ),
    );
  }

2️⃣ 轮播Banner区域(渐变+优惠标签)

dart 复制代码
  // 轮播图
  Widget _buildBanner() {
    return Container(
      height: 160,
      margin: const EdgeInsets.all(15),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(15),
        gradient: LinearGradient(
          colors: [Colors.orangeAccent, Colors.orange],
        ),
      ),
      child: Stack(
        children: [
          const Positioned(
            left: 20,
            top: 30,
            child: Text(
              "限时特惠\n新人专享优惠",
              style: TextStyle(color: Colors.white, fontSize: 18, height: 1.5),
            ),
          ),
          Positioned(
            right: 20,
            top: 20,
            child: Container(
              width: 80,
              height: 80,
              decoration: const BoxDecoration(
                color: Colors.white12,
                shape: BoxShape.circle,
              ),
              child: const Icon(Icons.shopping_bag, color: Colors.white, size: 40),
            ),
          ),
        ],
      ),
    );
  }

3️⃣ 商品分类网格(10个分类+图标)

dart 复制代码
  // 分类网格
  Widget _buildCategoryGrid() {
    return GridView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      padding: const EdgeInsets.symmetric(horizontal: 15),
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 5,
        crossAxisSpacing: 5,
        mainAxisSpacing: 15,
        childAspectRatio: 1,
      ),
      itemCount: _categories.length,
      itemBuilder: (context, index) {
        var item = _categories[index];
        return Column(
          children: [
            Container(
              width: 50,
              height: 50,
              decoration: BoxDecoration(
                color: item['color'].withOpacity(0.1),
                borderRadius: BorderRadius.circular(10),
              ),
              child: Icon(item['icon'], color: item['color']),
            ),
            const SizedBox(height: 5),
            Text(item['name'], style: const TextStyle(fontSize: 12)),
          ],
        );
      },
    );
  }

4️⃣ 为你推荐商品(双列网格+销量+价格)

dart 复制代码
  // 推荐商品
  Widget _buildRecommendSection() {
    return Container(
      padding: const EdgeInsets.all(15),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            "为你推荐",
            style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
          ),
          const SizedBox(height: 15),
          GridView.builder(
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              crossAxisSpacing: 10,
              mainAxisSpacing: 10,
              childAspectRatio: 0.75,
            ),
            itemCount: _products.length,
            itemBuilder: (context, index) {
              var product = _products[index];
              return Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(10),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.1),
                      blurRadius: 3,
                    ),
                  ],
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // 商品图片占位
                    Container(
                      height: 120,
                      decoration: BoxDecoration(
                        color: Colors.grey[100],
                        borderRadius: const BorderRadius.vertical(top: Radius.circular(10)),
                      ),
                      child: const Center(child: Icon(Icons.image, color: Colors.grey)),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8),
                      child: Text(
                        product['name'],
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                        style: const TextStyle(fontSize: 13),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 8),
                      child: Text(
                        "¥${product['price']}",
                        style: const TextStyle(color: Colors.red, fontSize: 14, fontWeight: FontWeight.bold),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8),
                      child: Text(
                        "销量 ${product['sales']}",
                        style: const TextStyle(color: Colors.grey, fontSize: 11),
                      ),
                    ),
                  ],
                ),
              );
            },
          ),
        ],
      ),
    );
  }

5️⃣ 底部导航栏(首页/分类/购物车/我的)

dart 复制代码
  // 底部导航
  Widget _buildBottomNav() {
    return BottomNavigationBar(
      items: const [
        BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
        BottomNavigationBarItem(icon: Icon(Icons.category), label: '分类'),
        BottomNavigationBarItem(icon: Icon(Icons.shopping_cart), label: '购物车'),
        BottomNavigationBarItem(icon: Icon(Icons.person), label: '我的'),
      ],
      currentIndex: _selectedIndex,
      selectedItemColor: Colors.orange,
      unselectedItemColor: Colors.grey,
      type: BottomNavigationBarType.fixed,
      onTap: _onItemTapped,
    );
  }
}

✅ 运行方法(3秒搞定)

  1. 在项目lib/pages/下新建home_page.dart
  2. 复制全部代码进去
  3. 在路由中跳转到该页面
  4. 运行在鸿蒙模拟器/真机
  5. 效果直接拉满🎉

💡 鸿蒙适配避坑指南(大学生必看)

我实战踩过的坑😭,直接帮你避开:

  1. 页面溢出报错
    ✅ 解决方案:外层套SingleChildScrollView
  2. 网格列表卡顿
    ✅ 解决方案:shrinkWrap: true + NeverScrollableScrollPhysics
  3. 鸿蒙状态栏文字看不清
    ✅ 解决方案:使用渐变背景+白色文字对比
  4. 底部导航被键盘顶起
    ✅ 解决方案:Flutter原生已自动适配,直接用

🚀 毕设加分扩展功能(超简单)

想让你的鸿蒙电商项目更高级?直接加这些👇

  • 真实轮播图(carousel_slider
  • 商品点击跳详情页
  • 搜索栏跳转到上一篇的搜索页面
  • 商品数据对接后端接口
  • 鸿蒙深色模式自动切换
  • 购物车数量小红点

🎉 总结

作为大学生实战项目,Flutter for OpenHarmony 真的超香!

一套代码直接跑安卓+iOS+鸿蒙,找实习、做毕设、参加比赛都超级加分💯

本篇鸿蒙电商首页从搜索栏、轮播、分类到商品流、底部导航全部完整实现,UI还原度100%、鸿蒙适配拉满、代码可直接运行~

下一篇我会继续更新分类页/购物车/个人中心完整教程,小伙伴们点赞+收藏+关注不迷路😆


相关推荐
梦想不只是梦与想2 小时前
flutter 与原生通信的底层原理(二)
flutter
以太浮标2 小时前
华为eNSP模拟器综合实验之- 华为设备 LLDP(Link Layer Discovery Protocol)解析
运维·服务器·网络·网络协议·华为·信息与通信·信号处理
Lanren的编程日记2 小时前
Flutter 鸿蒙应用离线模式实战:无网络也能流畅使用
网络·flutter·harmonyos
IntMainJhy3 小时前
【Flutter for OpenHarmony 】第三方库 聊天应用:Provider 状态管理实战指南
flutter·华为·harmonyos
想你依然心痛3 小时前
HarmonyOS 6金融应用实战:基于悬浮导航与沉浸光感的“光影财富“智能投顾系统
金融·harmonyos·鸿蒙·悬浮导航·沉浸光感
互联网散修3 小时前
鸿蒙星闪实战:从零实现高速可靠的跨设备文件传输 - 星闪篇
华为·harmonyos
小红星闪啊闪3 小时前
鸿蒙开发速通(一)
harmonyos
特立独行的猫a3 小时前
HarmonyOS鸿蒙PC开源QT软件移植:移植开源文本编辑器 NotePad--(Ndd)到鸿蒙 PC实践总结
qt·开源·notepad++·harmonyos·notepad--·鸿蒙pc