main.dart 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import 'package:flutter/material.dart';
  2. import 'package:xterm/flutter.dart';
  3. import 'package:xterm/xterm.dart';
  4. void main() {
  5. runApp(MyApp());
  6. }
  7. class MyApp extends StatelessWidget {
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'xterm.dart demo',
  12. theme: ThemeData(
  13. primarySwatch: Colors.blue,
  14. visualDensity: VisualDensity.adaptivePlatformDensity,
  15. ),
  16. home: MyHomePage(),
  17. );
  18. }
  19. }
  20. class MyHomePage extends StatefulWidget {
  21. MyHomePage({Key key}) : super(key: key);
  22. @override
  23. _MyHomePageState createState() => _MyHomePageState();
  24. }
  25. class _MyHomePageState extends State<MyHomePage> {
  26. Terminal terminal;
  27. @override
  28. void initState() {
  29. super.initState();
  30. terminal = Terminal(
  31. onInput: onInput,
  32. maxLines: 10000,
  33. );
  34. terminal.write('xterm.dart demo');
  35. terminal.write('\r\n');
  36. terminal.write('\$ ');
  37. }
  38. void onInput(String input) {
  39. // in a "real" terminal emulation you would connect onInput to the backend
  40. // (like a pty or ssh connection) that then handles the changes in the
  41. // terminal.
  42. // As we don't have a connected backend here we simulate the changes by
  43. // directly writing to the terminal.
  44. if (input == '\r') {
  45. terminal.write('\r\n');
  46. terminal.write('\$ ');
  47. } else if (input.codeUnitAt(0) == 127) {
  48. terminal.buffer.eraseCharacters(1);
  49. terminal.buffer.backspace();
  50. terminal.refresh();
  51. } else {
  52. terminal.write(input);
  53. }
  54. }
  55. @override
  56. Widget build(BuildContext context) {
  57. return Scaffold(
  58. body: SafeArea(
  59. child: TerminalView(terminal: terminal),
  60. ),
  61. );
  62. }
  63. }