ANTIPALSU Label template editor using flutter

canvas_setup_page.dart 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_canvas_editor/providers/editor.dart';
  3. import 'package:provider/provider.dart';
  4. class CanvasSetupPage extends StatefulWidget {
  5. @override
  6. _CanvasSetupPageState createState() => _CanvasSetupPageState();
  7. }
  8. class _CanvasSetupPageState extends State<CanvasSetupPage> {
  9. final _formKey = GlobalKey<FormState>();
  10. final TextEditingController _widthController = TextEditingController();
  11. final TextEditingController _heightController = TextEditingController();
  12. @override
  13. void initState() {
  14. // TODO: implement initState
  15. super.initState();
  16. final editorProvider = Provider.of<Editor>(context, listen: false);
  17. _widthController.text = (editorProvider.canvasProperty.width / 10).toString();
  18. _heightController.text = (editorProvider.canvasProperty.height / 10).toString();
  19. }
  20. @override
  21. Widget build(BuildContext context) {
  22. final editorProvider = Provider.of<Editor>(context);
  23. return Scaffold(
  24. appBar: AppBar(
  25. title: Text('Canvas Setup'),
  26. ),
  27. body: Padding(
  28. padding: const EdgeInsets.all(16.0),
  29. child: Form(
  30. key: _formKey,
  31. child: Column(
  32. children: <Widget>[
  33. Container(
  34. padding: EdgeInsets.symmetric(vertical: 8, horizontal: 12),
  35. decoration: BoxDecoration(
  36. color: Colors.amber,
  37. borderRadius: BorderRadius.circular(2)
  38. ),
  39. width: double.infinity,
  40. child: Text('Warning Test'),
  41. ),
  42. SizedBox(height: 16),
  43. TextFormField(
  44. controller: _widthController,
  45. decoration: InputDecoration(labelText: 'Width (mm)'),
  46. keyboardType: TextInputType.number,
  47. validator: (value) {
  48. if (value == null || value.isEmpty) {
  49. return 'Please enter width';
  50. }
  51. double? parsedValue = double.tryParse(value);
  52. if (parsedValue is! double) {
  53. return 'Not a valid input';
  54. }
  55. if (parsedValue < 30) {
  56. return 'Width should be >= 30';
  57. }
  58. if (parsedValue > 1000) {
  59. return 'Width should be > 1000';
  60. }
  61. return null;
  62. },
  63. ),
  64. TextFormField(
  65. controller: _heightController,
  66. decoration: InputDecoration(labelText: 'Height (mm)'),
  67. keyboardType: TextInputType.number,
  68. validator: (value) {
  69. if (value == null || value.isEmpty) {
  70. return 'Please enter height';
  71. }
  72. double? parsedValue = double.tryParse(value);
  73. if (parsedValue is! double) {
  74. return 'Not a valid input';
  75. }
  76. if (parsedValue < 15) {
  77. return 'Width should be >= 15';
  78. }
  79. if (parsedValue > 1000) {
  80. return 'Width should be >= 1000';
  81. }
  82. return null;
  83. },
  84. ),
  85. SizedBox(height: 20),
  86. ElevatedButton(
  87. onPressed: () async {
  88. if (_formKey.currentState!.validate()) {
  89. // confirmation
  90. final confirmationResult = await showDialog(
  91. barrierDismissible: false,
  92. context: context,
  93. builder: (context) => AlertDialog(
  94. title: Text('Update Label Size'),
  95. content: Text('updating label size will remove all history you have changed, Are you sure want to update label size ?'),
  96. actions: [
  97. TextButton(
  98. onPressed: () => Navigator.pop(context, true),
  99. child: Text('Update label size')
  100. ),
  101. TextButton(
  102. onPressed: () => Navigator.pop(context, false),
  103. child: Text('Cancel')
  104. )
  105. ],
  106. ),
  107. );
  108. if (!confirmationResult) return;
  109. // Process data
  110. editorProvider.updateCanvasProperty(
  111. context,
  112. double.parse(_widthController.text) * 10 ,
  113. double.parse(_heightController.text) * 10
  114. );
  115. Navigator.pop(context);
  116. }
  117. },
  118. child: Text('Submit'),
  119. ),
  120. ],
  121. ),
  122. ),
  123. ),
  124. );
  125. }
  126. }