【问题标题】:How to make Scrollable text in flutter?如何在颤动中制作可滚动的文本?
【发布时间】:2018-04-03 20:19:32
【问题描述】:

我在 Column 小部件的顶部有一个图像,之后有一个标题是 Text 小部件,之后,还有一个 Text 小部件包含一些描述并且超出了屏幕并给出了呈现错误。

所以我想让那个文本视图可以滚动,这样它就应该完全可见并且也可以滚动。并且它的大小必须是动态的,因为来自 API 的数据。 我尝试了几种方法,但无法完成。 这是截图

截图:

@override


 Widget build(BuildContext context) {
    var size = MediaQuery
        .of(context)
        .size;
    final double itemHeight = (size.height - kToolbarHeight - 24) / 2;
    final double itemWidth = size.width;


return new Container(
  child: new Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: <Widget>[
      new Padding(
        padding: const EdgeInsets.fromLTRB(0.0, 24.0, 0.0, 0.0),
        child: new Image.asset(
          'assets/img/noconnection.png',
          height: 200.0,
          width: itemWidth,
        ),
      ),
      new Padding(
        padding: const EdgeInsets.all(12.0),
        child: new Text(
          "Some Heading Text",
          style: new TextStyle(
              fontSize: 28.0,
              color: Colors.black87,
              fontWeight: FontWeight.w600),
        ),
      ),
      new SingleChildScrollView(
        child: new Text(
          "Description that is too long in text format(Here Data is coming from API)",
          style: new TextStyle(
            fontSize: 16.0, color: Colors.black87,
          ),
        ),
      ),
    ],
  ),
);

}

【问题讨论】:

  • new SingleChildScrollView( child: new Text( "描述文本格式太长", style: new TextStyle( fontSize: 16.0, color: Colors.black87, ), ), ), 改变了上面代码列中的第三个孩子......但不工作:(
  • 你能用新的格式化代码更新问题吗?
  • @RaoufRahiche 更新了代码 ...

标签: scroll flutter scrollable


【解决方案1】:

您需要将 SingleChildScrollView 包装在 Expanded 小部件中,您将获得所需的内容。

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

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

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var size = MediaQuery
        .of(context)
        .size;
    final double itemHeight = (size.height - kToolbarHeight - 24) / 2;
    final double itemWidth = size.width;

    return new Container(
      child: new Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          new Padding(
            padding: const EdgeInsets.fromLTRB(0.0, 24.0, 0.0, 0.0),
            child: new Image.asset(
              'assets/rp1.jpg',
              height: 200.0,
              width: itemWidth,
            ),
          ),
          new Padding(
            padding: const EdgeInsets.all(12.0),
            child: new Text(
              "Some Heading Text",
              style: new TextStyle(
                  fontSize: 28.0,
                  color: Colors.white,
                  fontWeight: FontWeight.w600),
            ),
          ),
          new Expanded(
            flex: 1,
            child: new SingleChildScrollView(
              scrollDirection: Axis.vertical,//.horizontal
              child: new Text(
                "1 Description that is too long in text format(Here Data is coming from API) jdlksaf j klkjjflkdsjfkddfdfsdfds " + 
                "2 Description that is too long in text format(Here Data is coming from API) d fsdfdsfsdfd dfdsfdsf sdfdsfsd d " + 
                "3 Description that is too long in text format(Here Data is coming from API)  adfsfdsfdfsdfdsf   dsf dfd fds fs" + 
                "4 Description that is too long in text format(Here Data is coming from API) dsaf dsafdfdfsd dfdsfsda fdas dsad" + 
                "5 Description that is too long in text format(Here Data is coming from API) dsfdsfd fdsfds fds fdsf dsfds fds " + 
                "6 Description that is too long in text format(Here Data is coming from API) asdfsdfdsf fsdf sdfsdfdsf sd dfdsf" + 
                "7 Description that is too long in text format(Here Data is coming from API) df dsfdsfdsfdsfds df dsfds fds fsd" + 
                "8 Description that is too long in text format(Here Data is coming from API)" + 
                "9 Description that is too long in text format(Here Data is coming from API)" + 
                "10 Description that is too long in text format(Here Data is coming from API)",     
                style: new TextStyle(
                  fontSize: 16.0, color: Colors.white,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

【讨论】:

  • 没有解决问题...使用 Expand 作为 SingleChildScrollView 的父级会在屏幕上显示注释.. 甚至顶部的图像和标题文本都没有.. 它给出以下错误...
  • I/flutter ( 1653): Another exception was thrown: RenderFlex children have non-zero flex but incoming height constraints are unbounded. I/flutter ( 1653): Another exception was thrown: 'package:flutter/src/rendering/box.dart': Failed assertion: line 1446 pos 12: 'hasSize': is not true. I/flutter ( 1653): Another exception was thrown: 'package:flutter/src/rendering/box.dart': Failed assertion: line 1446 pos 12: 'hasSize': is not true.I/flutter ( 1653): Another exception was thrown: NoSuchMethodError: The method '&lt;=' was called on null.
  • 我已经添加了对我有用的代码以供参考。
  • 非常感谢你@jhon Wiese...它起作用了,但我仍然不知道我到底犯了什么错误......无论如何,非常感谢你:)跨度>
  • @JohnWiese Perfect.
【解决方案2】:

如果您想触摸屏幕的任何部分并能够滚动,请使用此示例。 我使用黄色和绿色来表示整个屏幕会滚动,而不仅仅是文本区域。

import 'package:flutter/material.dart';

class ShowFilePage extends StatefulWidget {
  @override
  _ShowFilePageState createState() => _ShowFilePageState();
}

class _ShowFilePageState extends State<ShowFilePage> {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
        backgroundColor: Colors.yellow,
        appBar: AppBar(
          title: Text('Text'),
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Expanded(
                child: Container(
                    color: Colors.green,
                    child: SingleChildScrollView(child: Text('insert long text here')))),
          ],
        ));
  }
}

【讨论】:

    【解决方案3】:

    对于垂直和水平滚动文本,我使用了以下代码。

    SingleChildScrollView(
        scrollDirection: Axis.vertical,
        child: SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: Text('Your text.......')
        ),
      );
    

    【讨论】:

      【解决方案4】:

      为简单起见,只需使用 3rd pub:Marquee

      Marquee(
        text: 'There once was a boy who told this story about a boy: "',
      )
      

      【讨论】:

      • 这样做会给我带来错误,或者简单地说,它不允许我使用选取框,无论我使用什么容器、列等。
      • @AnAndroid 您可以在该库的 github 页面上创建问题。
      • Marquee 的作者已经说过,他有一段时间没有研究这个包了,现在因为大部分东西都是新的/改变的,所以现在他要更新它(整个包,他需要时间),这就是我所知道的,这就是我来这里寻找其他方式的原因!顺便说一句,谢谢!
      【解决方案5】:

      只需将根容器放在 SingleChildScrollView() 小部件中即可。

      SingleChildScrollView(
        child: Container() // your root container
      )
      

      【讨论】:

        猜你喜欢
        • 2021-05-18
        • 1970-01-01
        • 1970-01-01
        • 2019-11-30
        • 2019-08-25
        • 2020-06-17
        • 2019-10-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多