paint_event_demo.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:cool_ui/cool_ui.dart';
  4. class PaintEventDemo extends StatefulWidget{
  5. @override
  6. State<StatefulWidget> createState() {
  7. // TODO: implement createState
  8. return PaintEventDemoState();
  9. }
  10. }
  11. class PaintEventDemoState extends State<PaintEventDemo>{
  12. bool isPaintBackgroud = false;
  13. @override
  14. Widget build(BuildContext context) {
  15. // TODO: implement build
  16. return Scaffold(
  17. appBar: AppBar(
  18. title: Text("PaintEvent Demo"),
  19. ),
  20. body: Row(
  21. children: <Widget>[
  22. FlatButton(onPressed: (){
  23. setState((){
  24. isPaintBackgroud = !isPaintBackgroud;
  25. });
  26. }, child: Text(isPaintBackgroud?"渲染前填充颜色":"渲染后填充颜色")),
  27. PaintEvent(
  28. child: Text("子Widget文字"),
  29. paintAfter: (context,offset,size){
  30. if(!isPaintBackgroud){
  31. final Paint paint = Paint();
  32. paint.color = Colors.red;
  33. context.canvas.drawRect(offset&size, paint);
  34. }
  35. },paintBefore: (context,offset,size){
  36. if(isPaintBackgroud){
  37. final Paint paint = Paint();
  38. paint.color = Colors.red;
  39. context.canvas.drawRect(offset&size, paint);
  40. }
  41. },
  42. )
  43. ],
  44. )
  45. );
  46. }
  47. }