import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gifimage/flutter_gifimage.dart';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

const base64_url = "[encoded data, 315936 characters]";

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

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

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {

  GifController controller1,controller2,controller3,controller4;

  @override
  void initState() {
    // TODO: implement initState
    controller1 = GifController(vsync: this);
    controller2 = GifController(vsync: this);
    controller4= GifController(vsync: this);
    WidgetsBinding.instance.addPostFrameCallback((_){
      controller1.repeat(min: 0,max: 53,period: Duration(milliseconds: 200));
    });
    WidgetsBinding.instance.addPostFrameCallback((_){
      controller2.repeat(min: 0,max: 13,period: Duration(milliseconds: 200));
      controller4.repeat(min: 0,max: 13,period: Duration(milliseconds: 200));
    });
    controller3 = GifController(vsync: this,duration: Duration(milliseconds: 200),reverseDuration: Duration(milliseconds: 200));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return DefaultTabController(length: 2,
    child: Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
        bottom: TabBar(
          tabs: <Widget>[Tab(child: Text("不同类型图片"),),Tab(child: Text("控制方式"),)],
        ),
      ),
      body: TabBarView(
        children: <Widget>[
          ListView(
            children: <Widget>[
              Text("资源"),
              GifImage(
                controller: controller1,
                image: AssetImage("images/animate.gif"),
              ),
              Text("网络"),
              GifImage(
                controller: controller2,
                image: NetworkImage("http://img.mp.itc.cn/upload/20161107/5cad975eee9e4b45ae9d3c1238ccf91e.jpg"),
              ),
              Text("内存"),
              GifImage(
                controller: controller4,
                image: MemoryImage(base64Decode(base64_url)),
              )
            ],
          ),
          Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  RaisedButton(
                    child: Text("无限循环"),
                    onPressed: (){
                      controller3.repeat(min: 0,max: 25,period: Duration(milliseconds: 500));
                    },
                  ),
                  RaisedButton(
                    child: Text("暂停"),
                    onPressed: (){
                      controller3.stop();
                    },
                  ),
                  RaisedButton(
                    child: Text("播放到末尾一次"),
                    onPressed: (){
                      controller3.animateTo(52,duration: Duration(milliseconds: 1000));
                    },
                  )
                ],
              ),
              Slider(
                  onChanged: (v){
                    controller3.value = v;
                    setState(() {

                    });
                  },
                max: 53,
                min: 0,
                value: controller3.value,
              ),
              GifImage(
                controller: controller3,
                image: AssetImage("images/animate.gif"),
              ),
            ],
          )
        ],
      ),
    ),);
  }
}
