import 'dart:io';
import 'dart:typed_data';
import 'package:image_picker/image_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';
import 'UnitModel.dart';

import 'Util.dart';

class PhotoViewer extends StatefulWidget {
  Uint8List byte;
  String id;
  List<jenisDrop> dropMenu;
  bool edit;
  String jenis;
  PhotoViewer(this.byte,{this.id,this.edit= true,this.dropMenu,this.jenis});
//  PhotoViewer({Key key}) : super(key: key);
  @override
  _PhotoViewerState createState() => _PhotoViewerState();
}

class _PhotoViewerState extends State<PhotoViewer> {
  Util util = new Util();
  String jenisImages;
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Flexible(
          flex: 8,
          child:  Container(
            child: Hero(
              tag: widget.id,
              child: PhotoView(
                imageProvider: MemoryImage(widget.byte),
              ),
            ),
          ),
        ),
        Expanded(
          flex: 1,
          child:  Material(
            color: Colors.transparent,
            child: Container(
              child: Text(widget.jenis,style: TextStyle(color: Colors.white,fontSize: 18),),
            ),
          ),
        ),
        (widget.edit)?Flexible(
          flex: 1,
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              FlatButton(
                child: Text('Cancel'),
                color: Colors.red,
                onPressed: (){Navigator.pop(context);},
              ),
              FlatButton(
                child: Text('Change'),
                color: Colors.green,
                onPressed: ()async{
                    File temp = await ImagePicker.pickImage(source: ImageSource.camera,maxWidth: 800);
                    if(temp!=null){
                      util.showLoading(context);
                      widget.byte = temp.readAsBytesSync();
                      temp.deleteSync();
                      setState(() {

                      });
                      await Future.delayed(Duration(seconds: 1));
                    }
                    Navigator.pop(context);
                    if(temp!= null)Navigator.pop(context,{'byte':widget.byte});
                },
              ),
            ],
          ),
        ):Container()
      ],
    );
  }
}