import 'dart:convert' as convert;

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/mine/mine_quick_entry/model/mine_quick_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class MineQuickEntry extends StatelessWidget {
  final Map<String, dynamic> data;
  Map<String, dynamic> json;
  MineQuickModel model;

  MineQuickEntry(this.data, {Key key}) : super(key: key) {
    String d = data['data'];
    json = convert.jsonDecode(d);
    model = MineQuickModel.fromJson(Map<String, dynamic>.from(json));
  }

  @override
  Widget build(BuildContext context) {
    int column = int.tryParse(model.layout) ?? 4;

    return (model?.items?.length ?? 0) <= 0
        ? Container()
        : Container(
            margin: EdgeInsets.only(
              left: 12.5,
              right: 12.5,
              top: 6,
              bottom: 6,
            ),
            width: double.infinity,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(7.5),
                color: HexColor.fromHex(model.myFunctionsBgColor)),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                (model?.title ?? '') == ''
                    ? Container()
                    : Container(
                        margin:
                            EdgeInsets.only(left: 12.5, right: 12.5, top: 12.5),
                        child: Text(
                          model.title,
                          style: TextStyle(
                              fontSize: 15, fontWeight: FontWeight.w500),
                        ),
                      ),
                GridView.builder(
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: column,
                    crossAxisSpacing: 1,
                    mainAxisSpacing: 1,
                    childAspectRatio: 1,
                  ),
                  itemCount: model?.items?.length ?? 0,
                  itemBuilder: (BuildContext context, int index) {
                    return GestureDetector(
                      child: Container(
                        child: Center(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              Container(
                                width: 36,
                                height: 36,
                                margin: EdgeInsets.only(bottom: 4),
                                child: CachedNetworkImage(
                                  imageUrl: model.items[index].icon,
                                  fit: BoxFit.cover,
                                ),
                              ),
                              Text(
                                model.items[index].name,
                                style: TextStyle(
                                    color: HexColor.fromHex('#333333'),
                                    fontSize: 11),
                              )
                            ],
                          ),
                        ),
                      ),
                      onTap: () {
                        RouterUtil.route(model.items[index].toJson(), context);
                      },
                    );
                  },
                ),
              ],
            ),
          );
  }
}
