fcontrol.dart 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. import 'package:flutter/gestures.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/rendering.dart';
  4. import 'fdefine.dart';
  5. import 'finnershadow.dart';
  6. class FControl extends StatefulWidget {
  7. //组件标识,可以随意填写,用来在特殊情况下标识当前组件
  8. final String componentId;
  9. //组件宽高,不指定宽高的情况下,control的size会随内容变化而变化
  10. final double width;
  11. final double height;
  12. //组件的内外边距,默认均为0
  13. final EdgeInsetsGeometry margin; //组件外边距
  14. final EdgeInsetsGeometry padding; //组件内边距
  15. //组件视觉效果及外观设置
  16. //FAppearance,组件外观选项,支持三种外观,Flat(扁平风格),Neumorphism(新拟态风格),Material(材质风格),
  17. //FLightOrientation 光源方向,分为左上、左下、右上、右下四个方向。用来控制光源照射方向,会影响高亮方向和阴影方向
  18. //FSurface 组件表面的视觉效果, Flat(平面效果)、Convex(凸面效果)、Concave(凹面效果)该效果会随着光源而变化
  19. //FSurfaceForStateCallback 根据状态不同,返回不同的Surface
  20. //FShape 设置组件的边框及外形 支持RoundedRectangle(圆角),ContinuousRectangle(连续弧度),BeveledRectangle(斜角)三种形式
  21. //FShapeForStateCallback 根据状态不同,返回不同的Shape
  22. final FAppearance appearance;
  23. final FLightOrientation lightOrientation;
  24. final FSurface surface;
  25. final FSurfaceForStateCallback surfaceForCallback;
  26. final FShape shape;
  27. final FShapeForStateCallback shapeForStateCallback;
  28. //阴影设置
  29. //supportDropShadow 是否支持外阴影,默认为支持
  30. //supportInnerShadow 是否支持内阴影,主要用于新拟态风格,默认为支持
  31. //当任何一种阴影的support属性设置为true后,整个组件内部会去掉阴影层,从而ji
  32. final bool supportDropShadow;
  33. final FShadow dropShadow;
  34. final bool supportInnerShadow;
  35. final FShadow innerShadow;
  36. //背景色设置,color与gradient为互斥关系,设置了gradient就会覆盖color
  37. //Color 设置单色背景色
  38. //FColorForStateCallback 根据状态不同,返回不同的Color
  39. //Gradient 设置渐变背景色
  40. //FGradientForStateCallback 根据状态不同,返回不同的Gradient
  41. //maskColor 蒙板颜色,主要用于风格中的一些视觉效果,大多数情况下不应该被修改
  42. final Color color;
  43. final FColorForStateCallback colorForCallback;
  44. final Gradient gradient;
  45. final FGradientForStateCallback gradientForCallback;
  46. final Color maskColor;
  47. //组件的类型,组件支持的类型, Button,Toggle,
  48. //isSelected仅在controlType == Toggle的时候有效,可以通过设置isSelected来控制默认是否为“按下”状态
  49. final FType controlType;
  50. final bool isSelected;
  51. //是否禁用,默认为false,设置为yes时,会激活所有的stateForCallback回调
  52. final bool disabled;
  53. //是否与用户产生交互,默认为true,设置为false后,不会发生任何变化,只是不再响应事件
  54. final bool userInteractive;
  55. //子组件
  56. //FChildForStateCallback 子组件的callback方法,可以根据组件不同的状态来设置不同的子组件
  57. final Widget child;
  58. final FChildForStateCallback childForStateCallback;
  59. final FGroupController controller;
  60. final FOnTapCallback onTapCallback; //最近被点击后的callback
  61. final FOnTapDownCallback onTapDownCallback;
  62. final FOnTapUpCallback onTapUpCallback;
  63. final FOnTapCancelCallback onTapCancelCallback;
  64. final ValueChanged<bool> onHover;
  65. final Color hoverColor;
  66. const FControl({
  67. Key key,
  68. this.componentId = "componentId", //默认的componentId
  69. this.width,
  70. this.height,
  71. this.lightOrientation = FLightOrientation.LeftTop, //默认光源为左上角
  72. this.color = FPrimerColor,
  73. this.colorForCallback,
  74. this.gradient,
  75. this.gradientForCallback,
  76. this.surface = FSurface.Flat, //默认表面形状为“凸面”
  77. this.surfaceForCallback,
  78. this.childForStateCallback,
  79. this.shape,
  80. this.shapeForStateCallback,
  81. this.disabled = false, //默认为不禁用
  82. this.margin,
  83. this.padding,
  84. this.maskColor,
  85. this.supportDropShadow = true,
  86. this.dropShadow = const FShadow(),
  87. this.supportInnerShadow = true,
  88. this.innerShadow = const FShadow(),
  89. this.appearance,
  90. this.controlType = FType.Button, //默认类型为button
  91. this.isSelected = false, //默认为非选中
  92. this.userInteractive = true,
  93. this.controller,
  94. this.child,
  95. this.onTapCallback,
  96. this.onTapDownCallback,
  97. this.onTapUpCallback,
  98. this.onTapCancelCallback,
  99. this.onHover,
  100. this.hoverColor,
  101. }) : super(key: key);
  102. @override
  103. State<StatefulWidget> createState() {
  104. return FControlState();
  105. }
  106. }
  107. class FControlState extends State<FControl> {
  108. FState controlState;
  109. Color defaultColor;
  110. Color currentColor;
  111. Gradient defaultGradient;
  112. Gradient currentGradient;
  113. Widget defaultWidget;
  114. Widget currentWidget;
  115. Border defaultBorder;
  116. Border currentBorder;
  117. FShape defaultShape;
  118. FShape currentShape;
  119. FSurface defaultSurface;
  120. FSurface currentSurface;
  121. bool isSelected = false;
  122. bool disabled = false;
  123. bool supportDropShadow = true;
  124. FType controlType = FType.Button;
  125. FAppearance appearance = FAppearance.Flat;
  126. FLightOrientation lightOrientation = FLightOrientation.LeftTop;
  127. Color maskColor;
  128. bool _hovering = false;
  129. // MouseCursor effectiveMouseCursor;
  130. @override
  131. void initState() {
  132. // effectiveMouseCursor = MaterialStateProperty.resolveAs<MouseCursor>(
  133. // MaterialStateMouseCursor.clickable,
  134. // <MaterialState>{
  135. // if (disabled) MaterialState.disabled,
  136. // if (_hovering) MaterialState.hovered,
  137. //// if (_hasFocus) MaterialState.focused,
  138. // },
  139. // );
  140. controlState = FState.Normal;
  141. appearance = widget.appearance ?? FAppearance.Flat;
  142. supportDropShadow = widget.supportDropShadow ?? true;
  143. lightOrientation = widget.lightOrientation ?? FLightOrientation.LeftTop;
  144. controlType = widget.controlType ?? FType.Button;
  145. maskColor = widget.maskColor ?? Colors.black12;
  146. defaultColor = widget.color ?? FPrimerColor;
  147. defaultGradient = widget.gradient;
  148. if (widget.controller != null) {
  149. widget.controller.states.add(this);
  150. }
  151. isSelected = widget.isSelected ?? false;
  152. if (controlType == FType.Toggle) {
  153. controlState = isSelected ? FState.Highlighted : FState.Normal;
  154. }
  155. disabled = widget.disabled ?? false;
  156. if (disabled) {
  157. controlState = FState.Disable;
  158. defaultColor = FDisableColor;
  159. // defaultBackgroundColors = [FDisableColor, FDisableColor];
  160. }
  161. if (widget.colorForCallback != null) {
  162. currentColor = widget.colorForCallback(widget, controlState);
  163. }
  164. currentColor = currentColor ?? defaultColor;
  165. if (widget.gradientForCallback != null) {
  166. currentGradient = widget.gradientForCallback(widget, controlState);
  167. }
  168. currentGradient = currentGradient ?? defaultGradient;
  169. // if (widget.backgroundColorsForCallback != null) {
  170. // currentBackgroundColors =
  171. // widget.backgroundColorsForCallback(widget, controlState);
  172. // }
  173. // currentBackgroundColors =
  174. // _fixBackgroundColors(currentBackgroundColors, widget.backgroundColors);
  175. defaultShape = widget.shape ?? FShape();
  176. currentShape = defaultShape;
  177. defaultWidget = widget.child;
  178. if (widget.childForStateCallback != null) {
  179. currentWidget = widget.childForStateCallback(widget, controlState);
  180. }
  181. currentWidget = currentWidget ?? defaultWidget;
  182. defaultSurface = widget.surface;
  183. if (widget.surfaceForCallback != null) {
  184. currentSurface = widget.surfaceForCallback(widget, controlState);
  185. }
  186. currentSurface = currentSurface ?? defaultSurface;
  187. if (controlType == FType.Toggle && isSelected) {
  188. controlState = FState.Highlighted;
  189. _controlGestureHandlerForState();
  190. }
  191. super.initState();
  192. }
  193. @override
  194. Widget build(BuildContext context) {
  195. if (disabled) {
  196. controlState = FState.Disable;
  197. _controlGestureHandlerForState();
  198. return createCoreControl();
  199. }
  200. if (widget.userInteractive == false) {
  201. return createCoreControl();
  202. }
  203. return MouseRegion(
  204. // cursor: effectiveMouseCursor,
  205. onEnter: _handleMouseEnter,
  206. onExit: _handleMouseExit,
  207. child: GestureDetector(
  208. onTapDown: (details) {
  209. if (controlType == FType.Button) {
  210. controlState = FState.Highlighted;
  211. } else {
  212. if (widget.controller != null &&
  213. widget.controller.mustBeSelected &&
  214. isSelected) {
  215. return;
  216. }
  217. isSelected = !isSelected;
  218. controlState = FState.Highlighted;
  219. }
  220. _controlGestureHandlerForState();
  221. if (widget.onTapDownCallback != null) {
  222. widget.onTapDownCallback(widget, isSelected);
  223. }
  224. },
  225. onTapUp: (details) {
  226. if (widget.onTapUpCallback != null) {
  227. widget.onTapUpCallback(widget, isSelected);
  228. }
  229. },
  230. onTapCancel: () {
  231. if (controlType == FType.Button) {
  232. controlState = FState.Normal;
  233. } else {
  234. controlState =
  235. isSelected ? FState.Highlighted : FState.Normal;
  236. }
  237. _controlGestureHandlerForState();
  238. if (widget.onTapCancelCallback != null) {
  239. widget.onTapCancelCallback(widget, isSelected);
  240. }
  241. },
  242. onTap: () {
  243. if (controlType == FType.Button) {
  244. controlState = FState.Normal;
  245. _controlGestureHandlerForState();
  246. } else {
  247. controlState =
  248. isSelected ? FState.Highlighted : FState.Normal;
  249. if (widget.controller != null &&
  250. widget.controller.states.length > 0) {
  251. List<Widget> all = List();
  252. Widget changed;
  253. for (int i = 0; i < widget.controller.states.length; i++) {
  254. FControlState state = widget.controller.states[i];
  255. all.add(state.widget);
  256. if (state != this) {
  257. if (state.isSelected == false &&
  258. state.controlState == FState.Normal) {
  259. continue;
  260. }
  261. state.isSelected = false;
  262. state.controlState = FState.Normal;
  263. state._controlGestureHandlerForState();
  264. } else {
  265. changed = this.widget;
  266. _controlGestureHandlerForState();
  267. }
  268. }
  269. if (widget.controller.groupClickCallback != null) {
  270. widget.controller.groupClickCallback(changed, isSelected, all);
  271. }
  272. } else {
  273. _controlGestureHandlerForState();
  274. }
  275. }
  276. if (widget.onTapCallback != null) {
  277. widget.onTapCallback(widget, isSelected);
  278. }
  279. },
  280. child: createCoreControl(),
  281. ),
  282. );
  283. }
  284. void _handleMouseEnter(PointerEnterEvent event) => _handleHoverChange(true);
  285. void _handleMouseExit(PointerExitEvent event) => _handleHoverChange(false);
  286. void _handleHoverChange(bool hovering) {
  287. if (_hovering != hovering) {
  288. _hovering = hovering;
  289. widget.onHover?.call(_hovering);
  290. }
  291. if (widget.hoverColor != null) {
  292. _controlGestureHandlerForState();
  293. }
  294. }
  295. //============> 工具函数
  296. Widget createCoreControl() {
  297. return Container(
  298. margin: widget.margin,
  299. width: widget.width,
  300. height: widget.height,
  301. decoration: ShapeDecoration(
  302. color: (currentGradient != null) ? null : currentColor,
  303. gradient: currentGradient ?? defaultGradient,
  304. shape: _createShapeBorder(controlState, false),
  305. shadows: _createDropShadowList(
  306. controlState, widget.dropShadow, supportDropShadow),
  307. ),
  308. foregroundDecoration: ShapeDecoration(
  309. shape: _createShapeBorder(controlState, false),
  310. ),
  311. child: _createChildContainer(
  312. controlState, widget.lightOrientation, widget.innerShadow),
  313. );
  314. }
  315. Widget _createChildContainer(FState state,
  316. FLightOrientation lightOrientation, FShadow innerShadow) {
  317. switch (appearance) {
  318. case FAppearance.Flat: //扁平风格,忽略所有阴影效果
  319. return Container(
  320. foregroundDecoration: _createSurfaceShape(),
  321. // decoration: ShapeDecoration(
  322. // shape: _createShapeBorder(state, true),
  323. // gradient: _createGradientBackgroundColorForState(state),
  324. // ),
  325. padding: widget.padding,
  326. child: currentWidget,
  327. );
  328. case FAppearance.Material: //Google风格,不做内阴影
  329. return Container(
  330. // alignment: Alignment.center,
  331. foregroundDecoration: _createSurfaceShape(),
  332. // decoration: ShapeDecoration(
  333. // shape: _createShapeBorder(state, true),
  334. // gradient: _createGradientBackgroundColorForState(state),
  335. // ),
  336. padding: widget.padding,
  337. child: currentWidget,
  338. );
  339. default: //FControlAppearance.Neumorphism
  340. if (innerShadow == null) {
  341. innerShadow = FShadow();
  342. }
  343. if (widget.supportInnerShadow ?? true) {
  344. return FInnerShadow(
  345. blur: innerShadow.highlightBlur,
  346. color: _innerShadowColor(true, state, innerShadow),
  347. offset: _innerShadowOffset(false, state, innerShadow),
  348. child: FInnerShadow(
  349. blur: innerShadow.shadowDistance,
  350. color: _innerShadowColor(false, state, innerShadow),
  351. offset: _innerShadowOffset(true, state, innerShadow),
  352. child: Container(
  353. padding: widget.padding,
  354. foregroundDecoration: _createSurfaceShape(),
  355. decoration: ShapeDecoration(
  356. color: (currentGradient != null) ? null : currentColor,
  357. gradient: currentGradient ?? defaultGradient,
  358. shape: _createShapeBorder(state, true),
  359. // gradient:
  360. // _createGradientBackgroundColorForState(controlState),
  361. ),
  362. child: currentWidget,
  363. ),
  364. ),
  365. );
  366. } else {
  367. return Container(
  368. padding: widget.padding,
  369. foregroundDecoration: _createSurfaceShape(),
  370. decoration: ShapeDecoration(
  371. color: (currentGradient != null) ? null : currentColor,
  372. gradient: currentGradient ?? defaultGradient,
  373. shape: _createShapeBorder(state, true),
  374. // gradient: _createGradientBackgroundColorForState(state),
  375. ),
  376. child: currentWidget,
  377. );
  378. }
  379. }
  380. }
  381. //统一处理手势操作
  382. void _controlGestureHandlerForState() {
  383. _updateState();
  384. setState(() {});
  385. }
  386. void _updateState() {
  387. if (widget.colorForCallback != null) {
  388. currentColor =
  389. widget.colorForCallback(widget, controlState) ?? defaultColor;
  390. if (controlState == FState.Normal && widget.hoverColor != null && _hovering) {
  391. currentColor = widget.hoverColor;
  392. }
  393. }
  394. if (widget.gradientForCallback != null) {
  395. currentGradient =
  396. widget.gradientForCallback(widget, controlState) ?? defaultGradient;
  397. }
  398. if (widget.childForStateCallback != null) {
  399. currentWidget =
  400. widget.childForStateCallback(widget, controlState) ?? defaultWidget;
  401. }
  402. if (widget.surfaceForCallback != null) {
  403. currentSurface =
  404. widget.surfaceForCallback(widget, controlState) ?? defaultSurface;
  405. }
  406. if (widget.shapeForStateCallback != null) {
  407. currentShape =
  408. widget.shapeForStateCallback(widget, controlState) ?? defaultShape;
  409. }
  410. }
  411. //处理边框
  412. ShapeBorder _createShapeBorder(FState state, bool justShape) {
  413. if (widget.shapeForStateCallback != null) {
  414. currentShape =
  415. widget.shapeForStateCallback(widget, state) ?? defaultShape;
  416. } else {
  417. currentShape = defaultShape;
  418. }
  419. // justShape = false;
  420. switch (currentShape.borderShape) {
  421. case FBorderShape.BeveledRectangle:
  422. return BeveledRectangleBorder(
  423. borderRadius: currentShape.borderRadius,
  424. side: justShape ? BorderSide.none : currentShape.side,
  425. );
  426. case FBorderShape.ContinuousRectangle:
  427. return ContinuousRectangleBorder(
  428. borderRadius: currentShape.borderRadius,
  429. side: justShape ? BorderSide.none : currentShape.side,
  430. );
  431. default:
  432. return RoundedRectangleBorder(
  433. borderRadius: currentShape.borderRadius,
  434. side: justShape ? BorderSide.none : currentShape.side,
  435. );
  436. }
  437. }
  438. List<BoxShadow> _createDropShadowList(
  439. FState state, FShadow dropShadow, bool canUseShadow) {
  440. List<BoxShadow> shadows = List();
  441. switch (appearance) {
  442. case FAppearance.Flat: //扁平风格,忽略所有阴影效果
  443. break;
  444. case FAppearance.Material: //Google风格,只处理背光阴影
  445. if (canUseShadow == false) {
  446. return shadows;
  447. }
  448. if (dropShadow == null) {
  449. dropShadow = FShadow();
  450. }
  451. shadows.add(BoxShadow(
  452. color: dropShadow.shadowColor,
  453. offset: dropShadow.shadowOffset ?? _dropShadowOffset(
  454. appearance, lightOrientation, state, true, dropShadow),
  455. blurRadius: dropShadow.shadowBlur,
  456. spreadRadius: dropShadow.shadowSpread,
  457. ));
  458. break;
  459. default: //FControlAppearance.Neumorphism,新拟态风格,处理向光和背光两个阴影
  460. if (canUseShadow == false) {
  461. return shadows;
  462. }
  463. if (dropShadow == null) {
  464. dropShadow = FShadow();
  465. }
  466. shadows.add(BoxShadow(
  467. color: dropShadow.highlightColor,
  468. offset: _dropShadowOffset(
  469. appearance, lightOrientation, state, true, dropShadow),
  470. blurRadius:
  471. (state == FState.Highlighted) ? 0 : dropShadow.highlightBlur,
  472. spreadRadius: (state == FState.Highlighted)
  473. ? 0
  474. : dropShadow.highlightSpread,
  475. ));
  476. shadows.add(BoxShadow(
  477. color: dropShadow.shadowColor,
  478. offset: _dropShadowOffset(
  479. appearance, lightOrientation, state, false, dropShadow),
  480. blurRadius:
  481. (state == FState.Highlighted) ? 0 : dropShadow.shadowBlur,
  482. spreadRadius:
  483. (state == FState.Highlighted) ? 0 : dropShadow.shadowSpread,
  484. ));
  485. }
  486. return shadows;
  487. }
  488. Offset _dropShadowOffset(
  489. FAppearance appearance, //外观风格
  490. FLightOrientation lightOrientation, //光源方向,在FControlAppearance.Flat时无效
  491. FState state, //控件状态
  492. bool isHighlight, //当前是否处理高光,仅在FControlAppearance.Neumorphism时有效
  493. FShadow shadow) {
  494. //光影效果定义,仅在FControlAppearance.Neumorphism且isHighlight=true时处理高光
  495. Offset offset;
  496. if (appearance == FAppearance.Flat || state == FState.Highlighted) {
  497. return Offset.zero;
  498. }
  499. switch (lightOrientation) {
  500. case FLightOrientation.LeftTop:
  501. if (appearance == FAppearance.Material) {
  502. offset = Offset(shadow.shadowDistance, shadow.shadowDistance);
  503. } else {
  504. //FControlAppearance.Neumorphism
  505. offset = isHighlight
  506. ? Offset(-shadow.highlightDistance, -shadow.highlightDistance)
  507. : Offset(shadow.shadowDistance, shadow.shadowDistance);
  508. }
  509. return offset;
  510. case FLightOrientation.LeftBottom:
  511. if (appearance == FAppearance.Material) {
  512. offset = Offset(shadow.shadowDistance, -shadow.shadowDistance);
  513. } else {
  514. //FControlAppearance.Neumorphism
  515. offset = isHighlight
  516. ? Offset(-shadow.highlightDistance, shadow.highlightDistance)
  517. : Offset(shadow.shadowDistance, -shadow.shadowDistance);
  518. }
  519. return offset;
  520. case FLightOrientation.RightTop:
  521. if (appearance == FAppearance.Material) {
  522. offset = Offset(-shadow.shadowDistance, shadow.shadowDistance);
  523. } else {
  524. //FControlAppearance.Neumorphism
  525. offset = isHighlight
  526. ? Offset(shadow.highlightDistance, -shadow.highlightDistance)
  527. : Offset(-shadow.shadowDistance, shadow.shadowDistance);
  528. }
  529. return offset;
  530. case FLightOrientation.RightBottom:
  531. if (appearance == FAppearance.Material) {
  532. offset = Offset(-shadow.shadowDistance, -shadow.shadowDistance);
  533. } else {
  534. //FControlAppearance.Neumorphism
  535. offset = isHighlight
  536. ? Offset(shadow.highlightDistance, shadow.highlightDistance)
  537. : Offset(-shadow.shadowDistance, -shadow.shadowDistance);
  538. }
  539. return offset;
  540. }
  541. return null;
  542. }
  543. Color _innerShadowColor(
  544. bool isBacklight, FState state, FShadow innerShadow) {
  545. if (state == FState.Normal || state == FState.Disable) {
  546. return Color.fromARGB(0, 0, 0, 0);
  547. } else {
  548. return isBacklight ? innerShadow.highlightColor : innerShadow.shadowColor;
  549. }
  550. }
  551. Offset _innerShadowOffset(
  552. bool isBacklight, FState state, FShadow innerShadow) {
  553. double forwardlightDistance = innerShadow.highlightDistance.abs();
  554. double backlightDistance = innerShadow.shadowDistance.abs();
  555. switch (lightOrientation) {
  556. case FLightOrientation.LeftTop:
  557. {
  558. Offset offset = isBacklight
  559. ? Offset(backlightDistance, backlightDistance)
  560. : Offset(-forwardlightDistance, -forwardlightDistance);
  561. if (state == FState.Normal || state == FState.Disable) {
  562. offset = Offset.zero;
  563. }
  564. return offset;
  565. }
  566. case FLightOrientation.LeftBottom:
  567. {
  568. Offset offset = isBacklight
  569. ? Offset(backlightDistance, -backlightDistance)
  570. : Offset(-forwardlightDistance, forwardlightDistance);
  571. if (controlState == FState.Normal ||
  572. controlState == FState.Disable) {
  573. offset = Offset.zero;
  574. }
  575. return offset;
  576. }
  577. case FLightOrientation.RightTop:
  578. {
  579. Offset offset = isBacklight
  580. ? Offset(-backlightDistance, backlightDistance)
  581. : Offset(forwardlightDistance, -forwardlightDistance);
  582. if (controlState == FState.Normal ||
  583. controlState == FState.Disable) {
  584. offset = Offset.zero;
  585. }
  586. return offset;
  587. }
  588. case FLightOrientation.RightBottom:
  589. {
  590. Offset offset = isBacklight
  591. ? Offset(-backlightDistance, -backlightDistance)
  592. : Offset(forwardlightDistance, forwardlightDistance);
  593. if (controlState == FState.Normal ||
  594. controlState == FState.Disable) {
  595. offset = Offset.zero;
  596. }
  597. return offset;
  598. }
  599. }
  600. return isBacklight
  601. ? Offset(backlightDistance, backlightDistance)
  602. : Offset(-forwardlightDistance, -forwardlightDistance);
  603. }
  604. ShapeDecoration _createSurfaceShape() {
  605. Color surfaceShadowColor = Colors.black26;
  606. switch (currentSurface) {
  607. case FSurface.Flat:
  608. return ShapeDecoration(
  609. shape: _createShapeBorder(controlState, true),
  610. color: (controlState == FState.Highlighted)
  611. ? maskColor
  612. : Colors.transparent);
  613. case FSurface.Convex:
  614. switch (lightOrientation) {
  615. case FLightOrientation.LeftTop:
  616. return ShapeDecoration(
  617. shape: _createShapeBorder(controlState, true),
  618. gradient: LinearGradient(
  619. colors: [Colors.transparent, surfaceShadowColor],
  620. stops: [0.4, 1.0],
  621. begin: Alignment.topLeft,
  622. end: Alignment.bottomRight),
  623. );
  624. case FLightOrientation.LeftBottom:
  625. return ShapeDecoration(
  626. shape: _createShapeBorder(controlState, true),
  627. gradient: LinearGradient(
  628. colors: [Colors.transparent, surfaceShadowColor],
  629. stops: [0.4, 1.0],
  630. begin: Alignment.bottomLeft,
  631. end: Alignment.topRight),
  632. );
  633. case FLightOrientation.RightTop:
  634. return ShapeDecoration(
  635. shape: _createShapeBorder(controlState, true),
  636. gradient: LinearGradient(
  637. colors: [Colors.transparent, surfaceShadowColor],
  638. stops: [0.4, 1.0],
  639. begin: Alignment.topRight,
  640. end: Alignment.bottomLeft),
  641. );
  642. case FLightOrientation.RightBottom:
  643. return ShapeDecoration(
  644. shape: _createShapeBorder(controlState, true),
  645. gradient: LinearGradient(
  646. colors: [Colors.transparent, surfaceShadowColor],
  647. stops: [0.4, 1.0],
  648. begin: Alignment.bottomLeft,
  649. end: Alignment.topRight),
  650. );
  651. }
  652. return ShapeDecoration(
  653. shape: _createShapeBorder(controlState, true),
  654. gradient: LinearGradient(
  655. colors: [Colors.transparent, surfaceShadowColor],
  656. stops: [0.4, 1.0],
  657. begin: Alignment.topLeft,
  658. end: Alignment.bottomRight),
  659. );
  660. case FSurface.Concave:
  661. switch (lightOrientation) {
  662. case FLightOrientation.LeftTop:
  663. return ShapeDecoration(
  664. shape: _createShapeBorder(controlState, true),
  665. gradient: LinearGradient(
  666. colors: [surfaceShadowColor, Colors.transparent],
  667. begin: Alignment.topLeft,
  668. end: Alignment.bottomRight),
  669. );
  670. case FLightOrientation.LeftBottom:
  671. return ShapeDecoration(
  672. shape: _createShapeBorder(controlState, true),
  673. gradient: LinearGradient(
  674. colors: [surfaceShadowColor, Colors.transparent],
  675. begin: Alignment.bottomLeft,
  676. end: Alignment.topRight),
  677. );
  678. case FLightOrientation.RightTop:
  679. return ShapeDecoration(
  680. shape: _createShapeBorder(controlState, true),
  681. gradient: LinearGradient(
  682. colors: [surfaceShadowColor, Colors.transparent],
  683. begin: Alignment.topRight,
  684. end: Alignment.bottomLeft),
  685. );
  686. case FLightOrientation.RightBottom:
  687. return ShapeDecoration(
  688. shape: _createShapeBorder(controlState, true),
  689. gradient: LinearGradient(
  690. colors: [surfaceShadowColor, Colors.transparent],
  691. begin: Alignment.bottomLeft,
  692. end: Alignment.topRight),
  693. );
  694. }
  695. return ShapeDecoration(
  696. shape: _createShapeBorder(controlState, true),
  697. gradient: LinearGradient(
  698. colors: [surfaceShadowColor, Colors.transparent],
  699. begin: Alignment.topLeft,
  700. end: Alignment.bottomRight),
  701. );
  702. }
  703. }
  704. //============> 重载部分生存周期函数,用于不同状态的变化响应
  705. void didUpdateWidget(FControl oldWidget) {
  706. isSelected = widget.isSelected ?? false;
  707. controlType = widget.controlType ?? FType.Button;
  708. disabled = widget.disabled ?? false;
  709. defaultColor = widget.color;
  710. if (disabled) {
  711. controlState = FState.Disable;
  712. defaultColor = FDisableColor;
  713. // defaultBackgroundColors = [FDisableColor, FDisableColor];
  714. } else if (controlType == FType.Toggle) {
  715. controlState = isSelected ? FState.Highlighted : FState.Normal;
  716. _updateState();
  717. } else if(controlState == FState.Disable){
  718. controlState = FState.Normal;
  719. }
  720. if(widget.colorForCallback != null){
  721. defaultColor = widget.colorForCallback(widget, controlState) ?? widget.color;
  722. }
  723. defaultColor = defaultColor ?? FPrimerColor;
  724. appearance = widget.appearance ?? FAppearance.Flat;
  725. defaultWidget = widget.child;
  726. defaultGradient = widget.gradient;
  727. if (controlState == FState.Normal) {
  728. // currentWidget = defaultWidget;
  729. // currentColor = defaultColor;
  730. currentGradient = defaultGradient;
  731. }
  732. currentWidget = defaultWidget;
  733. if(widget.childForStateCallback != null){
  734. currentWidget = widget.childForStateCallback(widget, controlState);
  735. }
  736. currentWidget = currentWidget ?? defaultWidget;
  737. currentGradient = currentGradient ?? defaultGradient;
  738. // currentColor = currentColor ?? defaultColor;
  739. currentColor = defaultColor;
  740. // defaultBackgroundColors = widget.backgroundColors ?? [FPrimerColor];
  741. // disabled = widget.disabled ?? false;
  742. // if (disabled) {
  743. // controlState = FState.Disable;
  744. // defaultColor = FDisableColor;
  745. // // defaultBackgroundColors = [FDisableColor, FDisableColor];
  746. // }
  747. defaultShape = widget.shape ?? FShape();
  748. supportDropShadow = widget.supportDropShadow ?? true;
  749. maskColor = widget.maskColor ?? Colors.black12;
  750. // isSelected = widget.isSelected ?? false;
  751. lightOrientation = widget.lightOrientation ?? FLightOrientation.LeftTop;
  752. // controlType = widget.controlType ?? FType.Button;
  753. currentSurface = widget.surface ?? FSurface.Flat;
  754. // if (controlType == FType.Toggle) {
  755. // controlState = isSelected ? FState.Highlighted : FState.Normal;
  756. // _controlGestureHandlerForState();
  757. // }
  758. // if (disabled) {
  759. // controlState = FState.Disable;
  760. // }
  761. if (widget.surfaceForCallback != null) {
  762. currentSurface = widget.surfaceForCallback(widget, controlState);
  763. currentSurface = currentSurface ?? (widget.surface ?? FSurface.Flat);
  764. }
  765. super.didUpdateWidget(oldWidget);
  766. }
  767. void dispose() {
  768. if (widget.controller != null) {
  769. widget.controller.states.remove(this);
  770. }
  771. super.dispose();
  772. }
  773. }