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

前端 未结 7 954
小鲜肉
小鲜肉 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:08

    Try this.

    Watch the output from here ( Video )

    or refer below images

    import 'package:flutter/material.dart';
    
    class DemoPage extends StatefulWidget {
      final Widget child;
    
      DemoPage({Key key, this.child}) : super(key: key);
    
      _DemoPageState createState() => _DemoPageState();
    }
    
    class _DemoPageState extends State {
    
    String descText = "Description Line 1\nDescription Line 2\nDescription Line 3\nDescription Line 4\nDescription Line 5\nDescription Line 6\nDescription Line 7\nDescription Line 8";
    bool descTextShowFlag = false;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("DemoPage"),
          ),
          body: new Container(
            margin: EdgeInsets.all(16.0),
            child: new Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                  Text(descText,
                    maxLines: descTextShowFlag ? 8 : 2,textAlign: TextAlign.start),
                  InkWell(
                    onTap: (){ setState(() {
                    descTextShowFlag = !descTextShowFlag; 
                    }); },
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        descTextShowFlag ? Text("Show Less",style: TextStyle(color: Colors.blue),) :  Text("Show More",style: TextStyle(color: Colors.blue))
                      ],
                    ),
                  ),
              ],
            ),
          ),
        );
      }
    }
    

提交回复
热议问题