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

Flutter学习笔记(七)---主题

Theme

视频课里面很多主题设置已经不起作用了,需要注意

在这里插入图片描述

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';void main(){//调用runApp函数runApp(MyApp());
}class MyApp extends StatelessWidget{Widget build(BuildContext context) {return MaterialApp(//保持整个app是Material风格title: "主题演示",debugShowCheckedModeBanner: false,//去掉右上角的debugtheme: ThemeData(// 亮度brightness: Brightness.light,primarySwatch: Colors.red,// 明确设置 primaryColorprimaryColor: Colors.green,// 统一textButtonTheme的一些配置textButtonTheme: TextButtonThemeData(style: ButtonStyle(minimumSize: MaterialStateProperty.all(Size(10, 100)),backgroundColor: MaterialStateProperty.all(Colors.greenAccent))),// 统一Card的一些配置cardTheme: CardTheme(color: Colors.yellow),// // 设置 AppBar 主题// appBarTheme: AppBarTheme(//   backgroundColor: Colors.red,//   foregroundColor: Colors.white,// ),// 设置 FloatingActionButton 主题floatingActionButtonTheme: FloatingActionButtonThemeData(backgroundColor: Colors.red,foregroundColor: Colors.white,),),//暗黑模式darkTheme: ThemeData(cardTheme: CardTheme(color: Colors.grey),),home: YZHomePage());}
}class YZHomePage extends StatelessWidget
{Widget build(BuildContext context) {return Scaffold(//脚手架appBar: AppBar(title: Text("Theme Study"),),body: YZContentBody(),bottomNavigationBar: BottomNavigationBar(items: [BottomNavigationBarItem(label: "首页",icon: Icon(Icons.home)),BottomNavigationBarItem(label: "我的",icon: Icon(Icons.person))],),floatingActionButton: FloatingActionButton(child: Icon(Icons.add),onPressed: (){print("object");}),);}
}class YZContentBody extends StatelessWidget{Widget build(BuildContext context) {// 获取主题颜色final theme = Theme.of(context);return Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,children: [Text("Hello World",textDirection: TextDirection.ltr,//从左到右style: TextStyle(fontSize: 24,color: Colors.deepOrange)),Switch(value: true, onChanged: (value){print("Switch changed: $value");}),/// iOS系统的样式CupertinoSwitch(value: true, onChanged: (value){}),TextButton(onPressed: (){Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context){return YZHomeDetailPage();}));},child: Text("c"),// style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.yellow)),),Card(child: Text("这是一个Card"),)],));}
}class YZHomeDetailPage extends StatelessWidget {const YZHomeDetailPage({super.key});Widget build(BuildContext context) {return Theme(// 重新搞一个ThemeData// data: ThemeData(//   // 自己的ThemeData,以及CardTheme//   cardTheme: CardTheme(//       color: Colors.redAccent//   ),// ),data: Theme.of(context).copyWith(cardTheme: CardTheme(color: Colors.purple)),child: Scaffold(appBar: AppBar(backgroundColor: Colors.green,),body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,children: [Card(child: Text("1222222222"),),TextButton(onPressed: (){}, child: Text("123"))],),),),);}
}

屏幕适配

import 'dart:ui';import 'package:flutter/material.dart';class YZScreenSizefit {static double? physicalWidth;static double? physicalHeight;static double? screenWidth;static double? screenHeight;static double? statusHeight;static double? rpx;static double? px;/// 初始化static void initialize({ double standardSize = 750 }) {/// 手机的物理分辨率physicalWidth = window.physicalSize.width;physicalHeight = window.physicalSize.height;print("分辨率:$physicalWidth * $physicalHeight");// 获取dprfinal dpr = window.devicePixelRatio;// 手机屏幕的大小screenWidth = physicalWidth! / dpr;screenHeight = physicalHeight! / dpr;print("手机屏幕大小:$screenWidth * $screenHeight");statusHeight = window.padding.top / dpr;print("状态栏的高度:$statusHeight");// 计算rpx的大小rpx = screenWidth! / standardSize;px = screenWidth! / standardSize * 2;}static double setRpx(double size) {return rpx! * size;}static double setPx(double size) {return px! * size;}
}

Extension

在这里插入图片描述

import '/YZScreenSizeFit.dart';extension DoubleFit on double {double px() {return YZScreenSizefit.setPx(this);}// 使用get方法。用的时候,不需要写()double get getPx {return YZScreenSizefit.setPx(this);}
}

美食广场项目

美食广场Git地址
在这里插入图片描述

使用终端创建flutter项目:
cd 到文件地址
flutter create name

修改id、图标、背景图等操作,在第18节视频课,23分钟开始

修改android的id:
修改安卓的ID

修改android的app名字:
在这里插入图片描述

将json转换成dart的model的网站:
json_to_dart

加载Json,并将Json转换成Model

import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:theme2/core/model/category_model.dart';class JsonParse {static Future<List<YZCategoryModel>> getCategoryData() async{//1. 加载JSON文件final jsonString = await rootBundle.loadString("assets/json/home.json");//2. 将jsonString转换成Map或者Listfinal result = json.decode(jsonString);//将其他格式转换成jsonString// json.encoder()//3. 将Map中的内容转成一个个对象final resultList = result["category"];//获取到的是数组List<YZCategoryModel> categoryList = [];for (var json in resultList){categoryList.add(YZCategoryModel.fromJson(json));}return categoryList;}
}

http://www.dtcms.com/a/344111.html

相关文章:

  • 嵌入式学习day34-网络-tcp/udp
  • 如何判断是否应该为了一个小功能而引入一个大体积的库
  • 配电网重构优化:以减小网损为目标的智能算法实现
  • GitLab CI :深入剖析 gl-sbom-report.cdx.json 解码“数字身份证”
  • 云蝠智能 VoiceAgent:重构售后服务场景
  • 岭回归算法拉索回归
  • LeeCode 40.组合总和II
  • 数据结构之深入探索归并排序
  • 西门子S7-1200系列基本组态常见问题
  • 【C++】多态(详解)
  • Debezium监听MySQL binlog并实现有状态重启
  • 工业环境电缆火灾预防的分布式光纤在线监测
  • 质谱数据解读
  • 【微服务的数据一致性分发问题】究极解决方案
  • Unity设置UI显示区域
  • 主题配色下的背景透明度
  • uniapp plus.io API 封装文件读写方法
  • 【IDEA2017】使用设置+创建项目的不同方式
  • GaussDB SQL引擎(1)-SQL执行流程与解析器和优化器
  • 【Qt调试】断点时,Expressions不能查看变量
  • 新手向:用FastAPI快速构建高性能Web服务
  • 单北斗变形监测系统应用指南
  • c++:MFC中sqlite3的使用(附实际案例)
  • VScode远程连接Ubuntu报错问题分析
  • 表格识别技术:通过图像处理与深度学习,将非结构化表格转化为可编辑结构化数据,推动智能化发展
  • Mac电脑英特尔版本最新系统15.6.1安装php环境
  • 机试备考笔记 18/31
  • 使用 JS 渲染页面并导出为PDF 常见问题与修复
  • Laravel 使用阿里云OSS S3 协议文件上传
  • 高效稳定的仁懋MOSFET系列,打造卓越服务器电源