import 'package:flutter/material.dart';
import 'package:liftmanager/res/colors.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/theme_utils.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/my_button.dart';
import 'package:liftmanager/widgets/signature.dart';

class SignaturePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return SignatureState();
  }
}

class SignatureState extends State<SignaturePage> {
  final SignatureController _controller = SignatureController(
    penStrokeWidth: 5,
  );

  @override
  void initState() {
    super.initState();
    _controller.addListener(() => print("Value changed"));
  }

  @override
  Widget build(BuildContext context) {
    bool isDark = ThemeUtils.isDark(context);
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: "签名",
        actions: <Widget>[
          FlatButton(
            child: Text("确定", key: const Key('actionName')),
            textColor: Colours.dark_text,
            highlightColor: Colors.transparent,
            onPressed: () async {
              if (_controller.isNotEmpty) {
                var data = await _controller.toPngBytes();
//                Image image = Image.memory(data);
                NavigatorUtils.goBackWithParams(context, data);
              } else {
                toasts("未签名");
              }
            },
          )
        ],
      ),
      body: Column(
        children: <Widget>[
          Signature(controller: _controller, backgroundColor: isDark?Colours.dark_text_gray:Colors.white),
          Container(
            color: isDark?Colours.dark_text_gray:Colors.white,
              child: MyButton(
            onPressed: () async {
              setState(() => _controller.clear());
            },
            text: "重写",
          )),
        ],
      ),
    );
  }
}
