-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from guchengxi1994/master
llm template
- Loading branch information
Showing
29 changed files
with
3,681 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// ignore_for_file: avoid_init_to_null | ||
|
||
import 'dart:ui'; | ||
|
||
import 'package:all_in_one/llm/template_editor/components/connector_painter.dart'; | ||
import 'package:all_in_one/llm/template_editor/notifiers/chain_flow_notifier.dart'; | ||
import 'package:all_in_one/llm/template_editor/notifiers/chain_flow_state.dart'; | ||
import 'package:all_in_one/src/rust/api/llm_api.dart'; | ||
import 'package:all_in_one/styles/app_style.dart'; | ||
import 'package:auto_size_text/auto_size_text.dart'; | ||
import 'package:collection/collection.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
||
class ChainFlow extends ConsumerStatefulWidget { | ||
const ChainFlow({super.key}); | ||
|
||
@override | ||
ConsumerState<ChainFlow> createState() => _ChainFlowState(); | ||
} | ||
|
||
class _ChainFlowState extends ConsumerState<ChainFlow> { | ||
late final ScrollController controller1 = ScrollController() | ||
..addListener(_scrollListener); | ||
final ScrollController controller2 = ScrollController(); | ||
|
||
late double childrenHeight = 0; | ||
List<String> items = []; | ||
|
||
init() async { | ||
items = | ||
await templateToPrompts(template: ref.read(chainFlowProvider).content); | ||
childrenHeight = items.length * 50; | ||
} | ||
|
||
int? first = null; | ||
String? firstStr = null; | ||
|
||
void _scrollListener() { | ||
if (!controller2.hasClients) { | ||
return; | ||
} | ||
controller2.jumpTo(controller1.offset); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return FutureBuilder( | ||
future: init(), | ||
builder: (c, s) { | ||
if (s.connectionState == ConnectionState.done) { | ||
return Container( | ||
width: 400, | ||
height: double.infinity, | ||
color: Colors.white, | ||
padding: const EdgeInsets.all(10), | ||
child: Row( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
SizedBox( | ||
width: 100, | ||
child: _wrapper2( | ||
SingleChildScrollView( | ||
controller: controller2, | ||
child: CustomPaint( | ||
painter: ConnectorPainter( | ||
bounds: ref | ||
.watch(chainFlowProvider) | ||
.items | ||
.map((e) => e.ids | ||
.map((ei) => Offset(100, ei * 50 + 25)) | ||
.toList()) | ||
.toList(), | ||
), | ||
size: Size(100, childrenHeight), | ||
), | ||
), | ||
context)), | ||
const SizedBox( | ||
width: 10, | ||
), | ||
Expanded( | ||
child: _wrapper( | ||
SingleChildScrollView( | ||
controller: controller1, | ||
child: SizedBox( | ||
height: childrenHeight, | ||
child: Column( | ||
children: items | ||
.mapIndexed((i, e) => InkWell( | ||
onTap: () { | ||
if (first == null) { | ||
first = i; | ||
firstStr = e; | ||
} else { | ||
ref | ||
.read(chainFlowProvider | ||
.notifier) | ||
.addItem(ChainFlowItem( | ||
ids: [first!, i], | ||
contents: [firstStr!, e], | ||
)); | ||
|
||
first = null; | ||
firstStr = null; | ||
} | ||
}, | ||
child: _itemBuilder(e), | ||
)) | ||
.toList(), | ||
), | ||
), | ||
), | ||
context)), | ||
], | ||
), | ||
); | ||
} | ||
return const Center( | ||
child: CircularProgressIndicator(), | ||
); | ||
}); | ||
} | ||
|
||
Widget _itemBuilder(String s) { | ||
return Container( | ||
height: 40, | ||
margin: const EdgeInsets.all(5), | ||
padding: const EdgeInsets.all(5), | ||
decoration: BoxDecoration( | ||
borderRadius: BorderRadius.circular(4), | ||
color: AppStyle.appColorLight), | ||
child: Align( | ||
alignment: Alignment.centerLeft, | ||
child: AutoSizeText( | ||
s, | ||
maxLines: 1, | ||
style: const TextStyle(color: AppStyle.white), | ||
), | ||
), | ||
); | ||
} | ||
|
||
Widget _wrapper(Widget c, BuildContext context) { | ||
return ScrollConfiguration( | ||
behavior: ScrollConfiguration.of(context) | ||
.copyWith(scrollbars: false, dragDevices: { | ||
//必须设置此事件,不然无法滚动 | ||
PointerDeviceKind.touch, | ||
PointerDeviceKind.mouse, | ||
}), | ||
child: c, //嵌套你的SingleChildScrollView组件 | ||
); | ||
} | ||
|
||
Widget _wrapper2(Widget c, BuildContext context) { | ||
return ScrollConfiguration( | ||
behavior: ScrollConfiguration.of(context) | ||
.copyWith(scrollbars: false, dragDevices: {}), | ||
child: c, //嵌套你的SingleChildScrollView组件 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class ConnectorPainter extends CustomPainter { | ||
final List<List<Offset>> bounds; | ||
|
||
ConnectorPainter({ | ||
required this.bounds, | ||
}); | ||
|
||
@override | ||
void paint(Canvas canvas, Size size) { | ||
final paint = Paint() | ||
..color = Colors.blue // 线条颜色 | ||
..strokeWidth = 2; // 线条宽度 | ||
|
||
for (var i = 0; i < bounds.length; i++) { | ||
for (var j = 0; j < bounds[i].length - 1; j++) { | ||
final startPoint = bounds[i][j]; | ||
final endPoint = bounds[i][j + 1]; | ||
|
||
Path path = Path() | ||
..moveTo(startPoint.dx, startPoint.dy) | ||
..lineTo(startPoint.dx - 10, startPoint.dy - 10) | ||
..lineTo(startPoint.dx - 10, startPoint.dy + 10) | ||
..close(); | ||
|
||
Path path2 = Path() | ||
..moveTo(endPoint.dx, endPoint.dy) | ||
..lineTo(endPoint.dx - 10, endPoint.dy - 10) | ||
..lineTo(endPoint.dx - 10, endPoint.dy + 10) | ||
..close(); | ||
|
||
final left1 = Offset(startPoint.dx / 2, startPoint.dy); | ||
final left2 = Offset(endPoint.dx / 2, endPoint.dy); | ||
|
||
canvas.drawPath(path, paint); | ||
canvas.drawLine(startPoint, left1, paint); | ||
canvas.drawLine(left1, left2, paint); | ||
canvas.drawLine(left2, endPoint, paint); | ||
canvas.drawPath(path2, paint); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
bool shouldRepaint(covariant CustomPainter oldDelegate) { | ||
return true; | ||
} | ||
} |
Oops, something went wrong.