123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- import 'package:flutter/material.dart';
- import 'package:kmpdfkit_demo/widgets/contains.dart';
- import 'package:kmpdfkit_demo/widgets/events.dart';
- import '../../models/annot_attribute_bean.dart';
- typedef AttrLinkCallback = Function(LinkType linkType, String linkUrl, int linkPage, String linkEmail);
- typedef AttrLinkCancelCreateCallback = Function(bool cancelCreateLink);
- class AttrLinkWidget extends StatefulWidget {
- final AttrLinkCallback callback;
- final AttrLinkCancelCreateCallback cancelCreateCallback;
- final AnnotAttributeBean attributeBean;
- const AttrLinkWidget({Key? key,required this.attributeBean, required this.callback, required this.cancelCreateCallback}) : super(key: key);
- @override
- State<AttrLinkWidget> createState() => _AttrLinkWidgetState();
- }
- class _AttrLinkWidgetState extends State<AttrLinkWidget> {
- final TextEditingController _webTextController =
- TextEditingController(text: 'http://');
- final TextEditingController _pageTextController = TextEditingController();
- final TextEditingController _emailTextController = TextEditingController();
- late LinkType selectType;
- bool linkDataIsValid = true;
- int pageCount = 1;
- @override
- void initState() {
- super.initState();
- initLinkData();
- getDocumentPageCount().then((value) {
- setState(() {
- pageCount = value;
- });
- });
- }
- @override
- Widget build(BuildContext context) {
- return AlertDialog(
- title: const Text('New Link'),
- content: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- children: [
- _linkTypeWidget(LinkType.website, Icons.language, 'URL'),
- _linkTypeWidget(LinkType.page, Icons.description_outlined, 'Page'),
- _linkTypeWidget(LinkType.email, Icons.email_outlined, 'Email')
- ],
- ),
- if (selectType == LinkType.website) ...{
- TextField(
- controller: _webTextController,
- keyboardType: TextInputType.url,
- onChanged: (value){
- checkDataIsValid();
- },
- )
- } else if (selectType == LinkType.page) ...{
- TextField(
- controller: _pageTextController,
- keyboardType: TextInputType.number,
- decoration: InputDecoration(hintText: '1-$pageCount',hintStyle: const TextStyle(fontSize: 12)),
- onChanged: (value){
- checkDataIsValid();
- },
- )
- } else ...{
- TextField(
- controller: _emailTextController,
- keyboardType: TextInputType.emailAddress,
- decoration: const InputDecoration(hintText: 'support@17pdf.com',hintStyle: TextStyle(fontSize: 12)),
- onChanged: (value){
- checkDataIsValid();
- },
- )
- }
- ],
- ),
- actions: [
- TextButton(
- onPressed: () {
- widget.cancelCreateCallback(true);
- Navigator.pop(context);
- },
- child: const Text('Cancel', style: TextStyle(color: Colors.grey),)),
- TextButton(onPressed: linkDataIsValid ? () {
- widget.callback(selectType, _webTextController.text, _pageTextController.text.isNotEmpty ? int.parse(_pageTextController.text) : 0, _emailTextController.text);
- Navigator.pop(context);
- } : null, child: Text('Done',style: TextStyle(color: linkDataIsValid ? Colors.blue : Colors.grey),))
- ],
- );
- }
- void checkDataIsValid() {
- setState(() {
- switch(selectType){
- case LinkType.website:
- linkDataIsValid = _webTextController.text.isNotEmpty;
- break;
- case LinkType.page :
- linkDataIsValid = _pageTextController.text.isNotEmpty;
- break;
- case LinkType.email:
- linkDataIsValid = _emailTextController.text.isNotEmpty;
- break;
- }
- });
- }
- Widget _linkTypeWidget(LinkType linkType, IconData icon, String title) {
- return InkWell(
- child: Column(
- children: [
- Icon(icon, color: selectType == linkType ? Colors.blue : Colors.grey),
- Text(
- title,
- style: TextStyle(
- color: selectType == linkType ? Colors.blue : Colors.grey),
- )
- ],
- ),
- onTap: (){
- setState(() {
- selectType = linkType;
- });
- checkDataIsValid();
- },
- );
- }
- void initLinkData(){
- selectType = widget.attributeBean.linkType;
- switch(selectType){
- case LinkType.website:
- _webTextController.text = widget.attributeBean.linkWeb.isEmpty ? "http://" : widget.attributeBean.linkWeb;
- break;
- case LinkType.email:
- _emailTextController.text = widget.attributeBean.linkEmail;
- break;
- case LinkType.page:
- _pageTextController.text = widget.attributeBean.linkPage.toString();
- break;
- }
- }
- }
|