如何在末尾显示更多文本来代替TextOverflow.ellipsis inFlutter?

问题描述 投票:0回答:2

我想在文本上代替showMore实现ellipsis or fade类文本。就像我们可以通过React https://www.npmjs.com/package/react-show-more-text中的此库来完成的一样。

这是一个简单的长文本

Container(
  color: Colors.white,
  child: Text(
    "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
    overflow: TextOverflow.ellipsis,
  ),
)

现在,如果我的文本在设备上溢出,我想在showMore末尾显示文本,该文本通常显示覆盖容器中的其余内容。

flutter dart flutter-layout
2个回答
1
投票

请检查this插件。您需要将小部件的GlobalKey传递到要显示文本弹出窗口的位置。

ShowMoreTextPopup popup = ShowMoreTextPopup(context,
    text: text,
    textStyle: TextStyle(color: Colors.black),
    height: 200,
    width: 100,
    backgroundColor: Color(0xFF16CCCC));
popup.show(
  widgetKey: key,


0
投票

您可以执行以下操作,代码可以正常工作:

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatefulWidget {
  @override
  State<MyWidget> createState() => MyWidgetState();
}

class MyWidgetState extends State<MyWidget> {
  bool more = false;
  String text =
      "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.black,
      child: Column(children: <Widget>[
        more
            ? Text(text)
            : Text(
                text,
                overflow: TextOverflow.ellipsis,
              ),
        FlatButton(
          child: more ? Text("less") : Text("more"),
          onPressed: () => setState(() => more = !more),
        ),
      ]),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.