当前位置: 首页 > 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光流法是一种计算图像序列中物体运动的光…...

【OSG学习笔记】Day 18: 碰撞检测与物理交互

物理引擎&#xff08;Physics Engine&#xff09; 物理引擎 是一种通过计算机模拟物理规律&#xff08;如力学、碰撞、重力、流体动力学等&#xff09;的软件工具或库。 它的核心目标是在虚拟环境中逼真地模拟物体的运动和交互&#xff0c;广泛应用于 游戏开发、动画制作、虚…...

Appium+python自动化(十六)- ADB命令

简介 Android 调试桥(adb)是多种用途的工具&#xff0c;该工具可以帮助你你管理设备或模拟器 的状态。 adb ( Android Debug Bridge)是一个通用命令行工具&#xff0c;其允许您与模拟器实例或连接的 Android 设备进行通信。它可为各种设备操作提供便利&#xff0c;如安装和调试…...

循环冗余码校验CRC码 算法步骤+详细实例计算

通信过程&#xff1a;&#xff08;白话解释&#xff09; 我们将原始待发送的消息称为 M M M&#xff0c;依据发送接收消息双方约定的生成多项式 G ( x ) G(x) G(x)&#xff08;意思就是 G &#xff08; x ) G&#xff08;x) G&#xff08;x) 是已知的&#xff09;&#xff0…...

Keil 中设置 STM32 Flash 和 RAM 地址详解

文章目录 Keil 中设置 STM32 Flash 和 RAM 地址详解一、Flash 和 RAM 配置界面(Target 选项卡)1. IROM1(用于配置 Flash)2. IRAM1(用于配置 RAM)二、链接器设置界面(Linker 选项卡)1. 勾选“Use Memory Layout from Target Dialog”2. 查看链接器参数(如果没有勾选上面…...

微服务商城-商品微服务

数据表 CREATE TABLE product (id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 商品id,cateid smallint(6) UNSIGNED NOT NULL DEFAULT 0 COMMENT 类别Id,name varchar(100) NOT NULL DEFAULT COMMENT 商品名称,subtitle varchar(200) NOT NULL DEFAULT COMMENT 商…...

Spring AI与Spring Modulith核心技术解析

Spring AI核心架构解析 Spring AI&#xff08;https://spring.io/projects/spring-ai&#xff09;作为Spring生态中的AI集成框架&#xff0c;其核心设计理念是通过模块化架构降低AI应用的开发复杂度。与Python生态中的LangChain/LlamaIndex等工具类似&#xff0c;但特别为多语…...

Element Plus 表单(el-form)中关于正整数输入的校验规则

目录 1 单个正整数输入1.1 模板1.2 校验规则 2 两个正整数输入&#xff08;联动&#xff09;2.1 模板2.2 校验规则2.3 CSS 1 单个正整数输入 1.1 模板 <el-formref"formRef":model"formData":rules"formRules"label-width"150px"…...

大数据学习(132)-HIve数据分析

​​​​&#x1f34b;&#x1f34b;大数据学习&#x1f34b;&#x1f34b; &#x1f525;系列专栏&#xff1a; &#x1f451;哲学语录: 用力所能及&#xff0c;改变世界。 &#x1f496;如果觉得博主的文章还不错的话&#xff0c;请点赞&#x1f44d;收藏⭐️留言&#x1f4…...

服务器--宝塔命令

一、宝塔面板安装命令 ⚠️ 必须使用 root 用户 或 sudo 权限执行&#xff01; sudo su - 1. CentOS 系统&#xff1a; yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh2. Ubuntu / Debian 系统…...

React---day11

14.4 react-redux第三方库 提供connect、thunk之类的函数 以获取一个banner数据为例子 store&#xff1a; 我们在使用异步的时候理应是要使用中间件的&#xff0c;但是configureStore 已经自动集成了 redux-thunk&#xff0c;注意action里面要返回函数 import { configureS…...