我正在制作一款Ffltter的绘图应用程序.当我把它放在只有家的脚手架上时,我可以正常地画一条线.但问题是,当我添加一个AppBar时,行从顶部向下偏移,它不是我在屏幕上touch 的位置,它与App bar的高度一样向下移动.
这是我的代码.
class HiraganaWriting extends StatefulWidget {
const HiraganaWriting({Key? key}) : super(key: key);
@override
State<HiraganaWriting> createState() => _HiraganaWritingState();
}
class _HiraganaWritingState extends State<HiraganaWriting> {
final GlobalKey _globalKey = GlobalKey();
List<DrawnLine> lines = <DrawnLine>[];
DrawnLine line = DrawnLine([Offset(50, 50)], Colors.black, 5.0);
Color selectedColor = Colors.black;
double selectedWidth = 5.0;
StreamController<List<DrawnLine>> linesStreamController = StreamController<List<DrawnLine>>.broadcast();
StreamController<DrawnLine> currentLineStreamController = StreamController<DrawnLine>.broadcast();
String picName = "Hi";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("AppBar"),
),
body: Container(
child: Stack(
children: [
buildAllPaths(context),
buildCurrentPath(context),
],
),
),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(icon: Icon(Icons.arrow_back), label: ""),
BottomNavigationBarItem(icon: Icon(Icons.arrow_back), label: ""),
BottomNavigationBarItem(icon: Icon(Icons.arrow_back), label: ""),
BottomNavigationBarItem(icon: Icon(Icons.arrow_back), label: ""),
],
),
);
}
Widget buildAllPaths(BuildContext context) {
return RepaintBoundary(
key: _globalKey,
child: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
color: Colors.transparent,
padding: EdgeInsets.all(4),
alignment: Alignment.topLeft,
child: StreamBuilder<List<DrawnLine>>(
stream: linesStreamController.stream,
builder: (context, snapshot) {
return CustomPaint(
painter: Sketcher(
lines: lines,
),
);
},
),
),
);
}
Widget buildCurrentPath(BuildContext context) {
return GestureDetector(
onPanStart: onPanStart,
onPanUpdate: onPanUpdate,
onPanEnd: onPanEnd,
child: RepaintBoundary(
child: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
padding: EdgeInsets.all(4),
color: Colors.transparent,
alignment: Alignment.topLeft,
child: StreamBuilder<DrawnLine>(
stream: currentLineStreamController.stream,
builder: (context, snapshot) {
return CustomPaint(
painter: Sketcher(
lines: [line],
),
);
},
),
),
),
);
}
void onPanStart(DragStartDetails details) {
RenderBox? box = context.findRenderObject() as RenderBox?;
Offset point = box!.globalToLocal(details.globalPosition);
line = DrawnLine([point], selectedColor, selectedWidth);
}
void onPanUpdate(DragUpdateDetails details) {
RenderBox? box = context.findRenderObject() as RenderBox?;
Offset point = box!.globalToLocal(details.globalPosition);
List<Offset> path = List.from(line.path)..add(point);
line = DrawnLine(path, selectedColor, selectedWidth);
currentLineStreamController.add(line);
}
void onPanEnd(DragEndDetails details) {
lines = List.from(lines)..add(line);
linesStreamController.add(lines);
}
}
我试着换了衬垫,或者把Container放进了一个大小的盒子里,...但它的工作方式有误