|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/scheduler.dart'; |
| 3 | +import 'package:flutter/services.dart'; |
| 4 | +import 'package:mdi/_prefs.dart'; |
| 5 | + |
| 6 | +const _promptIntro = 'Welcome to Dummy Terminal (v0.0.1)'; |
| 7 | +const _promptPrefix = r'user@local ~ $ '; |
| 8 | +const _textStyle = TextStyle(fontSize: 14, color: Colors.white); |
| 9 | + |
| 10 | +const _charRevealDuration = Duration(milliseconds: 200); |
| 11 | +const _cursorBlinkDuration = Duration(milliseconds: 400); |
| 12 | + |
| 13 | +class Terminal extends StatefulWidget { |
| 14 | + const Terminal({super.key}); |
| 15 | + |
| 16 | + @override |
| 17 | + State<Terminal> createState() => _TerminalState(); |
| 18 | +} |
| 19 | + |
| 20 | +class _TerminalState extends State<Terminal> { |
| 21 | + late final _scrollController = ScrollController(); |
| 22 | + late final _focus = FocusNode(); |
| 23 | + |
| 24 | + final _lines = <String>[]; |
| 25 | + final _chars = <String>[_promptPrefix]; |
| 26 | + |
| 27 | + @override |
| 28 | + void dispose() { |
| 29 | + _scrollController.dispose(); |
| 30 | + _focus.dispose(); |
| 31 | + super.dispose(); |
| 32 | + } |
| 33 | + |
| 34 | + void _onKey(RawKeyEvent event) { |
| 35 | + if (event is RawKeyDownEvent) { |
| 36 | + if (event.logicalKey == LogicalKeyboardKey.enter) { |
| 37 | + setState(() { |
| 38 | + _lines.add(_chars.join()); |
| 39 | + _chars |
| 40 | + ..clear() |
| 41 | + ..add(_promptPrefix); |
| 42 | + }); |
| 43 | + SchedulerBinding.instance.addPostFrameCallback( |
| 44 | + (_) async => _scrollController.jumpTo( |
| 45 | + _scrollController.position.maxScrollExtent, |
| 46 | + ), |
| 47 | + ); |
| 48 | + } else if (event.logicalKey == LogicalKeyboardKey.backspace) { |
| 49 | + if (_chars.length > 1) { |
| 50 | + setState(_chars.removeLast); |
| 51 | + } |
| 52 | + } else { |
| 53 | + final character = event.character; |
| 54 | + if (character != null) { |
| 55 | + setState(() => _chars.add(character)); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @override |
| 62 | + Widget build(BuildContext context) => MouseRegion( |
| 63 | + // TODO(albert): check if this can be done on higher level |
| 64 | + // when window receives focus for example. |
| 65 | + onEnter: (_) => _focus.requestFocus(), |
| 66 | + child: Padding( |
| 67 | + padding: const EdgeInsets.only(left: 6), |
| 68 | + child: RawKeyboardListener( |
| 69 | + focusNode: _focus, |
| 70 | + autofocus: true, |
| 71 | + onKey: _onKey, |
| 72 | + child: RawScrollbar( |
| 73 | + thumbColor: titleBarTextColor, |
| 74 | + thickness: 5, |
| 75 | + child: DefaultTextStyle.merge( |
| 76 | + style: _textStyle, |
| 77 | + child: ListView( |
| 78 | + controller: _scrollController, |
| 79 | + children: [ |
| 80 | + const Text(_promptIntro), |
| 81 | + const SizedBox(height: 8), |
| 82 | + ..._lines.map(Text.new), |
| 83 | + Wrap( |
| 84 | + children: [ |
| 85 | + ..._chars.map(_Character.new), |
| 86 | + const _Cursor(), |
| 87 | + ], |
| 88 | + ), |
| 89 | + ], |
| 90 | + ), |
| 91 | + ), |
| 92 | + ), |
| 93 | + ), |
| 94 | + ), |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +class _Character extends StatelessWidget { |
| 99 | + const _Character(this.char); |
| 100 | + |
| 101 | + final String char; |
| 102 | + |
| 103 | + @override |
| 104 | + Widget build(BuildContext context) => _Animated( |
| 105 | + _charRevealDuration, |
| 106 | + onInit: (controller) => controller.forward(), |
| 107 | + child: Text(char), |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +class _Cursor extends StatelessWidget { |
| 112 | + const _Cursor(); |
| 113 | + |
| 114 | + @override |
| 115 | + Widget build(BuildContext context) => _Animated( |
| 116 | + _cursorBlinkDuration, |
| 117 | + onInit: (controller) => controller.repeat(reverse: true), |
| 118 | + child: const Text('▌'), |
| 119 | + ); |
| 120 | +} |
| 121 | + |
| 122 | +class _Animated extends StatefulWidget { |
| 123 | + const _Animated( |
| 124 | + this.animationDuration, { |
| 125 | + required this.onInit, |
| 126 | + required this.child, |
| 127 | + }); |
| 128 | + |
| 129 | + final Duration animationDuration; |
| 130 | + final void Function(AnimationController controller) onInit; |
| 131 | + final Widget child; |
| 132 | + |
| 133 | + @override |
| 134 | + _AnimatedState createState() => _AnimatedState(); |
| 135 | +} |
| 136 | + |
| 137 | +class _AnimatedState extends State<_Animated> |
| 138 | + with SingleTickerProviderStateMixin { |
| 139 | + late final _controller = AnimationController( |
| 140 | + vsync: this, |
| 141 | + duration: widget.animationDuration, |
| 142 | + ); |
| 143 | + |
| 144 | + @override |
| 145 | + void initState() { |
| 146 | + super.initState(); |
| 147 | + widget.onInit(_controller); |
| 148 | + } |
| 149 | + |
| 150 | + @override |
| 151 | + void dispose() { |
| 152 | + _controller.dispose(); |
| 153 | + super.dispose(); |
| 154 | + } |
| 155 | + |
| 156 | + @override |
| 157 | + Widget build(BuildContext context) => FadeTransition( |
| 158 | + opacity: _controller, |
| 159 | + child: widget.child, |
| 160 | + ); |
| 161 | +} |
0 commit comments