import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'Cat.dart';
import 'layout_page.dart';

/// 有状态组件
class FulGroupPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _StatefulGroupState();
}

class _StatefulGroupState extends State<FulGroupPage> {
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Stateless学习',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text("Stateful基础组件"),
          leading: GestureDetector(
            onTap: () {
              Navigator.pop(context);
            },
            child: Icon(Icons.arrow_back),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            Logger().log("点我了");
          },
          child: Text('click'),
        ),
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: (index) {
            setState(() {
              _currentIndex = index;
            });
          },
          items: [
            BottomNavigationBarItem(
                icon: Icon(
                  Icons.home,
                  color: Colors.grey,
                ),
                activeIcon: Icon(
                  Icons.home,
                  color: Colors.blue,
                ),
                label: "首页"),
            BottomNavigationBarItem(
                icon: Icon(
                  Icons.accessibility,
                  color: Colors.grey,
                ),
                activeIcon: Icon(
                  Icons.accessibility,
                  color: Colors.blue,
                ),
                label: "我的"),
          ],
        ),
        body: _currentIndex == 0
            ? Container(
                decoration: BoxDecoration(color: Colors.white),
                alignment: Alignment.center,
                child: Column(
                  children: <Widget>[
                    Image.network(
                      "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600755280305&di=a7eeab2e720aac7cadfbd27b670dbdb1&imgtype=0&src=http%3A%2F%2Fpix2.tvzhe.com%2Fthumb%2Fstar%2F60%2F87%2F260x346.jpg",
                      width: 100,
                      height: 100,
                    ),
                    TextField(
                      decoration: InputDecoration(
                          contentPadding: EdgeInsets.fromLTRB(5, 0, 0, 0),
                          hintText: "请输入",
                          hintStyle: TextStyle(fontSize: 12)),
                    ),
                    Container(
                      height: 100,
                      margin: EdgeInsets.only(top: 10),
                      decoration: BoxDecoration(color: Colors.lightBlue),
                      child: PageView(
                        children: <Widget>[
                          _item('page1', Colors.lightBlue),
                          _item('page2', Colors.red),
                          _item('page3', Colors.green)
                        ],
                      ),
                    ),
                  ],
                ),
              )
            : RefreshIndicator(
                child: ListView(
                  children: <Widget>[Text(text)],
                ),
                onRefresh: handleRefresh,
              ),
      ),
    );
  }

  String text = "下拉刷新 ";

  Future<String> handleRefresh() async {
    text = "刷新中";
    setState(() {});
    await Future.delayed(Duration(milliseconds: 2000));
    text = "刷新完成";
    setState(() {});
    return text;
  }

  _my() {
    return LayoutPage();
  }

  _item(String title, MaterialColor color) {
    return Container(
      alignment: Alignment.center,
      decoration: BoxDecoration(color: color),
      child: GestureDetector(
        onTap: () {
          Fluttertoast.showToast(msg: null);
        },
        child: Text(
          title,
          style: TextStyle(fontSize: 22, color: Colors.white),
        ),
      ),
    );
  }
}
