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

server/2024/9/24 9:55:37/

起因, 目的:

来源就是客户需求。 从个人角度来说,我讨厌 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/server/121292.html

相关文章

uniapp使用uview2上传图片功能

官网地址Upload 上传 | uView 2.0 - 全面兼容 nvue 的 uni-app 生态框架 - uni-app UI 框架 前提&#xff0c;需要下载vuew2插件 <view class"upload"><view class"u-demo-block__content"><view class"u-page__upload-item"&…

HarmonyOS鸿蒙开发实战(5.0)悬浮窗拖拽和吸附动画实践

鸿蒙HarmonyOS NEXT开发实战往期文章必看&#xff08;持续更新......&#xff09; HarmonyOS NEXT应用开发性能实践总结 HarmonyOS NEXT应用开发案例实践总结合集 最新版&#xff01;“非常详细的” 鸿蒙HarmonyOS Next应用开发学习路线&#xff01;&#xff08;从零基础入门…

抓机遇,促发展——2025第十二届广州国际汽车零部件加工技术及汽车模具展览会

新能源时代&#xff0c;电动化、智能化正在重塑全球汽车市场格局。中国自主品牌新能源汽车的市占率不断提升、头部效应初显&#xff0c;更有机会带动相关供应链企业成长。中国的零部件企业有望抓住变局下的机会&#xff0c;在新一轮竞争中崛起。 智能电动车时代&#xff0c;汽车…

Windows11系统安装,配置CUDA、cuDNN等

已经有大几年没有安装过 Windows 的系统了&#xff0c;今天因为黑神话悟空&#xff0c;准备把 Win 11 装一台&#xff0c;玩玩游戏&#xff0c;顺便把一些 CUDA 相关的异步解析项目也安装到 Window 上。 下载安装 PE 因为十几年前&#xff0c;只会用 PE 装系统&#xff0c;所…

mysql学习教程,从入门到精通,SQL 删除表(DROP TABLE 语句)(20)

1、SQL 删除表&#xff08;DROP TABLE 语句&#xff09; 在SQL中&#xff0c;DROP TABLE语句用于删除数据库中的一个或多个表&#xff0c;同时删除表中的所有数据、索引、触发器、约束和表结构本身。这是一个非常强大的命令&#xff0c;因此在使用时需要格外小心&#xff0c;因…

基于单片机控制的程控开关电源研究

摘 要 : 在开关电源控制方案中 , 建立于单片机控制基础方式上的程控开关电源方案是一种比较有效的方式 。 利用单片机实现对程控开关的控制, 可以减少设计复杂度 、 增加控制可靠性 , 值得投入更多的精力进行研究和推进 。 本文就这一控制方法进行了详细的探讨 。 关键词…

基于web的工作管理系统设计与实现

博主介绍&#xff1a;专注于Java vue .net php phython 小程序 等诸多技术领域和毕业项目实战、企业信息化系统建设&#xff0c;从业十五余年开发设计教学工作 ☆☆☆ 精彩专栏推荐订阅☆☆☆☆☆不然下次找不到哟 我的博客空间发布了1000毕设题目 方便大家学习使用 感兴趣的…

AI辅助编码工具如何影响着程序员开发群体

AI辅助编码工具的出现对程序员开发群体产生了深远的影响&#xff0c;有一些初步基础的程序员&#xff0c;可以借助AI工具的加持&#xff0c;生产效率大大提升&#xff0c;达到中高级程序员的水平。 这些影响可以从多个角度来分析&#xff1a; 提高开发效率&#xff1a; AI工具…