diff --git a/win_text_editor/lib/modules/call_function/widgets/call_function_right_side.dart b/win_text_editor/lib/modules/call_function/widgets/call_function_right_side.dart index d39a2a9..5247636 100644 --- a/win_text_editor/lib/modules/call_function/widgets/call_function_right_side.dart +++ b/win_text_editor/lib/modules/call_function/widgets/call_function_right_side.dart @@ -1,20 +1,24 @@ +// call_function_right_side.dart import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; import 'package:win_text_editor/modules/call_function/controllers/call_function_controller.dart'; -import 'package:win_text_editor/shared/components/my_checkbox.dart'; +import 'package:win_text_editor/shared/components/code_generation_components.dart'; class CallFunctionRightSide extends StatefulWidget { final CallFunctionController controller; final TextEditingController codeController; - const CallFunctionRightSide({super.key, required this.controller, required this.codeController}); + const CallFunctionRightSide({ + super.key, + required this.controller, + required this.codeController, + }); @override State createState() => _CallFunctionRightSideState(); } class _CallFunctionRightSideState extends State { - final List _selectedOperations = []; + String? _selectedOperation; @override void initState() { @@ -30,100 +34,32 @@ class _CallFunctionRightSideState extends State { } void _updateDisplay() { - widget.codeController.text = widget.controller.genCodeString(_selectedOperations)!; - } - - @override - Widget build(BuildContext context) { - return Column( - children: [ - _buildCheckboxSection(), - Padding( - padding: const EdgeInsets.only(left: 8.0, right: 8.0), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - const Text('生成代码:', style: TextStyle(fontWeight: FontWeight.bold)), - IconButton( - icon: const Icon(Icons.content_copy, size: 20), - tooltip: '复制代码', - onPressed: () { - if (widget.codeController.text.isNotEmpty) { - Clipboard.setData(ClipboardData(text: widget.codeController.text)); - ScaffoldMessenger.of( - context, - ).showSnackBar(const SnackBar(content: Text('已复制到剪贴板'))); + if (_selectedOperation != null) { + widget.codeController.text = + widget.controller.genCodeString([_selectedOperation!]) ?? ''; + } else { + widget.codeController.text = ''; } - }, - ), - ], - ), - ), - Flexible(child: _buildCodeEditor()), - ], - ); - } - - Widget _buildCheckboxSection() { - final operations = ['普通调用', '事务调用']; - - return SizedBox( - width: double.infinity, - child: Card( - child: Padding( - padding: const EdgeInsets.only(left: 8, top: 4, bottom: 12), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Wrap( - spacing: 8, - runSpacing: 8, - children: operations.map((op) => _buildCheckbox(op)).toList(), - ), - ], - ), - ), - ), - ); } - Widget _buildCheckbox(String label) => MyCheckbox( - title: label, - value: _selectedOperations.contains(label), - onChanged: (bool? value) => _toggleOperation(label, value), - ); - - void _toggleOperation(String operation, bool? value) { + void _selectOperation(String? operation) { setState(() { - if (value == true) { - _selectedOperations.add(operation); - } else { - _selectedOperations.remove(operation); - } + _selectedOperation = operation; _updateDisplay(); }); } - Widget _buildCodeEditor() { - return Card( - child: Padding( - padding: const EdgeInsets.all(8), - child: Container( - decoration: BoxDecoration( - border: Border.all(color: Colors.grey), - borderRadius: BorderRadius.circular(4), - ), - child: TextField( - controller: widget.codeController, - maxLines: null, - expands: true, - decoration: const InputDecoration( - border: InputBorder.none, - contentPadding: EdgeInsets.all(8), - ), - style: const TextStyle(fontFamily: 'monospace', color: Colors.blueAccent), - ), - ), + @override + Widget build(BuildContext context) { + final operations = ['普通调用', '事务调用']; + + return CodeGenerationSection( + title: '生成代码', + codeController: widget.codeController, + child: OperationRadioSection( + operations: operations, + selectedOperation: _selectedOperation, + onOperationSelected: _selectOperation, ), ); } diff --git a/win_text_editor/lib/modules/memory_table/widgets/memory_table_right_side.dart b/win_text_editor/lib/modules/memory_table/widgets/memory_table_right_side.dart index 2614527..5a1b426 100644 --- a/win_text_editor/lib/modules/memory_table/widgets/memory_table_right_side.dart +++ b/win_text_editor/lib/modules/memory_table/widgets/memory_table_right_side.dart @@ -1,19 +1,25 @@ +// memory_table_right_side.dart import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:win_text_editor/modules/memory_table/controllers/memory_table_controller.dart'; +import 'package:win_text_editor/shared/components/code_generation_components.dart'; class MemoryTableRightSide extends StatefulWidget { final MemoryTableController controller; final TextEditingController codeController; - const MemoryTableRightSide({super.key, required this.controller, required this.codeController}); + const MemoryTableRightSide({ + super.key, + required this.controller, + required this.codeController, + }); @override State createState() => _MemoryTableRightSideState(); } class _MemoryTableRightSideState extends State { - String? _selectedOperation; // 改为可空的单个字符串 + String? _selectedOperation; @override void initState() { @@ -30,13 +36,14 @@ class _MemoryTableRightSideState extends State { void _updateDisplay() { if (_selectedOperation != null) { - widget.codeController.text = widget.controller.genCodeString([_selectedOperation!]) ?? ''; + widget.codeController.text = + widget.controller.genCodeString([_selectedOperation!]) ?? ''; } else { widget.codeController.text = ''; } } - void _selectOperation(String operation) { + void _selectOperation(String? operation) { setState(() { _selectedOperation = operation; _updateDisplay(); @@ -45,100 +52,17 @@ class _MemoryTableRightSideState extends State { @override Widget build(BuildContext context) { - return Column( - children: [ - _buildRadioSection(), - Padding( - padding: const EdgeInsets.only(left: 8.0, right: 8.0), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - const Text('生成代码:', style: TextStyle(fontWeight: FontWeight.bold)), - IconButton( - icon: const Icon(Icons.content_copy, size: 20), - tooltip: '复制代码', - onPressed: () { - if (widget.codeController.text.isNotEmpty) { - Clipboard.setData(ClipboardData(text: widget.codeController.text)); - ScaffoldMessenger.of( - context, - ).showSnackBar(const SnackBar(content: Text('已复制到剪贴板'))); - } - }, - ), - ], - ), - ), - Flexible(child: _buildCodeEditor()), - ], - ); - } - - Widget _buildRadioSection() { - final operations = ['获取记录', '获取记录数', '插入记录', '修改记录', '删除记录', '遍历记录']; - - return SizedBox( - width: double.infinity, - child: Card( - child: Padding( - padding: const EdgeInsets.all(12), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Wrap( - spacing: 16, // 水平间距 - runSpacing: 8, // 垂直间距 - children: operations.map((op) => _buildRadioItem(op)).toList(), - ), - ], - ), - ), - ), - ); - } - - Widget _buildRadioItem(String label) { - return Row( - mainAxisSize: MainAxisSize.min, // 使Row只占用必要宽度 - children: [ - Transform.scale( - scale: 0.75, - child: Radio( - value: label, - groupValue: _selectedOperation, - onChanged: (String? value) { - if (value != null) { - _selectOperation(value); - } - }, - ), - ), - const SizedBox(width: 4), - Text(label, style: const TextStyle(fontSize: 14)), - ], - ); - } + final operations = [ + '获取记录', '获取记录数', '插入记录', '修改记录', '删除记录', '遍历记录' + ]; - Widget _buildCodeEditor() { - return Card( - child: Padding( - padding: const EdgeInsets.all(8), - child: Container( - decoration: BoxDecoration( - border: Border.all(color: Colors.grey), - borderRadius: BorderRadius.circular(4), - ), - child: TextField( - controller: widget.codeController, - maxLines: null, - expands: true, - decoration: const InputDecoration( - border: InputBorder.none, - contentPadding: EdgeInsets.all(8), - ), - style: const TextStyle(fontFamily: 'monospace', color: Colors.blueAccent), - ), - ), + return CodeGenerationSection( + title: '生成代码', + codeController: widget.codeController, + child: OperationRadioSection( + operations: operations, + selectedOperation: _selectedOperation, + onOperationSelected: _selectOperation, ), ); } diff --git a/win_text_editor/lib/modules/uft_component/widgets/uft_component_right_side.dart b/win_text_editor/lib/modules/uft_component/widgets/uft_component_right_side.dart index 352586b..b645f3c 100644 --- a/win_text_editor/lib/modules/uft_component/widgets/uft_component_right_side.dart +++ b/win_text_editor/lib/modules/uft_component/widgets/uft_component_right_side.dart @@ -1,20 +1,24 @@ +// uft_component_right_side.dart import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; import 'package:win_text_editor/modules/uft_component/controllers/uft_component_controller.dart'; -import 'package:win_text_editor/shared/components/my_checkbox.dart'; +import 'package:win_text_editor/shared/components/code_generation_components.dart'; class UftComponentRightSide extends StatefulWidget { final UftComponentController controller; final TextEditingController codeController; - const UftComponentRightSide({super.key, required this.controller, required this.codeController}); + const UftComponentRightSide({ + super.key, + required this.controller, + required this.codeController, + }); @override State createState() => _UftComponentRightSideState(); } class _UftComponentRightSideState extends State { - final List _selectedOperations = []; + String? _selectedOperation; @override void initState() { @@ -30,100 +34,34 @@ class _UftComponentRightSideState extends State { } void _updateDisplay() { - widget.codeController.text = widget.controller.genCodeString(_selectedOperations)!; + if (_selectedOperation != null) { + widget.codeController.text = + widget.controller.genCodeString([_selectedOperation!]) ?? ''; + } else { + widget.codeController.text = ''; + } } - void _toggleOperation(String operation, bool? value) { + void _selectOperation(String? operation) { setState(() { - if (value == true) { - _selectedOperations.add(operation); - } else { - _selectedOperations.remove(operation); - } + _selectedOperation = operation; _updateDisplay(); }); } @override Widget build(BuildContext context) { - return Column( - children: [ - _buildCheckboxSection(), - Padding( - padding: const EdgeInsets.only(left: 8.0, right: 8.0), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - const Text('生成代码:', style: TextStyle(fontWeight: FontWeight.bold)), - IconButton( - icon: const Icon(Icons.content_copy, size: 20), - tooltip: '复制代码', - onPressed: () { - if (widget.codeController.text.isNotEmpty) { - Clipboard.setData(ClipboardData(text: widget.codeController.text)); - ScaffoldMessenger.of( - context, - ).showSnackBar(const SnackBar(content: Text('已复制到剪贴板'))); - } - }, - ), - ], - ), - ), - Flexible(child: _buildCodeEditor()), - ], - ); - } - - Widget _buildCheckboxSection() { - final operations = ['插入组件', '修改组件', '获取组件', '遍历组件', '组件大小', '尾部插入组件']; - - return SizedBox( - width: double.infinity, - child: Card( - child: Padding( - padding: const EdgeInsets.only(left: 8, top: 4, bottom: 12), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Wrap( - spacing: 8, - runSpacing: 8, - children: operations.map((op) => _buildCheckbox(op)).toList(), - ), - ], - ), - ), - ), - ); - } - - Widget _buildCheckbox(String label) => MyCheckbox( - title: label, - value: _selectedOperations.contains(label), - onChanged: (bool? value) => _toggleOperation(label, value), - ); + final operations = [ + '插入组件', '修改组件', '获取组件', '遍历组件', '组件大小', '尾部插入组件' + ]; - Widget _buildCodeEditor() { - return Card( - child: Padding( - padding: const EdgeInsets.all(8), - child: Container( - decoration: BoxDecoration( - border: Border.all(color: Colors.grey), - borderRadius: BorderRadius.circular(4), - ), - child: TextField( - controller: widget.codeController, - maxLines: null, - expands: true, - decoration: const InputDecoration( - border: InputBorder.none, - contentPadding: EdgeInsets.all(8), - ), - style: const TextStyle(fontFamily: 'monospace', color: Colors.blueAccent), - ), - ), + return CodeGenerationSection( + title: '生成代码', + codeController: widget.codeController, + child: OperationRadioSection( + operations: operations, + selectedOperation: _selectedOperation, + onOperationSelected: _selectOperation, ), ); }