Flutter: How to hide or show more text within certain length

前端 未结 7 974
小鲜肉
小鲜肉 2020-11-29 07:00

My Container having a description about movies.

Initially i want to show only few lines of description. And below to that there should be a link (more...

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 07:21

    A simple example

    class ExpandableText extends StatefulWidget {
      ExpandableText(this.text);
    
      final String text;
      bool isExpanded = false;
    
      @override
      _ExpandableTextState createState() => new _ExpandableTextState();
    }
    
    class _ExpandableTextState extends State {
      @override
      Widget build(BuildContext context) {
        return new Column(children: [
          new ConstrainedBox(
              constraints: widget.isExpanded
                  ? new BoxConstraints()
                  : new BoxConstraints(maxHeight: 50.0),
              child: new Text(
                widget.text,
                softWrap: true,
                overflow: TextOverflow.fade,
              )),
          widget.isExpanded
              ? new Container()
              : new FlatButton(
                  child: const Text('...'),
                  onPressed: () => setState(() => widget.isExpanded = true))
        ]);
      }
    }
    

    with animation

    class ExpandableText extends StatefulWidget {
      ExpandableText(this.text);
    
      final String text;
      bool isExpanded = false;
    
      @override
      _ExpandableTextState createState() => new _ExpandableTextState();
    }
    
    class _ExpandableTextState extends State
        with TickerProviderStateMixin {
      @override
      Widget build(BuildContext context) {
        return new Column(children: [
          new AnimatedSize(
              vsync: this,
              duration: const Duration(milliseconds: 500),
              child: new ConstrainedBox(
                  constraints: widget.isExpanded
                      ? new BoxConstraints()
                      : new BoxConstraints(maxHeight: 50.0),
                  child: new Text(
                    widget.text,
                    softWrap: true,
                    overflow: TextOverflow.fade,
                  ))),
          widget.isExpanded
              ? new ConstrainedBox(constraints: new BoxConstraints())
              : new FlatButton(
              child: const Text('...'),
              onPressed: () => setState(() => widget.isExpanded = true))
        ]);
      }
    }
    

提交回复
热议问题