【问题标题】:how can i set min height to my containe and after colick on button then wrapcontent height be equal of text height如何将最小高度设置为我的容器,然后单击按钮后包装内容高度等于文本高度
【发布时间】:2021-01-03 18:20:24
【问题描述】:

我想在颤动中显示更多带有设置动画到容器的文本。我想为我的容器设置最小高度,然后设置动画以扩展我的容器。 喜欢这张图片 https://raw.githubusercontent.com/Blogcat/Android-ExpandableTextView/master/demo.gif

【问题讨论】:

  • 欢迎来到 SO。到目前为止,您尝试了哪些方法来解决问题?

标签: android flutter expandable


【解决方案1】:

您可以在下面复制粘贴运行完整代码
您可以将AnimatedContainerText overflow: TextOverflow.fade 一起使用
代码sn-p

ShowMore(
          content: '...',
          desiredMaxHeight: 200.0,
          desiredMinHeight: 56.0,
        ),
...
AnimatedContainer(
          duration: const Duration(milliseconds: 700),
          width: double.infinity,
          height: showStatus == ShowStatus.EXPANDED
              ? widget.desiredMaxHeight
              : widget.desiredMinHeight,
          child: Text(
            widget.content,
            softWrap: true,
            overflow: TextOverflow.fade,
          )),

工作演示

完整代码

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';

class ShowMore extends StatefulWidget {
  /// The string which will be used by Text widget
  final String content;

  /// The height in pixels of the largest possible size an AnimatedContainer will be
  final double desiredMaxHeight;

  /// The height in pixels of the smallest possible size an AnimatedContainer will be
  final double desiredMinHeight;

  const ShowMore(
      {Key key,
      @required this.content,
      @required this.desiredMaxHeight,
      @required this.desiredMinHeight})
      : assert(content != null),
        assert(desiredMaxHeight != null),
        assert(desiredMinHeight != null),
        super(key: key);

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

class ShowMoreState extends State<ShowMore> {
  _ontap update;
  ShowStatus showStatus;

  void _update() {
    setState(() {
      showStatus = showStatus == ShowStatus.EXPANDED
          ? ShowStatus.UNEXPANDED
          : ShowStatus.EXPANDED;
    });
  }

  @override
  void initState() {
    super.initState();
    showStatus = ShowStatus.UNEXPANDED;
    update = _update;
  }

  @override
  Widget build(BuildContext context) {
    return Column(children: <Widget>[
      AnimatedContainer(
          duration: const Duration(milliseconds: 700),
          width: double.infinity,
          height: showStatus == ShowStatus.EXPANDED
              ? widget.desiredMaxHeight
              : widget.desiredMinHeight,
          child: Text(
            widget.content,
            softWrap: true,
            overflow: TextOverflow.fade,
          )),
      Container(
          width: double.infinity,
          height: 56.0,
          child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                showStatus == ShowStatus.EXPANDED
                    ? RaisedButton(
                        onPressed: () {
                          update();
                        },
                        child: Text("COLLAPSE"))
                    : RaisedButton(
                        onPressed: () {
                          update();
                        },
                        child: Text("EXPAND")),
              ])),
    ]);
  }
}

/// Function that is called when updating a widget
typedef void _ontap();

enum ShowStatus {
  UNEXPANDED,
  EXPANDED,
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            ShowMore(
              content:
                  'Lorem ipsum dolor sit amet, prima liberavisse cu mei, his elit fabulas ex. Ne justo dicunt referrentur eam. Eum nulla eleifend id, ex vim ferri vitae pericula, qui epicurei interpretaris te. His ei debitis habemus intellegebat, essent assentior incorrupte ne has. Has te inani option, qui eu etiam feugiat epicurei, posidonium dissentias at nam. Ne his malis probatus consequat. Purto neglegentur vim ea, et vim reque quaestio corrumpit, perfecto singulis no his. Homero minimum efficiendi vix no. Vel an adhuc debet constituam, dicant consul percipitur nam ut, pri vide dicam feugait ei. Lorem homero graeci ex nam, labitur virtute mnesarchum in mel.',
              desiredMaxHeight: 200.0,
              desiredMinHeight: 56.0,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-31
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    相关资源
    最近更新 更多