import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'OpenRoom.dart'; //创建房间
import 'dart:async';
import '../widget/LinearButton.dart';

class StartWindow extends StatefulWidget {
  @override
  StartWindowState createState() => StartWindowState();
}

class StartWindowState extends State<StartWindow> {
  int _num = 10;
  bool isNext = true;
  Timer timer;

  @override
  void initState() {
    super.initState();

    timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (_num == 0) {
        Navigator.of(context).pop(false);
        timer.cancel();
      } else {
        setState(() {
          _num--;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 375, height: 667)..init(context);
    return GestureDetector(
      // onTap: () {
      //   Navigator.of(context).pop();
      // },
      child: WillPopScope(
        child: Scaffold(
          backgroundColor: Color(0xCC000000),
          body: Container(
            child: Center(
                child: Container(
              width: ScreenUtil().setWidth(272),
              height: ScreenUtil().setWidth(319),
              decoration: BoxDecoration(
                  color: Color(0xFF15151D),
                  image: DecorationImage(
                    image: AssetImage('images/img_guangmang2.png'),
                  ),
                  border: Border.all(width: 1, color: Theme.of(context).primaryColor)),
              child: Stack(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.only(top: 29, bottom: 21),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text('点击确认开始游戏',
                            style: TextStyle(
                                color: Color(0xFFFDC372),
                                fontSize: 20,
                                fontWeight: FontWeight.w600)),
                        _centerContent(),
                        Container(
                            width: double.infinity,
                            margin: EdgeInsets.symmetric(horizontal: 42),
                            height: 44,
                            child: LinearButton(
                              btntext: '确认',
                              onTapHomeMenu: () {
                                timer.cancel();
                                Navigator.of(context).pop(true);
                              },
                            )
                            //   RaisedButton(
                            //   textTheme: ButtonTextTheme.primary,
                            //   child:
                            //   Text('确认'),
                            //   onPressed: () {
                            //     timer.cancel();
                            //    Navigator.of(context).pop(true);
                            //   },
                            // ),
                            )
                      ],
                    ),
                  ),
                  Positioned(
                    top: 0,
                    left: 0,
                    child: Image.asset(
                      'images/tancuang_shang.png',
                      // width: 131,
                    ),
                  ),
                  Positioned(
                    bottom: 0,
                    right: 4,
                    child: Image.asset(
                      'images/tancuang_xia.png',
                      // width: 148,
                    ),
                  )
                ],
              ),
            )),
          ),
        ),
        onWillPop: () {
          return Future.value(false);
        },
      ),
    );
  }

  Widget _centerContent() {
    return Container(
      width: ScreenUtil().setWidth(131),
      height: ScreenUtil().setWidth(131),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(100)),
        gradient: LinearGradient(
          colors: [Color(0xFFC18C4A), Color(0xFFE2AD5C)],
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
        ),
      ),
      child: Center(
        child: Container(
          width: ScreenUtil().setWidth(80),
          height: ScreenUtil().setWidth(80),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(100)),
            color: Color(0xFFF5C271),
          ),
          child: Center(
            child: Container(
              width: ScreenUtil().setWidth(60),
              height: ScreenUtil().setWidth(60),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(100)),
                gradient: LinearGradient(
                  colors: [Color(0xFFDF605C), Color(0xFFB34B48)],
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                ),
              ),
              child: Center(
                child: Text(
                  '$_num',
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: 32,
                      fontWeight: FontWeight.w600),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
