2
This commit is contained in:
1 parent
9fbb414ee6
commit
4554f22c87
233 files changed
+147430
-605
No files matched your search
@@ -0,0 +1,481 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
import 'package:re_editor_exmaple/find.dart';
|
||||
import 'package:re_editor_exmaple/menu.dart';
|
||||
import 'package:re_highlight/languages/dart.dart';
|
||||
import 'package:re_highlight/styles/atom-one-light.dart';
|
||||
|
||||
const List<CodePrompt> _kStringPrompts = [
|
||||
CodeFieldPrompt(
|
||||
word: 'length',
|
||||
type: 'int'
|
||||
),
|
||||
CodeFieldPrompt(
|
||||
word: 'isEmpty',
|
||||
type: 'bool'
|
||||
),
|
||||
CodeFieldPrompt(
|
||||
word: 'isNotEmpty',
|
||||
type: 'bool'
|
||||
),
|
||||
CodeFieldPrompt(
|
||||
word: 'characters',
|
||||
type: 'Characters'
|
||||
),
|
||||
CodeFieldPrompt(
|
||||
word: 'hashCode',
|
||||
type: 'int'
|
||||
),
|
||||
CodeFieldPrompt(
|
||||
word: 'codeUnits',
|
||||
type: 'List<int>'
|
||||
),
|
||||
CodeFieldPrompt(
|
||||
word: 'runes',
|
||||
type: 'Runes'
|
||||
),
|
||||
CodeFunctionPrompt(
|
||||
word: 'codeUnitAt',
|
||||
type: 'int',
|
||||
parameters: {
|
||||
'index': 'int'
|
||||
}
|
||||
),
|
||||
CodeFunctionPrompt(
|
||||
word: 'replaceAll',
|
||||
type: 'String',
|
||||
parameters: {
|
||||
'from': 'Pattern',
|
||||
'replace': 'String',
|
||||
}
|
||||
),
|
||||
CodeFunctionPrompt(
|
||||
word: 'contains',
|
||||
type: 'bool',
|
||||
parameters: {
|
||||
'other': 'String',
|
||||
}
|
||||
),
|
||||
CodeFunctionPrompt(
|
||||
word: 'split',
|
||||
type: 'List<String>',
|
||||
parameters: {
|
||||
'pattern': 'Pattern',
|
||||
}
|
||||
),
|
||||
CodeFunctionPrompt(
|
||||
word: 'endsWith',
|
||||
type: 'bool',
|
||||
parameters: {
|
||||
'other': 'String',
|
||||
}
|
||||
),
|
||||
CodeFunctionPrompt(
|
||||
word: 'startsWith',
|
||||
type: 'bool',
|
||||
parameters: {
|
||||
'other': 'String',
|
||||
}
|
||||
)
|
||||
];
|
||||
|
||||
class AutoCompleteEditor extends StatefulWidget {
|
||||
|
||||
const AutoCompleteEditor({super.key});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _AutoCompleteEditorState();
|
||||
|
||||
}
|
||||
|
||||
class _AutoCompleteEditorState extends State<AutoCompleteEditor> {
|
||||
|
||||
final CodeLineEditingController _controller = CodeLineEditingController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
rootBundle.loadString('assets/code.dart').then((value) {
|
||||
_controller.text = value;
|
||||
});
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CodeAutocomplete(
|
||||
viewBuilder: (context, notifier, onSelected) {
|
||||
return _DefaultCodeAutocompleteListView(
|
||||
notifier: notifier,
|
||||
onSelected: onSelected,
|
||||
);
|
||||
},
|
||||
promptsBuilder: DefaultCodeAutocompletePromptsBuilder(
|
||||
language: langDart,
|
||||
directPrompts: const [
|
||||
CodeFieldPrompt(
|
||||
word: 'foo',
|
||||
type: 'String'
|
||||
),
|
||||
CodeFieldPrompt(
|
||||
word: 'bar',
|
||||
type: 'String'
|
||||
),
|
||||
CodeFunctionPrompt(
|
||||
word: 'hello',
|
||||
type: 'void',
|
||||
parameters: {
|
||||
'value': 'String',
|
||||
}
|
||||
)
|
||||
],
|
||||
relatedPrompts: {
|
||||
'foo': _kStringPrompts,
|
||||
'bar': _kStringPrompts,
|
||||
},
|
||||
),
|
||||
child: CodeEditor(
|
||||
style: CodeEditorStyle(
|
||||
fontSize: 18,
|
||||
codeTheme: CodeHighlightTheme(
|
||||
languages: {
|
||||
'dart': CodeHighlightThemeMode(
|
||||
mode: langDart
|
||||
)
|
||||
},
|
||||
theme: atomOneLightTheme
|
||||
),
|
||||
),
|
||||
controller: _controller,
|
||||
wordWrap: false,
|
||||
indicatorBuilder: (context, editingController, chunkController, notifier) {
|
||||
return Row(
|
||||
children: [
|
||||
DefaultCodeLineNumber(
|
||||
controller: editingController,
|
||||
notifier: notifier,
|
||||
),
|
||||
DefaultCodeChunkIndicator(
|
||||
width: 20,
|
||||
controller: chunkController,
|
||||
notifier: notifier
|
||||
)
|
||||
],
|
||||
);
|
||||
},
|
||||
findBuilder: (context, controller, readOnly) => CodeFindPanelView(controller: controller, readOnly: readOnly),
|
||||
toolbarController: const ContextMenuControllerImpl(),
|
||||
leadingDivider: Container(
|
||||
width: 1,
|
||||
color: Colors.blue
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class _DefaultCodeAutocompleteListView extends StatefulWidget implements PreferredSizeWidget {
|
||||
|
||||
static const double kItemHeight = 26;
|
||||
|
||||
final ValueNotifier<CodeAutocompleteEditingValue> notifier;
|
||||
final ValueChanged<CodeAutocompleteResult> onSelected;
|
||||
|
||||
const _DefaultCodeAutocompleteListView({
|
||||
required this.notifier,
|
||||
required this.onSelected,
|
||||
});
|
||||
|
||||
@override
|
||||
Size get preferredSize => Size(
|
||||
250,
|
||||
// 2 is border size
|
||||
min(kItemHeight * notifier.value.prompts.length, 150) + 2
|
||||
);
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _DefaultCodeAutocompleteListViewState();
|
||||
|
||||
}
|
||||
|
||||
class _DefaultCodeAutocompleteListViewState extends State<_DefaultCodeAutocompleteListView> {
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
widget.notifier.addListener(_onValueChanged);
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
widget.notifier.removeListener(_onValueChanged);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
constraints: BoxConstraints.loose(widget.preferredSize),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey,
|
||||
borderRadius: BorderRadius.circular(6)
|
||||
),
|
||||
child: AutoScrollListView(
|
||||
controller: ScrollController(),
|
||||
initialIndex: widget.notifier.value.index,
|
||||
scrollDirection: Axis.vertical,
|
||||
itemCount: widget.notifier.value.prompts.length,
|
||||
itemBuilder:(context, index) {
|
||||
final CodePrompt prompt = widget.notifier.value.prompts[index];
|
||||
final BorderRadius radius = BorderRadius.only(
|
||||
topLeft: index == 0 ? const Radius.circular(5) : Radius.zero,
|
||||
topRight: index == 0 ? const Radius.circular(5) : Radius.zero,
|
||||
bottomLeft: index == widget.notifier.value.prompts.length - 1 ? const Radius.circular(5) : Radius.zero,
|
||||
bottomRight: index == widget.notifier.value.prompts.length - 1 ? const Radius.circular(5) : Radius.zero,
|
||||
);
|
||||
return InkWell(
|
||||
borderRadius: radius,
|
||||
onTap: () {
|
||||
widget.onSelected(widget.notifier.value.copyWith(
|
||||
index: index
|
||||
).autocomplete);
|
||||
},
|
||||
child: Container(
|
||||
width: double.infinity,
|
||||
height: _DefaultCodeAutocompleteListView.kItemHeight,
|
||||
padding: const EdgeInsets.only(
|
||||
left: 5,
|
||||
right: 5
|
||||
),
|
||||
alignment: Alignment.centerLeft,
|
||||
decoration: BoxDecoration(
|
||||
color: index == widget.notifier.value.index ? const Color.fromARGB(255, 255, 140, 0) : null,
|
||||
borderRadius: radius
|
||||
),
|
||||
child: RichText(
|
||||
text: prompt.createSpan(context, widget.notifier.value.input),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
maxLines: 1,
|
||||
),
|
||||
)
|
||||
);
|
||||
},
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
void _onValueChanged() {
|
||||
setState(() {
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension _CodePromptExtension on CodePrompt {
|
||||
|
||||
InlineSpan createSpan(BuildContext context, String input) {
|
||||
const TextStyle style = TextStyle();
|
||||
final InlineSpan span = style.createSpan(
|
||||
value: word,
|
||||
anchor: input,
|
||||
color: Colors.blue,
|
||||
fontWeight: FontWeight.bold,
|
||||
);
|
||||
final CodePrompt prompt = this;
|
||||
if (prompt is CodeFieldPrompt) {
|
||||
return TextSpan(
|
||||
children: [
|
||||
span,
|
||||
TextSpan(
|
||||
text: ' ${prompt.type}',
|
||||
style: style.copyWith(
|
||||
color: Colors.cyan
|
||||
)
|
||||
)
|
||||
]
|
||||
);
|
||||
}
|
||||
if (prompt is CodeFunctionPrompt) {
|
||||
return TextSpan(
|
||||
children: [
|
||||
span,
|
||||
TextSpan(
|
||||
text: '(...) -> ${prompt.type}',
|
||||
style: style.copyWith(
|
||||
color: Colors.cyan
|
||||
)
|
||||
)
|
||||
]
|
||||
);
|
||||
}
|
||||
return span;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension _TextStyleExtension on TextStyle {
|
||||
|
||||
InlineSpan createSpan({
|
||||
required String value,
|
||||
required String anchor,
|
||||
required Color color,
|
||||
FontWeight? fontWeight,
|
||||
bool casesensitive = false,
|
||||
}) {
|
||||
if (anchor.isEmpty) {
|
||||
return TextSpan(
|
||||
text: value,
|
||||
style: this,
|
||||
);
|
||||
}
|
||||
final int index;
|
||||
if (casesensitive) {
|
||||
index = value.indexOf(anchor);
|
||||
} else {
|
||||
index = value.toLowerCase().indexOf(anchor.toLowerCase());
|
||||
}
|
||||
if (index < 0) {
|
||||
return TextSpan(
|
||||
text: value,
|
||||
style: this,
|
||||
);
|
||||
}
|
||||
return TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: value.substring(0, index),
|
||||
style: this
|
||||
),
|
||||
TextSpan(
|
||||
text: value.substring(index, index + anchor.length),
|
||||
style: copyWith(
|
||||
color: color,
|
||||
fontWeight: fontWeight,
|
||||
)
|
||||
),
|
||||
TextSpan(
|
||||
text: value.substring(index + anchor.length),
|
||||
style: this
|
||||
)
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class AutoScrollListView extends StatefulWidget {
|
||||
|
||||
final ScrollController controller;
|
||||
final IndexedWidgetBuilder itemBuilder;
|
||||
final int itemCount;
|
||||
final int initialIndex;
|
||||
final Axis scrollDirection;
|
||||
|
||||
const AutoScrollListView({
|
||||
super.key,
|
||||
required this.controller,
|
||||
required this.itemBuilder,
|
||||
required this.itemCount,
|
||||
this.initialIndex = 0,
|
||||
this.scrollDirection = Axis.vertical,
|
||||
});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _AutoScrollListViewState();
|
||||
|
||||
}
|
||||
|
||||
class _AutoScrollListViewState extends State<AutoScrollListView> {
|
||||
|
||||
late final List<GlobalKey> _keys;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
_keys = List.generate(widget.itemCount, (index) => GlobalKey());
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_autoScroll();
|
||||
});
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant AutoScrollListView oldWidget) {
|
||||
if (widget.itemCount > oldWidget.itemCount) {
|
||||
_keys.addAll(List.generate(widget.itemCount - oldWidget.itemCount, (index) => GlobalKey()));
|
||||
} else if (widget.itemCount < oldWidget.itemCount) {
|
||||
_keys.sublist(oldWidget.itemCount - widget.itemCount);
|
||||
}
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_autoScroll();
|
||||
});
|
||||
super.didUpdateWidget(oldWidget);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final List<Widget> widgets = [];
|
||||
for (int i = 0; i < widget.itemCount; i++) {
|
||||
widgets.add(Container(
|
||||
key: _keys[i],
|
||||
child: widget.itemBuilder(context, i),
|
||||
));
|
||||
}
|
||||
return SingleChildScrollView(
|
||||
controller: widget.controller,
|
||||
scrollDirection: widget.scrollDirection,
|
||||
child: isHorizontal ? Row(
|
||||
children: widgets,
|
||||
) : Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: widgets,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _autoScroll() {
|
||||
final ScrollController controller = widget.controller;
|
||||
if (!controller.hasClients) {
|
||||
return;
|
||||
}
|
||||
if (controller.position.maxScrollExtent == 0) {
|
||||
return;
|
||||
}
|
||||
double pre = 0;
|
||||
double cur = 0;
|
||||
for (int i = 0; i < _keys.length; i++) {
|
||||
final RenderObject? obj = _keys[i].currentContext?.findRenderObject();
|
||||
if (obj == null || obj is! RenderBox) {
|
||||
continue;
|
||||
}
|
||||
if (isHorizontal) {
|
||||
double width = obj.size.width;
|
||||
if (i == widget.initialIndex) {
|
||||
cur = pre + width;
|
||||
break;
|
||||
}
|
||||
pre += width;
|
||||
} else {
|
||||
double height = obj.size.height;
|
||||
if (i == widget.initialIndex) {
|
||||
cur = pre + height;
|
||||
break;
|
||||
}
|
||||
pre += height;
|
||||
}
|
||||
}
|
||||
if (pre == cur) {
|
||||
return;
|
||||
}
|
||||
if (pre < widget.controller.offset) {
|
||||
controller.jumpTo(pre - 1);
|
||||
} else if (cur > controller.offset + controller.position.viewportDimension) {
|
||||
controller.jumpTo(cur - controller.position.viewportDimension);
|
||||
}
|
||||
}
|
||||
|
||||
bool get isHorizontal => widget.scrollDirection == Axis.horizontal;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
|
||||
class BasicField extends StatelessWidget {
|
||||
|
||||
const BasicField({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CodeEditor(
|
||||
wordWrap: false,
|
||||
controller: CodeLineEditingController.fromText(('${'Hello Reqable💐👏 ' * 10}\n') * 100),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
import 'package:re_editor_exmaple/find.dart';
|
||||
import 'package:re_editor_exmaple/menu.dart';
|
||||
import 'package:re_highlight/languages/json.dart';
|
||||
import 'package:re_highlight/styles/atom-one-light.dart';
|
||||
|
||||
class JsonEditor extends StatefulWidget {
|
||||
|
||||
const JsonEditor({super.key});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _JsonEditorState();
|
||||
|
||||
}
|
||||
|
||||
class _JsonEditorState extends State<JsonEditor> {
|
||||
|
||||
final CodeLineEditingController _controller = CodeLineEditingController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
rootBundle.loadString('assets/code.json').then((value) {
|
||||
_controller.text = value;
|
||||
});
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CodeEditor(
|
||||
style: CodeEditorStyle(
|
||||
codeTheme: CodeHighlightTheme(
|
||||
languages: {
|
||||
'json': CodeHighlightThemeMode(
|
||||
mode: langJson
|
||||
)
|
||||
},
|
||||
theme: atomOneLightTheme
|
||||
),
|
||||
),
|
||||
controller: _controller,
|
||||
wordWrap: false,
|
||||
indicatorBuilder: (context, editingController, chunkController, notifier) {
|
||||
return Row(
|
||||
children: [
|
||||
DefaultCodeLineNumber(
|
||||
controller: editingController,
|
||||
notifier: notifier,
|
||||
),
|
||||
DefaultCodeChunkIndicator(
|
||||
width: 20,
|
||||
controller: chunkController,
|
||||
notifier: notifier
|
||||
)
|
||||
],
|
||||
);
|
||||
},
|
||||
findBuilder: (context, controller, readOnly) => CodeFindPanelView(controller: controller, readOnly: readOnly),
|
||||
toolbarController: const ContextMenuControllerImpl(),
|
||||
leadingDivider: Container(
|
||||
width: 1,
|
||||
color: Colors.blue
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
import 'package:re_editor_exmaple/find.dart';
|
||||
import 'package:re_editor_exmaple/menu.dart';
|
||||
|
||||
class LargeTextEditor extends StatefulWidget {
|
||||
|
||||
const LargeTextEditor({super.key});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _LargeTextEditorState();
|
||||
|
||||
}
|
||||
|
||||
class _LargeTextEditorState extends State<LargeTextEditor> {
|
||||
|
||||
final CodeLineEditingController _controller = CodeLineEditingController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
rootBundle.loadString('assets/large.txt').then((value) {
|
||||
_controller.text = value;
|
||||
});
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CodeEditor(
|
||||
controller: _controller,
|
||||
wordWrap: false,
|
||||
indicatorBuilder: (context, editingController, chunkController, notifier) {
|
||||
return Row(
|
||||
children: [
|
||||
DefaultCodeLineNumber(
|
||||
controller: editingController,
|
||||
notifier: notifier,
|
||||
),
|
||||
DefaultCodeChunkIndicator(
|
||||
width: 20,
|
||||
controller: chunkController,
|
||||
notifier: notifier
|
||||
)
|
||||
],
|
||||
);
|
||||
},
|
||||
findBuilder: (context, controller, readOnly) => CodeFindPanelView(controller: controller, readOnly: readOnly),
|
||||
toolbarController: const ContextMenuControllerImpl(),
|
||||
leadingDivider: Container(
|
||||
width: 1,
|
||||
color: Colors.blue
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
|
||||
const EdgeInsetsGeometry _kDefaultFindMargin = EdgeInsets.only(
|
||||
right: 10
|
||||
);
|
||||
const double _kDefaultFindPanelWidth = 360;
|
||||
const double _kDefaultFindPanelHeight = 36;
|
||||
const double _kDefaultReplacePanelHeight = _kDefaultFindPanelHeight * 2;
|
||||
const double _kDefaultFindIconSize = 16;
|
||||
const double _kDefaultFindIconWidth = 30;
|
||||
const double _kDefaultFindIconHeight = 30;
|
||||
const double _kDefaultFindInputFontSize = 13;
|
||||
const double _kDefaultFindResultFontSize = 12;
|
||||
const EdgeInsetsGeometry _kDefaultFindPadding = EdgeInsets.only(
|
||||
left: 5,
|
||||
right: 5,
|
||||
top: 2.5,
|
||||
bottom: 2.5
|
||||
);
|
||||
const EdgeInsetsGeometry _kDefaultFindInputContentPadding = EdgeInsets.only(
|
||||
left: 5,
|
||||
right: 5,
|
||||
);
|
||||
|
||||
class CodeFindPanelView extends StatelessWidget implements PreferredSizeWidget {
|
||||
|
||||
final CodeFindController controller;
|
||||
final EdgeInsetsGeometry margin;
|
||||
final bool readOnly;
|
||||
final Color? iconColor;
|
||||
final Color? iconSelectedColor;
|
||||
final double iconSize;
|
||||
final double inputFontSize;
|
||||
final double resultFontSize;
|
||||
final Color? inputTextColor;
|
||||
final Color? resultFontColor;
|
||||
final EdgeInsetsGeometry padding;
|
||||
final InputDecoration decoration;
|
||||
|
||||
const CodeFindPanelView({
|
||||
super.key,
|
||||
required this.controller,
|
||||
this.margin = _kDefaultFindMargin,
|
||||
required this.readOnly,
|
||||
this.iconSelectedColor,
|
||||
this.iconColor,
|
||||
this.iconSize = _kDefaultFindIconSize,
|
||||
this.inputFontSize = _kDefaultFindInputFontSize,
|
||||
this.resultFontSize = _kDefaultFindResultFontSize,
|
||||
this.inputTextColor,
|
||||
this.resultFontColor,
|
||||
this.padding = _kDefaultFindPadding,
|
||||
this.decoration = const InputDecoration(
|
||||
filled: true,
|
||||
contentPadding: _kDefaultFindInputContentPadding,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(0)),
|
||||
gapPadding: 0
|
||||
),
|
||||
)
|
||||
});
|
||||
|
||||
@override
|
||||
Size get preferredSize => Size(
|
||||
double.infinity,
|
||||
controller.value == null ? 0 :
|
||||
((controller.value!.replaceMode ? _kDefaultReplacePanelHeight : _kDefaultFindPanelHeight) + margin.vertical)
|
||||
);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (controller.value == null) {
|
||||
return const SizedBox(width: 0, height: 0);
|
||||
}
|
||||
return Container(
|
||||
margin: margin,
|
||||
alignment: Alignment.topRight,
|
||||
height: preferredSize.height,
|
||||
child: SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: SizedBox(
|
||||
width: _kDefaultFindPanelWidth,
|
||||
child: Column(
|
||||
children: [
|
||||
_buildFindInputView(context),
|
||||
if (controller.value!.replaceMode)
|
||||
_buildReplaceInputView(context),
|
||||
],
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFindInputView(BuildContext context) {
|
||||
final CodeFindValue value = controller.value!;
|
||||
final String result;
|
||||
if (value.result == null) {
|
||||
result = 'none';
|
||||
} else {
|
||||
result = '${value.result!.index + 1}/${value.result!.matches.length}';
|
||||
}
|
||||
return Row(
|
||||
children: [
|
||||
SizedBox(
|
||||
width: _kDefaultFindPanelWidth / 1.75,
|
||||
height: _kDefaultFindPanelHeight,
|
||||
child: Stack(
|
||||
alignment: Alignment.center,
|
||||
children: [
|
||||
_buildTextField(
|
||||
context: context,
|
||||
controller: controller.findInputController,
|
||||
focusNode: controller.findInputFocusNode,
|
||||
iconsWidth: _kDefaultFindIconWidth * 1.5
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
_buildCheckText(
|
||||
context: context,
|
||||
text: 'Aa',
|
||||
checked: value.option.caseSensitive,
|
||||
onPressed: () {
|
||||
controller.toggleCaseSensitive();
|
||||
}
|
||||
),
|
||||
_buildCheckText(
|
||||
context: context,
|
||||
text: '.*',
|
||||
checked: value.option.regex,
|
||||
onPressed: () {
|
||||
controller.toggleRegex();
|
||||
}
|
||||
)
|
||||
],
|
||||
)
|
||||
],
|
||||
)
|
||||
),
|
||||
Text(result,
|
||||
style: TextStyle(
|
||||
color: resultFontColor,
|
||||
fontSize: resultFontSize
|
||||
)
|
||||
),
|
||||
Expanded(
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
_buildIconButton(
|
||||
onPressed: value.result == null ? null : () {
|
||||
controller.previousMatch();
|
||||
},
|
||||
icon: Icons.arrow_upward,
|
||||
tooltip: 'Previous'
|
||||
),
|
||||
_buildIconButton(
|
||||
onPressed: value.result == null ? null : () {
|
||||
controller.nextMatch();
|
||||
},
|
||||
icon: Icons.arrow_downward,
|
||||
tooltip: 'Next'
|
||||
),
|
||||
_buildIconButton(
|
||||
onPressed: () {
|
||||
controller.close();
|
||||
},
|
||||
icon: Icons.close,
|
||||
tooltip: 'Close'
|
||||
)
|
||||
],
|
||||
),
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildReplaceInputView(BuildContext context) {
|
||||
final CodeFindValue value = controller.value!;
|
||||
return Row(
|
||||
children: [
|
||||
SizedBox(
|
||||
width: _kDefaultFindPanelWidth / 1.75,
|
||||
height: _kDefaultFindPanelHeight,
|
||||
child: _buildTextField(
|
||||
context: context,
|
||||
controller: controller.replaceInputController,
|
||||
focusNode: controller.replaceInputFocusNode,
|
||||
),
|
||||
),
|
||||
_buildIconButton(
|
||||
onPressed: value.result == null ? null : () {
|
||||
controller.replaceMatch();
|
||||
},
|
||||
icon: Icons.done,
|
||||
tooltip: 'Replace'
|
||||
),
|
||||
_buildIconButton(
|
||||
onPressed: value.result == null ? null : () {
|
||||
controller.replaceAllMatches();
|
||||
},
|
||||
icon: Icons.done_all,
|
||||
tooltip: 'Replace All'
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTextField({
|
||||
required BuildContext context,
|
||||
required TextEditingController controller,
|
||||
required FocusNode focusNode,
|
||||
double iconsWidth = 0,
|
||||
}) {
|
||||
return Padding(
|
||||
padding: padding,
|
||||
child: TextField(
|
||||
maxLines: 1,
|
||||
focusNode: focusNode,
|
||||
style: TextStyle(
|
||||
color: inputTextColor,
|
||||
fontSize: inputFontSize
|
||||
),
|
||||
decoration: decoration.copyWith(
|
||||
contentPadding: (decoration.contentPadding ?? EdgeInsets.zero).add(EdgeInsets.only(
|
||||
right: iconsWidth
|
||||
)
|
||||
)),
|
||||
controller: controller,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCheckText({
|
||||
required BuildContext context,
|
||||
required String text,
|
||||
required bool checked,
|
||||
required VoidCallback onPressed,
|
||||
}) {
|
||||
final Color selectedColor = iconSelectedColor ?? Theme.of(context).primaryColor;
|
||||
return GestureDetector(
|
||||
onTap: onPressed,
|
||||
child: MouseRegion(
|
||||
cursor: SystemMouseCursors.click,
|
||||
child: SizedBox(
|
||||
width: _kDefaultFindIconWidth * 0.75,
|
||||
child: Text(
|
||||
text,
|
||||
style: TextStyle(
|
||||
color: checked ? selectedColor : iconColor,
|
||||
fontSize: inputFontSize,
|
||||
)
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildIconButton({
|
||||
required IconData icon,
|
||||
VoidCallback? onPressed,
|
||||
String? tooltip
|
||||
}) {
|
||||
return IconButton(
|
||||
onPressed: onPressed,
|
||||
icon: Icon(
|
||||
icon,
|
||||
size: iconSize,
|
||||
),
|
||||
constraints: const BoxConstraints(
|
||||
maxWidth: _kDefaultFindIconWidth,
|
||||
maxHeight: _kDefaultFindIconHeight
|
||||
),
|
||||
tooltip: tooltip,
|
||||
splashRadius: max(_kDefaultFindIconWidth, _kDefaultFindIconHeight) / 2,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:re_editor_exmaple/editor_autocomplete.dart';
|
||||
import 'package:re_editor_exmaple/editor_basic_field.dart';
|
||||
import 'package:re_editor_exmaple/editor_json.dart';
|
||||
import 'package:re_editor_exmaple/editor_large_text.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
// This widget is the root of your application.
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Re-Editor',
|
||||
theme: ThemeData(
|
||||
colorScheme: const ColorScheme.light(
|
||||
primary: Color.fromARGB(255, 255, 140, 0),
|
||||
)
|
||||
),
|
||||
home: const MyHomePage(title: 'Re-Editor Demo Page'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({Key? key, required this.title}) : super(key: key);
|
||||
|
||||
// This widget is the home page of your application. It is stateful, meaning
|
||||
// that it has a State object (defined below) that contains fields that affect
|
||||
// how it looks.
|
||||
|
||||
// This class is the configuration for the state. It holds the values (in this
|
||||
// case the title) provided by the parent (in this case the App widget) and
|
||||
// used by the build method of the State. Fields in a Widget subclass are
|
||||
// always marked "final".
|
||||
|
||||
final String title;
|
||||
|
||||
@override
|
||||
State<MyHomePage> createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
|
||||
static const Map<String, Widget> _editors = {
|
||||
'Basic Field': BasicField(),
|
||||
'Json Editor': JsonEditor(),
|
||||
'Auto Complete': AutoCompleteEditor(),
|
||||
'Large Text': LargeTextEditor(),
|
||||
};
|
||||
|
||||
int _index = 0;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final Widget child = _editors.values.elementAt(_index);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.title),
|
||||
),
|
||||
body: Container(
|
||||
margin: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
children: [
|
||||
SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: Row(
|
||||
children: _editors.entries.mapIndexed((index, entry) {
|
||||
return TextButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_index = index;
|
||||
});
|
||||
},
|
||||
child: Text(
|
||||
entry.key,
|
||||
style: TextStyle(
|
||||
color: _index == index ? null : Colors.black
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
color: Colors.grey
|
||||
)
|
||||
),
|
||||
child: child,
|
||||
)
|
||||
)
|
||||
],
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
|
||||
class ContextMenuItemWidget extends PopupMenuItem<void> implements PreferredSizeWidget {
|
||||
|
||||
ContextMenuItemWidget({
|
||||
Key? key,
|
||||
required String text,
|
||||
required VoidCallback onTap,
|
||||
}) : super(
|
||||
key: key,
|
||||
onTap: onTap,
|
||||
child: Text(text)
|
||||
);
|
||||
|
||||
@override
|
||||
Size get preferredSize => const Size(150, 25);
|
||||
|
||||
}
|
||||
|
||||
class ContextMenuControllerImpl implements SelectionToolbarController {
|
||||
|
||||
const ContextMenuControllerImpl();
|
||||
|
||||
@override
|
||||
void hide(BuildContext context) {
|
||||
}
|
||||
|
||||
@override
|
||||
void show({
|
||||
required BuildContext context,
|
||||
required CodeLineEditingController controller,
|
||||
required TextSelectionToolbarAnchors anchors,
|
||||
Rect? renderRect,
|
||||
required LayerLink layerLink,
|
||||
required ValueNotifier<bool> visibility,
|
||||
}) {
|
||||
showMenu(
|
||||
context: context,
|
||||
position: RelativeRect.fromSize(anchors.primaryAnchor & const Size(150, double.infinity),
|
||||
MediaQuery.of(context).size),
|
||||
items: [
|
||||
ContextMenuItemWidget(
|
||||
text: 'Cut',
|
||||
onTap: () {
|
||||
controller.cut();
|
||||
},
|
||||
),
|
||||
ContextMenuItemWidget(
|
||||
text: 'Copy',
|
||||
onTap: () {
|
||||
controller.copy();
|
||||
},
|
||||
),
|
||||
ContextMenuItemWidget(
|
||||
text: 'Paste',
|
||||
onTap: () {
|
||||
controller.paste();
|
||||
},
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user