当前位置: 首页 > news >正文

Flutter小功能实现-咖啡店

1 导航栏实现

效果图:

 

 

1.Package

google_nav_bar: ^5.0.6

使用文档:

google_nav_bar | Flutter Package

2.Code

//MyBottomNavBar

class MyBottomNavBar extends StatelessWidget {void Function(int)? onTabChange;MyBottomNavBar({super.key, required this.onTabChange});@overrideWidget build(BuildContext context) {return Container(margin: const EdgeInsets.all(25),child: GNav(onTabChange: (value) => onTabChange!(value),color: Colors.grey[400],mainAxisAlignment: MainAxisAlignment.center,activeColor: Colors.grey[700],tabBackgroundColor: Colors.grey.shade300,tabBorderRadius: 24,tabActiveBorder: Border.all(color: Colors.white),tabs: const [GButton(icon: Icons.home, text: 'Shop'),GButton(icon: Icons.shopping_bag_outlined, text: 'Cart'),]),);}
}

//HomePage

class HomePage extends StatefulWidget {const HomePage({super.key});@overrideState<HomePage> createState() => _HomePageState();
}class _HomePageState extends State<HomePage> {int _selectPage = 0;final List<Widget> _pages = [ShopPage(), CartPage()];@overrideWidget build(BuildContext context) {return Scaffold(backgroundColor: backgroundColor,bottomNavigationBar: MyBottomNavBar(onTabChange: (index) => navigateBottomBar(index),),body: _pages[_selectPage],);}///点击底部按钮响切换///navigateBottomBar(int index) {setState(() {_selectPage = index;});}
}

2 商品列表实现

1.Package

provider

使用文档:

provider | Flutter Package

2.Code

Models 数据模型

Coffee 咖啡数据模型:名称、价格、图片

CoffeeShop 咖啡售卖数据模型:coffeeShop 在售咖啡种类,userCart用户购物车 ,addItemToCart添加到购物车方法,removeItemFromCart从购物车移除方法

class Coffee {final String name;final String price;final String imagePath;Coffee({required this.name, required this.price, required this.imagePath});
}class CoffeeShop extends ChangeNotifier {//sale listfinal List<Coffee> _shop = [Coffee(name: 'Long Black',price: '4.10',imagePath: 'lib/images/coffee-cup.png'),Coffee(name: 'Espresso', price: '4.10', imagePath: 'lib/images/espresso.png'),Coffee(name: 'Frappe', price: '4.10', imagePath: 'lib/images/frappe.png'),Coffee(name: 'Iced', price: '4.10', imagePath: 'lib/images/iced.png'),Coffee(name: 'Latte', price: '4.10', imagePath: 'lib/images/latte.png'),];//user cartfinal List<Coffee> _userCart = [];//get coffee listList<Coffee> get coffeeShop => _shop;//get user cartList<Coffee> get userCart => _userCart;//add item to cartvoid addItemToCart(Coffee coffee) {_userCart.add(coffee);notifyListeners();}//remove item from cartvoid removeItemFromCart(Coffee coffee) {_userCart.remove(coffee);notifyListeners();}
}

商品展示Tile组件

class CoffeeTile extends StatelessWidget {final Coffee coffee;final Icon icon;void Function()? onPressed;CoffeeTile({super.key,required this.coffee,required this.onPressed,required this.icon,});@overrideWidget build(BuildContext context) {return Container(decoration: BoxDecoration(color: Colors.grey[200], borderRadius: BorderRadius.circular(12)),margin: const EdgeInsets.only(bottom: 10),padding: const EdgeInsets.symmetric(vertical: 25, horizontal: 10),child: ListTile(title: Text(coffee.name),subtitle: Text(coffee.price),leading: Image.asset(coffee.imagePath),trailing: IconButton(icon: icon,onPressed: onPressed,),),);}
}

3.源码下载:

https://download.csdn.net/download/sc_liuliye/88278760

http://www.lryc.cn/news/148910.html

相关文章:

  • JavaSE 集合框架及背后的数据结构
  • -9501 MAL系统没有配置或者服务器不是企业版(dm8达梦数据库)
  • 云备份——第三方库简单介绍并使用(上)
  • MySQL数据库之索引
  • OpenCV(四):Mat支持的运算
  • WebRTC音视频通话-WebRTC推拉流过程中日志log输出
  • 用Jmeter压测问题解决
  • C语言:字符函数和字符串函数(一篇拿捏字符串函数!)
  • 问道管理:成交量买卖公式?
  • 【MySQL】5、MySQL高阶语句
  • 【Linux】redhat7.8配置yum在线源【redhat7.8镜像容器内配置yum在线源】通用
  • 强大的处理器和接口支持BL304ARM控制器
  • react 基础知识(一)
  • SpringBoot整合JUnit、MyBatis、SSM
  • virtuoso61x中集成calibre
  • com.google.guava:guava 组件安全漏洞及健康分析
  • Hadoop服务脚本
  • [QT]设置程序仅打开一个,再打开就唤醒已打开程序的窗口
  • 数据库(二) Oracle篇
  • TDengine函数大全-目录
  • 代理模式之静态代理
  • LeetCode——栈的压入、弹出序列
  • Flutter 逆向安全
  • 【微服务部署】01-Kubernetes部署流程
  • SPI3+DMA外设驱动-TFTLCD初始化
  • 通过chatgpt 学习React的useEffect
  • rabbitMq介绍及使用
  • rabbitmq载在.net中批量消费的问题记录
  • 【RPC 协议】序列化与反序列化 | lua-cjson | lua-protobuf
  • Flutter的Timer类