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

如何使用Flutter开发执行操作系统shell命令的工具

简介
Flutter是一种由Google开发的移动应用程序开发框架,它允许开发人员使用单个代码库构建高性能、高质量的移动体验。而Android终端命令行工具则允许用户在Android手机上运行类似于Linux的操作系统命令。本文的目的是介绍如何在Flutter应用中开发一个Android终端命令行工具,包括终端命令行页面的布局设计、与Shell通信的基本原理、输入输出处理的基本技巧、终端样式和输出样式的可配置性,以及如何在具体应用中利用终端命令行工具来执行系统命令和与用户进行交互。
实现终端命令行页面
为了实现一个完整的终端命令行页面,我们可以使用Flutter提供的基础控制台模块,例如TextArea、TextField等。需要注意的是,在Android中Shell是另一个进程,我们需要使用Flutter提供的方法来与Shell进行通信。具体来说,我们可以通过创建一个Future或者Stream来与Shell进行通信。其中Future用于单个结果的返回,而Stream则可以返回多个结果。我们可以在Flutter使用Async函数和这些模块的onChanged()方法来处理输入和输出。以下是最简单的实现方式。
dart复制代码

class TerminalPage extends StatefulWidget {_TerminalPageState createState() => _TerminalPageState();
}class _TerminalPageState extends State<TerminalPage> {TextEditingController _controller;Future<Process> _process;Stream<String> _output;void initState() {super.initState();_controller = TextEditingController();_process = Process.start('/system/bin/sh', []);_controller.addListener(_handleInput);}void _handleInput() {_process.then((process) {process.stdin.writeln(_controller.text);_controller.clear();});}Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("Terminal Command"),),body: Column(children: [Flexible(flex: 1,child: StreamBuilder<String>(stream: _output,builder: (BuildContext context, AsyncSnapshot<String> snapshot) {if (snapshot.hasData) {return Text(snapshot.data);} else {return CircularProgressIndicator();}},),),TextField(controller: _controller,decoration: InputDecoration(border: InputBorder.none, hintText: "Input terminal command"),)],),);}
}

终端样式和输出样式的设计
除了基本的终端命令行页面,我们也需要为终端和输出样式提供更多的可配置性。在Flutter中,我们可以使用TextStyle模块(例如,color、fontSize、fontFamily等)来控制终端和输出的样式。我们可以将TextStyle保存在Flutter的本地存储中,以便在应用中进行修改。以下是一个具体的样例:
dart复制代码

 class _TerminalPageState extends State<TerminalPage> {TextEditingController _controller;Future<Process> _process;Stream<String> _output;TextStyle _terminalStyle;TextStyle _outputStyle;void initState() {super.initState();_controller = TextEditingController();_process = Process.start('/system/bin/sh', []);_terminalStyle = TextStyle(color: Colors.white, backgroundColor: Colors.black, fontFamily: 'Monospace');_outputStyle = TextStyle(color: Colors.white, backgroundColor: Colors.black, fontSize: 16);_controller.addListener(_handleInput);}void _handleInput() {_process.then((process) {process.stdin.writeln(_controller.text);_controller.clear();});}Future<File> _getTerminalStyleFile() async {final directory = await getApplicationDocumentsDirectory();final file = File('${directory.path}/terminalStyle');if (file.existsSync()) {return file;} else {await file.create();await file.writeAsString('{"color": "#FFFFFF", "background": "#000000"}');return file;}}Future<File> _getOutputStyleFile() async {final directory = await getApplicationDocumentsDirectory();final file = File('${directory.path}/outputStyle');if (file.existsSync()) {return file;} else {await file.create();await file.writeAsString('{"color": "#FFFFFF", "background": "#000000", "size": 16}');return file;}}Future<void> _loadStyles() async {final terminalFile = await _getTerminalStyleFile();final terminalJson = await terminalFile.readAsString();final terminalStylesMap = jsonDecode(terminalJson);_terminalStyle = TextStyle(color: Color(int.parse(terminalStylesMap['color'].toString(), radix: 16)),backgroundColor: Color(int.parse(terminalStylesMap['background'].toString(), radix: 16)),fontFamily: 'Monospace',);final outputFile = await _getOutputStyleFile();final outputJson = await outputFile.readAsString();final outputStylesMap = jsonDecode(outputJson);_outputStyle = TextStyle(color: Color(int.parse(outputStylesMap['color'].toString(), radix: 16)),backgroundColor: Color(int.parse(outputStylesMap['background'].toString(), radix: 16)),fontSize: outputStylesMap['size'].toDouble(),);}Future<void> _saveTerminalStyle(String color, String background) async {final terminalFile = await _getTerminalStyleFile();final terminalStylesMap = {"color": color, "background": background};final terminalJson = jsonEncode(terminalStylesMap);await terminalFile.writeAsString(terminalJson);}Future<void> _saveOutputStyle(String color, String background, double size) async {final outputFile = await _getOutputStyleFile();final outputStylesMap = {"color": color, "background": background, "size": size.toInt()};final outputJson = jsonEncode(outputStylesMap);await outputFile.writeAsString(outputJson);}Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("Terminal Command"),),body: FutureBuilder(future: _loadStyles(),builder: (BuildContext context, AsyncSnapshot<void> snapshot) {switch (snapshot.connectionState) {case ConnectionState.done:return Column(children: [Flexible(flex: 1,child: StreamBuilder<String>(stream: _output,builder: (BuildContext context, AsyncSnapshot<String> snapshot) {if (snapshot.hasData) {return Text(snapshot.data, style: _outputStyle);} else {return CircularProgressIndicator();}},),),TextField(style: _terminalStyle,controller: _controller,decoration: InputDecoration(border: InputBorder.none,hintText: "Input terminal command",hintStyle: _terminalStyle,),)],);default:return Center(child: CircularProgressIndicator());}},),);}}

在Flutter应用中使用终端命令行工具的例子
在具体应用中,我们可以使用终端命令行工具来执行一些系统命令,例如ping、df、top等。同时,我们也可以利用终端界面来与用户进行交互,例如读取用户的输入并将其传递给后端服务器。以下是一个简单的应用案例:
dart复制代码

class PingPage extends StatelessWidget {final String ip;PingPage({Key key, this.ip}) : super(key: key);Future<Process> _getPingProcess() async {final process = await Process.start('/system/bin/ping', ['-c', '5', ip]);return process;}Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("Ping $ip")),body: Container(child: FutureBuilder(future: _getPingProcess(),builder: (BuildContext context, AsyncSnapshot<Process> snapshot) {if (snapshot.hasData) {return Column(children: [Expanded(child: TerminalOutput(process: snapshot.data),),Container(margin: EdgeInsets.all(10),child: TextField(decoration: InputDecoration(border: OutlineInputBorder(),hintText: 'Input Ping Command',),),),Container(margin: EdgeInsets.all(10),child: ElevatedButton(onPressed: () {},child: Text("Send Command"),),)],);} else {return Center(child: CircularProgressIndicator());}},),),);}
}class TerminalOutput extends StatelessWidget {final Process process;TerminalOutput({Key key, this.process}) : super(key: key);Widget build(BuildContext context) {return StreamBuilder<String>(stream: process.stdout.transform(utf8.decoder),builder: (BuildContext context, AsyncSnapshot<String> snapshot) {if (snapshot.hasData) {return SingleChildScrollView(child: Text(snapshot.data,style: TextStyle(color: Colors.white,fontFamily: 'Monospace',backgroundColor: Colors.black,),),);} else {return Center(child: CircularProgressIndicator());}},);}
}

总结
本文介绍了在Flutter应用中开发Android终端命令行工具的方法。主要包括终端命令行页面的布局设计、与Shell通信的基本原理、输入输出处理的基本技巧、终端样式和输出样式的可配置性,以及如何在具体应用中利用终端命令行工具来执行系统命令和与用户进行交互。
本文提供了一个完整的演示案例,包括输出和输入的显示、终端命令行页面的开发、以及关于终端样式为可配置的,输出样式也为可配置的。希望这篇文章对于Flutter开发者有所帮助,能够为他们在实际开发中提供参考。
除了本文所介绍的方法外,还有其他方法可以在Flutter应用中实现终端命令行工具,例如使用Dart的Process类进行进程调用,或者将Flutter应用封装为Docker容器。这些方法也值得开发者进一步探索和试验。未来,随着移动应用越来越复杂,终端命令行工具的需求也会越来越大。因此,掌握Flutter中开发终端命令行工具的技能将会变得越来越重要。

链接:https://juejin.cn/post/7248827630865596474

相关文章:

如何使用Flutter开发执行操作系统shell命令的工具

简介 Flutter是一种由Google开发的移动应用程序开发框架&#xff0c;它允许开发人员使用单个代码库构建高性能、高质量的移动体验。而Android终端命令行工具则允许用户在Android手机上运行类似于Linux的操作系统命令。本文的目的是介绍如何在Flutter应用中开发一个Android终端命…...

西山居 游戏研发工程师实习生 面经

西山居实习面经 面试时长&#xff1a;26min&#xff08;两个面试官交替问&#xff09; 1、自我介绍 2、你平常怎么学习的 3、你实习接受加班么 4、说一下Unity的生命周期&#xff0c;Start和Awake哪里不同 5、Unity中Update与FixedUpdate的区别&#xff0c;怎么设置Fixed…...

YOLOv8训练自己的数据集+改进方法复现

yolov8已经出来好几个月了&#xff0c;并且yolov8从刚开始出来之后的小版本也升级好几次&#xff0c;总体变化不大&#xff0c;个别文件存放位置发生了变化&#xff0c;以下以最新版本的YOLOv8来详细学习和使用YOLOv8完成一次目标检测。 一、环境按照 深度学习环境搭建就不再…...

尚硅谷kafka3.0.0

目录 &#x1f483;概述 ⛹定义 ​编辑⛹消息队列 &#x1f938;‍♂️消息队列应用场景 ​编辑&#x1f938;‍♂️两种模式&#xff1a;点对点、发布订阅 ​编辑⛹基本概念 &#x1f483;Kafka安装 ⛹ zookeeper安装 ⛹集群规划 ​编辑⛹流程 ⛹原神启动 &#x1f938;‍♂️…...

【Andriod】Appium的不同版本(Appium GUI、Appium Desktop、Appium Server )的安装教程

文章目录 前言一.Appium GUI二.Appium Desktop三.Appium Server 命令行版本1.安装node.js2.安装Appium Server 前言 Appium 安装提供两2方式&#xff1a;桌面版和命令行版。其中桌面版又分为 Appium GUI 和 Appium Desktop。 建议&#xff1a;使用Appium Desktop 一.Appium …...

leetcode:面试题 17.04. 消失的数字(找单身狗/排序/公式)

一、题目&#xff1a; 函数原型&#xff1a;int missingNumber(int* nums, int numsSize) 二、思路&#xff1a; 思路1 利用“找单身狗”的思路&#xff08;n^n0&#xff1b;0^nn&#xff09;&#xff0c;数组中有0-n的数字&#xff0c;但缺失了一个数字x。将这些数字按位异或0…...

基于SpringBoot的时间管理系统

基于SpringBoot的时间管理系统的设计与实现~ 开发语言&#xff1a;Java数据库&#xff1a;MySQL技术&#xff1a;SpringBootMyBatis工具&#xff1a;IDEA/Ecilpse、Navicat、Maven 系统展示 登录界面 管理员界面 用户界面 摘要 基于Spring Boot的时间管理系统是一款功能丰富…...

centos搭建elastic集群

1、环境可以在同一台集群上搭建elastic&#xff0c;也可以在三台机器上搭建&#xff0c;这次演示的是在同一台机器搭建机器。 2、下载elastic &#xff1a;https://www.elastic.co/cn/downloads/past-releases#elasticsearch 2、​​​​​​ tar -zxvf elasticsearch-xxx-版…...

CUDA学习笔记(九)Dynamic Parallelism

本篇博文转载于https://www.cnblogs.com/1024incn/tag/CUDA/&#xff0c;仅用于学习。 Dynamic Parallelism 到目前为止&#xff0c;所有kernel都是在host端调用&#xff0c;CUDA Dynamic Parallelism允许GPU kernel在device端创建调用。Dynamic Parallelism使递归更容易实现…...

周记之马上要答辩了

“ 要变得温柔和强大&#xff0c;就算哪天突然孤身一人&#xff0c;也能平静地活下去&#xff0c;不至于崩溃。” 10.16 今天提前写完了一篇六级阅读&#xff0c;积累了一些词组&#xff1a; speak out against 公然反对&#xff0c;印象最深刻的就这个&#xff1b; 先了解…...

git简介和指令

git是一个开源的的分布式版本控制系统&#xff0c;用于高效的管理各种大小项目和文件 用途&#xff1a;防止代码丢失&#xff0c;做备份 项目的版本管理和控制&#xff0c;可以通过设置节点进行跳转 建立各自的开发环境分支&#xff0c;互不影响&#xff0c;方便合并 在多终端开…...

alibaba.fastjson的使用(五)-- Json数组字符串 ==》 JSONArray

目录 1. 使用到的方法 2. 实例演示 1. 使用到的方法 static JSONArray parseArray(String text) 2. 实例演示 /*** 将Json数组字符串转JsonArray*/@Testpublic void test5() {String jsonArrStr = "[{\"name\":\"郭靖\",\"age\":35},{\…...

ts json的中boolean布尔值或者int数字都是字符串,转成对象对应类型

没啥好写的再水一篇 json中都是字符串&#xff0c;转换一下就好&#xff0c;简单来说就是转换一次不行&#xff0c;再转换换一次&#xff0c;整体转换不够&#xff0c;细分的再转换一次 这是vue中 ts写法 ,我这里是拿对象做对比&#xff0c;不好字符和对象做对比&#xff0c;…...

【OpenGL】七、混合

混合 文章目录 混合混合公式glBlendFunc&#xff08;混合函数&#xff09;glBlendFuncSeparate渲染半透明纹理 参考链接 混合(Blending)通常是实现物体透明度(Transparency)的一种技术 简而言之&#xff1a;混合就是如何将输出颜色和目标缓冲区颜色结合起来。 混合公式 C_fina…...

JVM——堆内存调优(Jprofiler使用)Jprofile下载和安装很容易,故没有记录,如有需要,在评论区留言)

堆内存调优 当遇到OOM时&#xff0c;可以进行调参 1、尝试扩大堆内存看结果 2、分析内存&#xff0c;看哪个地方出现了问题&#xff08;专业工具&#xff09; 调整初始分配内存为1024M&#xff0c;调整最大分配内存为1024M&#xff0c;打印GC细节&#xff08;如何添加JVM操…...

Android cmdline-tools 版本与其最小JDK关系

关键词&#xff1a;Android cmdline-tools 历史版本、Android cmdline-tools 最小JDK版本、JDK 对应 major version、JDK LTS 信息 由于 JDK8 是一个常用的、较低的版本&#xff0c;因此只需要关注 JDK8 及以上版本的运行情况。 cmdline-tools 版本和最低 JDK 最终结论&…...

基于ARM+FPGA+AD的多通道精密数据采集仪方案

XM 系列具备了数据采集仪应具备的“操作简单、便于携带、满足各种测量需求”等功能的产品。具有超小、超轻量的手掌大小尺寸&#xff0c;支持8 种测量模块&#xff0c;还可进行最多576 Ch的多通道测量。另外&#xff0c;支持省配线系统&#xff0c;可大幅削减配线工时。使用时不…...

【JAVA学习笔记】43 - 枚举类

项目代码 https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter11/src/com/yinhai/enum_ 〇、创建时自动填入版权 作者等信息 如何在每个文件创建的时候打入自己的信息以及版权呢 菜单栏-File-setting-Editor-File and Code Templaters -Includes-输入信…...

Springcloud介绍

1.基本介绍 Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发&#xff0c;如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等&#xff0c;都可以用Spring Boot的开发风格做到一键启动和部署。Spring …...

LK光流法和LK金字塔光流法(含python和c++代码示例)

0 引言 本文主要记录LK光流算法及LK金字塔光流算法的详细原理,最后还调用OpenCV中的cv2.calcOpticalFlowPyrLK()函数实现LK金字塔光流算法,其中第3部分是python语言实现版本,第4部分是c++语言实现版本。 1 LK光流算法 1.1 简述 LK光流法是一种计算图像序列中物体运动的光…...

SpringBoot-17-MyBatis动态SQL标签之常用标签

文章目录 1 代码1.1 实体User.java1.2 接口UserMapper.java1.3 映射UserMapper.xml1.3.1 标签if1.3.2 标签if和where1.3.3 标签choose和when和otherwise1.4 UserController.java2 常用动态SQL标签2.1 标签set2.1.1 UserMapper.java2.1.2 UserMapper.xml2.1.3 UserController.ja…...

华为云AI开发平台ModelArts

华为云ModelArts&#xff1a;重塑AI开发流程的“智能引擎”与“创新加速器”&#xff01; 在人工智能浪潮席卷全球的2025年&#xff0c;企业拥抱AI的意愿空前高涨&#xff0c;但技术门槛高、流程复杂、资源投入巨大的现实&#xff0c;却让许多创新构想止步于实验室。数据科学家…...

渗透实战PortSwigger靶场-XSS Lab 14:大多数标签和属性被阻止

<script>标签被拦截 我们需要把全部可用的 tag 和 event 进行暴力破解 XSS cheat sheet&#xff1a; https://portswigger.net/web-security/cross-site-scripting/cheat-sheet 通过爆破发现body可以用 再把全部 events 放进去爆破 这些 event 全部可用 <body onres…...

【决胜公务员考试】求职OMG——见面课测验1

2025最新版&#xff01;&#xff01;&#xff01;6.8截至答题&#xff0c;大家注意呀&#xff01; 博主码字不易点个关注吧,祝期末顺利~~ 1.单选题(2分) 下列说法错误的是:&#xff08; B &#xff09; A.选调生属于公务员系统 B.公务员属于事业编 C.选调生有基层锻炼的要求 D…...

三体问题详解

从物理学角度&#xff0c;三体问题之所以不稳定&#xff0c;是因为三个天体在万有引力作用下相互作用&#xff0c;形成一个非线性耦合系统。我们可以从牛顿经典力学出发&#xff0c;列出具体的运动方程&#xff0c;并说明为何这个系统本质上是混沌的&#xff0c;无法得到一般解…...

【论文阅读28】-CNN-BiLSTM-Attention-(2024)

本文把滑坡位移序列拆开、筛优质因子&#xff0c;再用 CNN-BiLSTM-Attention 来动态预测每个子序列&#xff0c;最后重构出总位移&#xff0c;预测效果超越传统模型。 文章目录 1 引言2 方法2.1 位移时间序列加性模型2.2 变分模态分解 (VMD) 具体步骤2.3.1 样本熵&#xff08;S…...

AI书签管理工具开发全记录(十九):嵌入资源处理

1.前言 &#x1f4dd; 在上一篇文章中&#xff0c;我们完成了书签的导入导出功能。本篇文章我们研究如何处理嵌入资源&#xff0c;方便后续将资源打包到一个可执行文件中。 2.embed介绍 &#x1f3af; Go 1.16 引入了革命性的 embed 包&#xff0c;彻底改变了静态资源管理的…...

Hive 存储格式深度解析:从 TextFile 到 ORC,如何选对数据存储方案?

在大数据处理领域&#xff0c;Hive 作为 Hadoop 生态中重要的数据仓库工具&#xff0c;其存储格式的选择直接影响数据存储成本、查询效率和计算资源消耗。面对 TextFile、SequenceFile、Parquet、RCFile、ORC 等多种存储格式&#xff0c;很多开发者常常陷入选择困境。本文将从底…...

Linux C语言网络编程详细入门教程:如何一步步实现TCP服务端与客户端通信

文章目录 Linux C语言网络编程详细入门教程&#xff1a;如何一步步实现TCP服务端与客户端通信前言一、网络通信基础概念二、服务端与客户端的完整流程图解三、每一步的详细讲解和代码示例1. 创建Socket&#xff08;服务端和客户端都要&#xff09;2. 绑定本地地址和端口&#x…...

SiFli 52把Imagie图片,Font字体资源放在指定位置,编译成指定img.bin和font.bin的问题

分区配置 (ptab.json) img 属性介绍&#xff1a; img 属性指定分区存放的 image 名称&#xff0c;指定的 image 名称必须是当前工程生成的 binary 。 如果 binary 有多个文件&#xff0c;则以 proj_name:binary_name 格式指定文件名&#xff0c; proj_name 为工程 名&…...