|
| 1 | +library flutter_console_widget; |
| 2 | + |
| 3 | +export 'package:flutter_console_widget/flutter_console_controller.dart'; |
| 4 | + |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | +import 'package:flutter_console_widget/flutter_console_controller.dart'; |
| 7 | +import 'package:flutter_console_widget/selectable_colored_text.dart'; |
| 8 | + |
| 9 | +class FlutterConsole extends StatelessWidget { |
| 10 | + const FlutterConsole({ |
| 11 | + this.consoleBackground = Colors.black, |
| 12 | + this.consoleTextColor = Colors.white, |
| 13 | + this.consoleSelectedTextBackgroundColor = Colors.white, |
| 14 | + this.consoleSelectedTextColor = Colors.black, |
| 15 | + required this.controller, |
| 16 | + required this.height, |
| 17 | + this.inputBackground = const Color(0xff333333), |
| 18 | + this.inputTextColor = Colors.white, |
| 19 | + Key? key, |
| 20 | + this.scrollColor = Colors.grey, |
| 21 | + required this.width, |
| 22 | + }) : super(key: key); |
| 23 | + |
| 24 | + final Color consoleBackground; |
| 25 | + final Color consoleTextColor; |
| 26 | + final Color consoleSelectedTextBackgroundColor; |
| 27 | + final Color consoleSelectedTextColor; |
| 28 | + final FlutterConsoleController controller; |
| 29 | + final double height; |
| 30 | + final Color inputBackground; |
| 31 | + final Color inputTextColor; |
| 32 | + final Color scrollColor; |
| 33 | + final double width; |
| 34 | + |
| 35 | + @override |
| 36 | + Widget build(BuildContext context) { |
| 37 | + return ValueListenableBuilder<FlutterConsoleData>( |
| 38 | + valueListenable: controller, |
| 39 | + builder: (context, consoleData, _) { |
| 40 | + return !consoleData.show |
| 41 | + ? const SizedBox.shrink() |
| 42 | + : Container( |
| 43 | + color: consoleBackground, |
| 44 | + width: width, |
| 45 | + height: height, |
| 46 | + child: Column( |
| 47 | + mainAxisSize: MainAxisSize.min, |
| 48 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 49 | + children: [ |
| 50 | + Expanded( |
| 51 | + child: SizedBox( |
| 52 | + width: width, |
| 53 | + child: Theme( |
| 54 | + data: ThemeData.light().copyWith( |
| 55 | + scrollbarTheme: |
| 56 | + const ScrollbarThemeData().copyWith( |
| 57 | + thumbColor: WidgetStateProperty.all(scrollColor), |
| 58 | + )), |
| 59 | + child: Scrollbar( |
| 60 | + controller: controller.scrollController, |
| 61 | + child: SingleChildScrollView( |
| 62 | + physics: const BouncingScrollPhysics(), |
| 63 | + controller: controller.scrollController, |
| 64 | + child: Padding( |
| 65 | + padding: const EdgeInsets.all(8.0), |
| 66 | + child: SelectableColoredText( |
| 67 | + consoleData.consoleContent, |
| 68 | + style: TextStyle( |
| 69 | + color: consoleTextColor, |
| 70 | + ), |
| 71 | + selectedTextColor: consoleSelectedTextColor, |
| 72 | + selectedTextBackgroundColor: |
| 73 | + consoleSelectedTextBackgroundColor, |
| 74 | + ), |
| 75 | + ), |
| 76 | + ), |
| 77 | + ), |
| 78 | + ), |
| 79 | + ), |
| 80 | + ), |
| 81 | + Container( |
| 82 | + color: inputBackground, |
| 83 | + padding: const EdgeInsets.only(bottom: 5.0), |
| 84 | + child: Row( |
| 85 | + children: [ |
| 86 | + const SizedBox( |
| 87 | + width: 10, |
| 88 | + ), |
| 89 | + Icon( |
| 90 | + Icons.arrow_forward_ios, |
| 91 | + color: inputTextColor, |
| 92 | + size: 20, |
| 93 | + ), |
| 94 | + Flexible( |
| 95 | + child: TextField( |
| 96 | + autofocus: true, |
| 97 | + keyboardType: consoleData.keyboardType, |
| 98 | + controller: controller.inputController, |
| 99 | + focusNode: controller.focusNode, |
| 100 | + cursorColor: inputTextColor, |
| 101 | + style: TextStyle(color: inputTextColor), |
| 102 | + decoration: InputDecoration( |
| 103 | + filled: true, |
| 104 | + fillColor: inputBackground, |
| 105 | + border: InputBorder.none, |
| 106 | + contentPadding: const EdgeInsets.only( |
| 107 | + right: 10, |
| 108 | + left: 5, |
| 109 | + ), |
| 110 | + ), |
| 111 | + onSubmitted: (value) { |
| 112 | + if (!controller.completer.isCompleted) { |
| 113 | + controller.completer.complete( |
| 114 | + value, |
| 115 | + ); |
| 116 | + } |
| 117 | + }, |
| 118 | + ), |
| 119 | + ), |
| 120 | + ElevatedButton( |
| 121 | + style: ElevatedButton.styleFrom( |
| 122 | + backgroundColor: consoleBackground, |
| 123 | + ), |
| 124 | + onPressed: () { |
| 125 | + if (!controller.completer.isCompleted) { |
| 126 | + controller.completer.complete( |
| 127 | + controller.inputController.text, |
| 128 | + ); |
| 129 | + } |
| 130 | + }, |
| 131 | + child: const Icon(Icons.done), |
| 132 | + ), |
| 133 | + const SizedBox( |
| 134 | + width: 10, |
| 135 | + ), |
| 136 | + ], |
| 137 | + ), |
| 138 | + ) |
| 139 | + ], |
| 140 | + ), |
| 141 | + ); |
| 142 | + }); |
| 143 | + } |
| 144 | +} |
0 commit comments