post_written.dart 3.88 KB
Newer Older
Herry's avatar
Herry committed
1 2
import 'package:animated_text_kit/animated_text_kit.dart';
import 'package:dashboard/constants/schema_demo_constants.dart';
Manuel Herold's avatar
Manuel Herold committed
3
import 'package:dashboard/ui/theme/color_holder.dart';
Herry's avatar
Herry committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
import 'package:flutter/material.dart';

enum PostStatus { WRITING, SENDING, DONE }

class WrittenPost extends StatefulWidget {
  @override
  _WrittenPostState createState() => _WrittenPostState();

  final TextStyle style = TextStyle(
    color: Colors.black,
    fontSize: 16,
    fontWeight: FontWeight.normal,
  );

  final Function onDone;
  final PostStatus initialStatus;
  final Duration pauseBetweenCharacters = Duration(milliseconds: 10);

  WrittenPost(this.onDone, {this.initialStatus = PostStatus.WRITING});
}

class _WrittenPostState extends State<WrittenPost> {
  PostStatus _status;

  void initState() {
    _status = widget.initialStatus;
    super.initState();
  }

  void _onWritingDone() {
    setState(() {
      _status = PostStatus.SENDING;
    });

    Future.delayed(Duration(milliseconds: 500))
        .then((value) => _onSendingDone());
  }

  void _onSendingDone() {
    setState(() {
      _status = PostStatus.DONE;
    });

    if (widget.onDone != null) widget.onDone();
  }

  @override
  Widget build(BuildContext context) {
    return Hero(
      tag: "post_written",
      child: SizedBox(
        height: 270,
        width: 400,
        child: Card(
Manuel Herold's avatar
Manuel Herold committed
58
          color: ColorHolder.color2,
Herry's avatar
Herry committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  "New Post",
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 22,
                  ),
                ),
                Divider(),
                Expanded(
                  child: _status == PostStatus.WRITING
                      ? TypewriterAnimatedTextKit(
                          speed: widget.pauseBetweenCharacters,
                          totalRepeatCount: 1,
                          text: [SchemaDemoConstants.post],
                          pause: Duration(milliseconds: 200),
                          textStyle: widget.style,
                          repeatForever: false,
                          onFinished: _onWritingDone,
                          textAlign: TextAlign.left,
                        )
                      : Text(
                          SchemaDemoConstants.post,
                          style: widget.style,
                        ),
                ),
                Divider(),
                _SendButton(_status),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class _SendButton extends StatelessWidget {
  final PostStatus status;

  _SendButton(this.status);

  Widget _icon() {
    switch (status) {
      case PostStatus.WRITING:
        return Icon(
          Icons.edit,
          color: Colors.white,
        );
      case PostStatus.SENDING:
        return SizedBox(
          width: 20,
          height: 20,
          child: CircularProgressIndicator(
            valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
          ),
        );
      case PostStatus.DONE:
      default:
        return Icon(
          Icons.check,
          color: Colors.white,
        );
    }
  }

  @override
  Widget build(BuildContext context) {
    return TextButton.icon(
      onPressed: () {},
      icon: Padding(
        padding: EdgeInsets.all(4),
        child: _icon(),
      ),
      style: TextButton.styleFrom(
        backgroundColor: status == PostStatus.WRITING
            ? Theme.of(context).primaryColor
            : Colors.grey,
      ),
      label: Padding(
        padding: EdgeInsets.only(top: 4, bottom: 4, right: 4),
        child: Text(
          "Submit",
          style: TextStyle(
            color: Colors.white,
          ),
        ),
      ),
    );
  }
}