flutter 设置字体大小,适应各种屏幕

news/2024/9/29 1:21:33/

起因, 目的:

来源就是客户需求。 从个人角度来说,我讨厌 flutter, 和 java 一样, 都是 臃肿,繁琐,死板.

1. 过程:

根据用户的屏幕尺寸,把子元素大小, 字体的大小,都设置为百分比,无需插件。

2. 有个报错: 无法打开包括文件: “atlstr.h”: No such file…
  • 我先换成 Chrome 浏览器运行。成功。
  • 然后,把代码复制到另一个新项目,运行,成功。
  • 所以,原因是,客户提供的配置环境与我电脑上的环境不匹配, 而不是代码有问题! 也不是我的环境有问题。
3. 代码: tree_1.dart
import 'package:flutter/material.dart';void main() {runApp(MaterialApp(home: MyApp())); // Wrap MyApp with MaterialApp
}class MyApp extends StatefulWidget {_MyAppState createState() => _MyAppState();
}class NodeTemplate {int id = 0;int value = 0;bool solid = false;Color color = Colors.grey;NodeTemplate();
}// --------------------  从这里开始 !!!! ----------------------------
class _MyAppState extends State<MyApp> {// This list holds the color of each box, initially all are grey.int nodeValue = 100;List<Color> boxColors = List<Color>.filled(15, Colors.grey);List<NodeTemplate> treeNodes =List<NodeTemplate>.generate(15, (_) => NodeTemplate());Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('Draggable Blue Box'),),body: Column(children: [// Binary tree structureExpanded(child: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,children: [// Level 1 - 1 box 11111111111111111111111111111111111111111111111111111111Row(mainAxisAlignment: MainAxisAlignment.center,children: [buildDragTarget(0),],),SizedBox(height: 16.0), // For spacing between levels// Level 2 - 2 boxes 22222222222222222222222222222222222222222222222222222222Row(mainAxisAlignment: MainAxisAlignment.center,children: [buildDragTarget(1),SizedBox(width: 100),buildDragTarget(2),],),SizedBox(height: 16.0), // For spacing between levels// Level 3 - 4 boxes 333333333333333333333333333333333333333333333333333333Row(mainAxisAlignment: MainAxisAlignment.center,children: [buildDragTarget(3),SizedBox(width: 50),buildDragTarget(4),SizedBox(width: 50),buildDragTarget(5),SizedBox(width: 50),buildDragTarget(6),],),SizedBox(height: 16.0), // For spacing between levels// Level 4 - 8 boxes 44444444444444444444444444444444444444444444444444Row(mainAxisAlignment: MainAxisAlignment.center,children: [buildDragTarget(7),buildDragTarget(8),buildDragTarget(9),buildDragTarget(10),buildDragTarget(11),buildDragTarget(12),buildDragTarget(13),buildDragTarget(14),],),],),),),// Draggable blue box at the bottomDraggable<int>(data: nodeValue,feedback: Container(width: 50.0,height: 50.0,color: Colors.blue.withOpacity(0.7),child: Center(child: Text(nodeValue.toString())),),childWhenDragging: Container(width: 50.0,height: 50.0,color: Colors.blue.withOpacity(0.3),),child: Container(width: 50.0,height: 50.0,color: Colors.blue,child: Center(child: Text(nodeValue.toString())),),),SizedBox(height: 30),],),// Use the built-in floatingActionButton propertyfloatingActionButton: FloatingActionButton(onPressed: () {_showInputDialog(context); // Show the dialog to enter value},backgroundColor: Colors.blue,child: Icon(Icons.add),),);}void _showInputDialog(BuildContext context) {TextEditingController controller = TextEditingController();showDialog(context: context,builder: (context) {return AlertDialog(title: Text('Enter value for the Node'),content: TextField(controller: controller,keyboardType: TextInputType.number,decoration: InputDecoration(hintText: "Enter value"),),actions: [TextButton(onPressed: () {setState(() {nodeValue = int.tryParse(controller.text) ??0; // Parse input or default to 0});Navigator.of(context).pop(); // Close the dialog},child: Text('Submit'),),],);},);}// Function to build a DragTarget for the grey boxesWidget buildDragTarget(int index) {return GestureDetector(onLongPress: () {setState(() {treeNodes[index].id = index;treeNodes[index].value = 0;treeNodes[index].color = Colors.grey;treeNodes[index].solid = false;});},child: DragTarget<int>(onAccept: (data) {setState(() {treeNodes[index].color =Colors.blue; // Change color when blue box is droppedtreeNodes[index].value = data;treeNodes[index].solid = true;});},builder: (context, candidateData, rejectedData) {// 获取屏幕的宽度和高度double screenWidth = MediaQuery.of(context).size.width;double screenHeight = MediaQuery.of(context).size.height;// 根据屏幕大小调整宽度和高度,可以设置为屏幕宽度的百分比double containerWidth = screenWidth * 0.08; // 比如宽度占屏幕宽度的 10%double containerHeight = screenHeight * 0.1; // 比如高度占屏幕高度的 10%// 动态调整 字体大小double fontSize = 500 * 0.05; // 根据屏幕宽度动态调整字体大小return Container(width: containerWidth, //50.0, 80.0height: containerHeight, //100.0,margin: EdgeInsets.symmetric(horizontal: 4.0),decoration: BoxDecoration(color: treeNodes[index].color, // Dynamic color based on stateshape: BoxShape.circle, // Makes the container a circle),//color: treeNodes[index].color, // Dynamic color based on statechild: Center(child: Text(treeNodes[index].value.toString(),// style: TextStyle(fontSize: 10),style: TextStyle(fontSize: fontSize),),),);},),);}
}
效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述



个人接单,留学生辅导,python, R语言,私聊.

老哥,支持一下啊。

支付宝扫码领红包哦


http://www.ppmy.cn/news/1531193.html

相关文章

Mac pnpm安装

安装pnpm的时候一定要把npm更新到最新版 不然pnpm下载不成功。 &#xff08;更新npm&#xff09;&#xff1a;sudo npm install -g npm (安装pnpm:) sudo npm install -g pnpm 检验安装是否成功&#xff1a;pnpm --version 项目内安装依赖&#xff1a;pnpm install / 运行项目&…

用Python实现运筹学——Day 4: 线性规划的几何表示

一、学习内容 线性规划的几何表示&#xff1a; 线性规划问题的解通常位于一个凸多边形&#xff08;即可行解空间&#xff09;的顶点上&#xff0c;这意味着在求解线性规划问题时&#xff0c;只需要找到可行解空间中的顶点并计算出目标函数值&#xff0c;再选择其中的最优解。 可…

车道线拟合

聚类如何帮助解决斑马线误拟合问题 聚类算法通过将相似的像素或特征点归为一类&#xff0c;可以用来分析图像中的不同结构&#xff08;例如车道线和斑马线&#xff09;。以下是一些具体的聚类方法和它们在车道线检测中的应用&#xff1a; 1. 基于几何特征的聚类 斑马线和车道…

解决R包依赖版本不兼容问题

ERROR: dependency ‘Matrix’ is not available for package ‘irlba’ removing ‘/root/anaconda3/envs/myview/lib/R/library/irlba’ ERROR: dependency ‘Matrix’ is not available for package ‘N2R’ removing ‘/root/anaconda3/envs/myview/lib/R/library/N2R’ ER…

【Python语言初识(五)】

一、文件和异常 在Python中实现文件的读写操作其实非常简单&#xff0c;通过Python内置的open函数&#xff0c;我们可以指定文件名、操作模式、编码信息等来获得操作文件的对象&#xff0c;接下来就可以对文件进行读写操作了。这里所说的操作模式是指要打开什么样的文件&#…

C++读取txt文件中的句子在终端显示,同时操控鼠标滚轮(涉及:多线程,产生随机数,文件操作等)

文章目录 &#x1f315;运行效果&#x1f315;功能描述&#x1f315;代码&#x1f319;mian.cpp&#x1f319;include⭐MouseKeyControl.h⭐TipsManagement.h &#x1f319;src⭐MouseControl.cpp⭐TipsManagement.cpp &#x1f315;运行效果 &#x1f315;功能描述 线程一&am…

CAT1 RTU软硬件设计开源资料分析(TCP协议+Modbus协议+GNSS定位版本 )

01 CAT1 RTU方案简介&#xff1a; 远程终端单元( Remote Terminal Unit&#xff0c;RTU)&#xff0c;一种针对通信距离较长和工业现场环境恶劣而设计的具有模块化结构的、特殊的计算机测控单元&#xff0c;它将末端检测仪表和执行机构与远程控制中心相连接。 奇迹TCP RTUGNS…

element ui中当el-dialog需要做全屏时,.fullscreen样式修改问题

element ui 饿了么UI中el-dialog样式修改问题 场景解决方法就是&#xff1a;去掉底部样式中的scoped,然后再进行页面级样式的更改即可。 场景 最近在使用element-ui时&#xff0c;使用到了弹窗组件&#xff1a; element-ui 官网链接地址&#xff1a; element-ui 官网链接地址…