Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
18 kB
1
Indexable
Never
import 'dart:convert';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:remedia_prova/requests/request_bloc.dart';
import 'package:remedia_prova/screens/old_view_request_screen.dart';
import 'package:remedia_prova/utils/date_utils.dart';
import 'package:remedia_prova/utils/string_utils.dart';

const Color blue = Color.fromRGBO(0, 117, 194, 1);

class MainDashboard extends StatefulWidget {

  const MainDashboard({Key? key}) : super(key: key);

  @override
  _MainDashboardState createState() => _MainDashboardState();
}

class _MainDashboardState extends State<MainDashboard> {

  List<Request> requests = List.empty();

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    final width = size.width;
    final height = size.height;
    return Scaffold(
      backgroundColor: const Color.fromRGBO(250, 250, 250, 1),
      appBar: AppBar(
        backgroundColor: Colors.white,
        centerTitle: true,
        elevation: 0,
        title: Text(
          "Manutenzione",
          style: TextStyle(
            fontSize: height / 55,
            fontWeight: FontWeight.w400,
            color: blue
          )
        ),
        leading: const Icon(
          Icons.arrow_back_ios,
          color: blue
        ),
      ),
      body: Row(
        children: [
          /// Menù fisso
          Container(
            width: max(width / 6, 220),
            height: height,
            padding: EdgeInsets.only(top: height / 30),
            color: blue,
            child: ListView.builder(
              itemCount: 10,
              itemBuilder: (context, index) {
                final items = {
                  "All": const Icon(Icons.notes, size: 14, color: Colors.white,),
                  "New": const Icon(Icons.add, size: 15, color: Colors.white,),
                  "Queue": const Icon(Icons.queue, size: 12, color: Colors.white,),
                  "Assigned": const Icon(Icons.assignment_ind, size: 13, color: Colors.white,),
                  "Working": const Icon(Icons.work, size: 13, color: Colors.white,),
                  "Testing": const Icon(Icons.extension, size: 12, color: Colors.white,),
                  "Canceled": const Icon(Icons.cancel_outlined, size: 15, color: Colors.white,),
                  "Completed": const Icon(Icons.done, size: 15, color: Colors.white,),
                  "Blocked": const Icon(Icons.lock, size: 13, color: Colors.white,),
                };
                // Add new button
                if (index == 9) {
                  return Center(
                    child: Container(
                      alignment: Alignment.center,
                      margin: EdgeInsets.only(left: width / 25, right: width / 25),
                      height: 100,
                      child: MaterialButton(
                        color: Colors.white,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(20)
                        ),
                        child: Container(
                          margin: const EdgeInsets.all(8),
                          child: const Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Icon(
                                Icons.add,
                                color: blue
                              ),
                              Text(
                                "    Add new"
                              ),
                            ],
                          ),
                        ),
                        onPressed: () => GoRouter.of(context).pushNamed("/create"),
                      ),
                    ),
                  );
                }
                // Menu item
                return Container(
                  margin: EdgeInsets.symmetric(vertical: height / 100, horizontal: width / 45),
                  child: ListTile(
                    leading: Container(
                      width: 20,
                      height: 20,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(5),
                        border: Border.all(color: Colors.white),
                        color: Colors.transparent
                      ),
                      child: Container(
                        margin: const EdgeInsets.all(2),
                          child: items.values.elementAt(index)
                      )
                    ),
                    title: Text(
                      items.keys.elementAt(index),
                      style: TextStyle(
                        fontSize: height / 45,
                        fontWeight: FontWeight.w500,
                        color: Colors.white
                      ),
                    ),
                  ),
                );
              },
            )
          ),
          Container(
            width: width < 950 ? (width - max(width / 4.8, 260)) : width / 2,
            margin: const EdgeInsets.only(left: 20),
            child: ListView(
              children: [
                Container(
                  height: 100,
                  padding: const EdgeInsets.symmetric(horizontal: 20),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        "Dashboard",
                        style: TextStyle(
                            fontSize: height / 50,
                            fontWeight: FontWeight.w600,
                            color: blue
                        ),
                      ),
                      Container(
                        width: 100,
                        height: 25,
                        child: const Row(
                          children: [
                            Icon(Icons.search, color: Colors.white,),
                            Text("Search")
                          ],
                        ),
                        decoration: BoxDecoration(
                            color: Colors.grey.withOpacity(0.3),
                            borderRadius: BorderRadius.circular(20)
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.only(left: 20, right: width / 8),
                  child: Text(
                      "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam a tellus eget nulla faucibus",
                      textAlign: TextAlign.left,
                      style: TextStyle(
                          fontSize: height / 55,
                          fontWeight: FontWeight.w400,
                          color: Colors.grey
                      )
                  ),
                ),
                Container(
                  height: 100,
                  padding: const EdgeInsets.symmetric(horizontal: 20),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        children: [
                          FilterChip(
                              color: const Color.fromRGBO(191, 55, 73, 1),
                              title: "High",
                              onClick: () {}
                          ),
                          FilterChip(
                              color: const Color.fromRGBO(252, 152, 116, 1),
                              title: "Medium",
                              onClick: () {}
                          ),
                          FilterChip(
                              color: const Color.fromRGBO(252, 218, 169, 1),
                              title: "Low",
                              onClick: () {}
                          ),
                        ],
                      ),
                      Row(
                        children: [
                          FilterChip(
                              color: const Color.fromRGBO(31, 51, 194, 0.6),
                              title: "Most recent",
                              fill: true,
                              onClick: () {}
                          ),
                          FilterChip(
                              color: const Color.fromRGBO(31, 51, 194, 1.0),
                              title: "Oldest",
                              onClick: () {}
                          ),
                        ],
                      )
                    ],
                  ),
                ),
                BlocBuilder<RequestBloc, RequestState>(
                    builder: (context, state) {
                      if (state.status == RequestStatus.initial) {
                        context.read<RequestBloc>().add(GetRequestsEvent());
                      }
                      if (state.status == RequestStatus.loading) {
                        return const Center(child: CupertinoActivityIndicator());
                      }
                      requests = state.requests;
                      if (requests.isEmpty) {
                        return Center(child: Text("There are no requests", style: titleStyle));
                      }
                      return GestureDetector(
                        child: Container(
                          margin: const EdgeInsets.only(left: 15, right: 15),
                          child: ListView.separated(
                            shrinkWrap: true,
                              itemCount: requests.length,
                              separatorBuilder: (context, index) {
                               return Container(
                                 width: width < 950 ? (width - max(width / 6, 240)) : width / 2.4,
                                 height: 1,
                                 color: Colors.grey.withOpacity(0.2),
                               );
                              },
                              itemBuilder: (context, index) {
                                final request = requests.elementAt(index);
                                return GestureDetector(
                                  child: Container(
                                    margin: EdgeInsets.only(top: index == 0 ? 0 : 20, bottom: 20),
                                    child: Row(
                                      children: [
                                        Container(
                                          height: max(85, width / 20),
                                          width: max(85, width / 20),
                                          margin: const EdgeInsets.only(right: 20),
                                          color: blue,
                                        ),
                                        Expanded(
                                          child: Column(
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            children: [
                                              Text(
                                                "${request.issuer}",
                                                style: const TextStyle(
                                                    fontSize: 15,
                                                    fontWeight: FontWeight.w600,
                                                    color: blue
                                                ),
                                              ),
                                              const SizedBox(height: 7,),
                                              Row(
                                                children: [
                                                  const Icon(
                                                    Icons.location_on,
                                                    color: Colors.grey,
                                                    size: 13,
                                                  ),
                                                  Text(
                                                    "  ${request.productCategory + request.product}",
                                                    overflow: TextOverflow.ellipsis,
                                                    style: TextStyle(
                                                        fontSize: 14,
                                                        color: Colors.black.withOpacity(0.4)
                                                    ),
                                                  ),
                                                ],
                                              ),
                                              const SizedBox(height: 5,),
                                              Container(
                                                margin: const EdgeInsets.only(bottom: 15),
                                                child: Text(
                                                  "${request.problem}",
                                                  maxLines: 4,
                                                  overflow: TextOverflow.ellipsis,
                                                  style: TextStyle(
                                                      fontSize: 15,
                                                      color: Colors.black.withOpacity(0.6)
                                                  ),
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                        if (width > 650) Container(
                                          width: 125,
                                          child: Column(
                                            mainAxisAlignment: MainAxisAlignment.center,
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            children: [
                                              Text(
                                                  "${fancyDate(DateTime.fromMillisecondsSinceEpoch(int.parse(request.createdOn)))}",
                                                  style: const TextStyle(
                                                      fontSize: 14,
                                                      color: Colors.grey
                                                  )
                                              ),
                                              Text(
                                                "\n${request.status}".toCamelCase(),
                                                style: const TextStyle(
                                                    fontSize: 15,
                                                    color: blue
                                                ),
                                              )
                                            ],
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                  onTap: () {
                                    GoRouter.of(context).pushNamed("/view", params: {
                                      "request": jsonEncode(request.toJson()).toString()
                                    });
                                  },
                                );
                              }
                          ),
                        ),
                      );
                    }
                ),
              ],
            ),
          ),
          /// Il grafico a destra
          if (width > 950) Container()
        ]
      )
    );
  }
}

class FilterChip extends StatelessWidget {
  
  final Color color;
  final String title;
  final Function() onClick;
  final bool fill;
  
  const FilterChip({Key? key, required this.color, required this.title, required this.onClick, this.fill = false}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final height = MediaQuery.of(context).size.height;
    return Container(
      margin: const EdgeInsets.only(right: 10),
      height: 20,
      decoration: BoxDecoration(
        border: Border.all(color: fill ? Colors.transparent : color, width: 2),
        borderRadius: BorderRadius.circular(15),
        color: fill ? color : Colors.transparent
      ),
      child: Container(
        margin: const EdgeInsets.symmetric(horizontal: 7),
        child: Center(
          child: Text(
            "  $title  ",
            style: TextStyle(
              fontSize: height / 70,
              color: fill ? Colors.white : color
            ),
          ),
        ),
      ),
    );
  }
}