| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import 'dart:async';
- import 'dart:convert';
- import 'package:dartssh2/dartssh2.dart';
- import 'package:example/src/virtual_keyboard.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:xterm/xterm.dart';
- const host = 'localhost';
- const port = 22;
- const username = '<your username>';
- const password = '<your password>';
- void main() {
- runApp(MyApp());
- }
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return CupertinoApp(
- title: 'xterm.dart demo',
- home: MyHomePage(),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- MyHomePage({super.key});
- @override
- // ignore: library_private_types_in_public_api
- _MyHomePageState createState() => _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> {
- late final terminal = Terminal(inputHandler: keyboard);
- final keyboard = VirtualKeyboard(defaultInputHandler);
- var title = host;
- @override
- void initState() {
- super.initState();
- initTerminal();
- }
- Future<void> initTerminal() async {
- terminal.write('Connecting...\r\n');
- final client = SSHClient(
- await SSHSocket.connect(host, port),
- username: username,
- onPasswordRequest: () => password,
- );
- terminal.write('Connected\r\n');
- final session = await client.shell(
- pty: SSHPtyConfig(
- width: terminal.viewWidth,
- height: terminal.viewHeight,
- ),
- );
- terminal.buffer.clear();
- terminal.buffer.setCursor(0, 0);
- terminal.onTitleChange = (title) {
- setState(() => this.title = title);
- };
- terminal.onResize = (width, height, pixelWidth, pixelHeight) {
- session.resizeTerminal(width, height, pixelWidth, pixelHeight);
- };
- terminal.onOutput = (data) {
- session.write(utf8.encode(data));
- };
- session.stdout
- .cast<List<int>>()
- .transform(Utf8Decoder())
- .listen(terminal.write);
- session.stderr
- .cast<List<int>>()
- .transform(Utf8Decoder())
- .listen(terminal.write);
- }
- @override
- Widget build(BuildContext context) {
- return CupertinoPageScaffold(
- navigationBar: CupertinoNavigationBar(
- middle: Text(title),
- backgroundColor:
- CupertinoTheme.of(context).barBackgroundColor.withOpacity(0.5),
- ),
- child: Column(
- children: [
- Expanded(
- child: TerminalView(terminal),
- ),
- VirtualKeyboardView(keyboard),
- ],
- ),
- );
- }
- }
|