import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flu_merchant/app_constants.dart';
import 'package:flu_merchant/ui/common/common.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:signature/signature.dart';
typedef CallBack = void Function(Uint8List? imageByte);
class DrawingBoardPage extends StatefulWidget {
final CallBack? drawComplete;
bool landScape; // 不用了
DrawingBoardPage({
required this.landScape,
this.drawComplete,
Key? key}) : super(key: key);
@override
State<DrawingBoardPage> createState() => _DrawingBoardPageState();
}
class _DrawingBoardPageState extends State<DrawingBoardPage> {
@override
void initState() {
// TODO: implement initState
super.initState();
SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeRight]);
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
}
final SignatureController _signatureController = SignatureController(
penStrokeWidth: 3,
penColor: Colors.black,
exportBackgroundColor: Colors.white,
exportPenColor: Colors.black,
onDrawStart: () => print('onDrawStart called!'),
onDrawEnd: () => print('onDrawEnd called!'),
);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
// width: ScreenUtil().screenWidth,
// height: ScreenUtil().screenHeight,
color: Colors.white,
child: FittedBox(
fit: BoxFit.contain,
child: DrawingView(
signatureController: _signatureController,
width: 1.sh,
height: 1.sw,
landScape: widget.landScape,
drawComplete: (image) {
if (widget.drawComplete != null) {
widget.drawComplete!(image);
}
},
resetCallback: () {
setState(() {
});
},
),
),
),
);
}
}
class DrawingView extends StatefulWidget {
final CallBack? drawComplete;
final SignatureController signatureController;
final resetCallback; // 還原大小回調(diào)
final double width;
final double height;
late bool landScape; // 是否橫屏
DrawingView(
{Key? key,
this.drawComplete,
required this.signatureController,
this.resetCallback,
required this.width,
required this.height,
required this.landScape})
: super(key: key);
@override
_DrawingViewState createState() => _DrawingViewState();
}
class _DrawingViewState extends State<DrawingView> {
bool isEmpty = true;
@override
void initState() {
super.initState();
if (widget.signatureController.value.length > 0) {
isEmpty = false;
} else {
isEmpty = true;
}
// 監(jiān)聽畫板
widget.signatureController.addListener(() {
bool tmpIsEmpty = true;
if (widget.signatureController.value.length > 0) {
tmpIsEmpty = false;
} else {
tmpIsEmpty = true;
}
if (isEmpty != tmpIsEmpty) {
if (this.mounted) {
setState(() {
isEmpty = tmpIsEmpty;
});
}
}
});
}
@override
Widget build(BuildContext context) {
return RotatedBox(
quarterTurns: widget.landScape ? 1 : 0,
child: aaa(),
);
}
Widget aaa() {
return Stack(
alignment: Alignment.center,
children: [
Signature(
controller: widget.signatureController,
width: widget.width,
height: widget.height,
backgroundColor: clFFF4F4F4,
),
Positioned(
top: 0,
child: Container(
height: 44, width: widget.height,
color: Colors.white,
padding: EdgeInsets.symmetric(horizontal: MediaQuery.of(context).padding.bottom+20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
GestureDetector(
onTap: (){
Navigator.pop(context);
},
child: Container(
width: 30,
child: Image.asset(ImageConstants.nav_back_balck, width: 25, height: 25),),
),
GestureDetector(
onTap: () async {
Navigator.pop(context);
ByteData? bytes = await (
await widget.signatureController.toImage(
width: widget.height.ceil(),height: widget.width.ceil()
))?.toByteData(format: ui.ImageByteFormat.png);
if (widget.drawComplete != null) {
widget.drawComplete!(bytes?.buffer.asUint8List());
}
},
child: Text('確定', style: TextStyle(color: clFF0BA198)),
)
],
),
),
),
// 暫無簽名
Offstage(
offstage: isEmpty ? false : true,
child: Text(
'簽名(必填)',
style: TextStyle(
fontSize: 44.sp,
color: clFFE5E5E5,
),
),
),
Positioned(
right: 20, bottom: 40,
child: Row(
children: [
GestureDetector(
child: Row(
children: [
Image.asset(ImageConstants.ic_reset, width: 24, height: 24),
Text('清除', style: TextStyle(color: clFF0BA198, fontSize: 16))
],
),
onTap: () {
setState(() {
widget.signatureController.clear();
});
},
),
],
),
),
Positioned(
bottom: 40,
child: Text(
'用手指簽名刨肃,注意字跡清晰',
style: TextStyle(
fontSize: 16,
color: clFF999999,
),
),
)
],
);
}
}
flutter 手寫畫板
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鹅搪,“玉大人站绪,你說我怎么就攤上這事∩” “怎么了崇众?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長航厚。 經(jīng)常有香客問我顷歌,道長,這世上最難降的妖魔是什么幔睬? 我笑而不...
- 正文 為了忘掉前任眯漩,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘赦抖。我一直安慰自己舱卡,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布队萤。 她就那樣靜靜地躺著轮锥,像睡著了一般。 火紅的嫁衣襯著肌膚如雪要尔。 梳的紋絲不亂的頭發(fā)上舍杜,一...
- 文/蒼蘭香墨 我猛地睜開眼蚕键,長吁一口氣:“原來是場噩夢啊……” “哼救欧!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起嚎幸,我...
- 正文 年R本政府宣布,位于F島的核電站惊科,受9級特大地震影響拍摇,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜馆截,卻給世界環(huán)境...
- 文/蒙蒙 一充活、第九天 我趴在偏房一處隱蔽的房頂上張望蜂莉。 院中可真熱鬧,春花似錦混卵、人聲如沸映穗。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽蚁滋。三九已至,卻和暖如春赘淮,著一層夾襖步出監(jiān)牢的瞬間枢赔,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 已收錄 : 100+ ? ? 盡可能收集好用的插件以便更快的開發(fā)效率拷窜,85%親測开皿,持續(xù)添加中 如果有好的插件或者...
- 目錄:Views 視圖1.1 widgets1.2 動態(tài)更新 UI1.3 增加或者隱藏一個控件1.4 增加動畫效果...
- 一、前言 閑來無事篮昧,花了兩個多星期入了Flutter的坑赋荆,這篇文章總結(jié)了一下這段時間學(xué)到的東西以及學(xué)習(xí)過程中看過的...