PageView Builder;通过页面视图重复文字

时间:2019-03-29 05:55:40

标签: dart flutter

我正在建立一些包含大量文本的网页浏览,如果内容超出屏幕,我想水平滚动

这是用来阅读的,就像一本书一样。我尝试使用Text而不是RichText,但是得到了相同的结果。

  @override
  Widget build(BuildContext context) {
    return  MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Color.fromRGBO(245, 205, 121, 1.0 ),
          title: Text('MyTitle'),
        ),
        body: 
        new PageView.builder(
        physics: ScrollPhysics(),
        controller: controller,
        itemBuilder: (context, index) {
          return new Center(
            child: new RichText(
              text:  TextSpan(style: TextStyle(fontSize: 25.0, color: Colors.black), text: myText),
              textAlign: TextAlign.start,
              textScaleFactor: 1.0,
              textDirection: TextDirection.ltr,

              ),
            );
          },
          )
      )

    ); 
  }
var myText = "A Lot of text here"

我希望lorem ipsum可以继续使用,但是所有页面的内容都相同。Same Text in all PageView

1 个答案:

答案 0 :(得分:1)

该功能将为您提供帮助,我想尝试一下。

  String currentPageText(int index, String myText) {
    // I assume 1000 is the length of the text that fits into your page, you can change it according to your page&textFont&textStyle
    if (index * 1000 < myText.length) {
      // you dont need the next line code if your index start from 1 but it can stay for a safety
      if (index == 0) return myText.substring(0, index * 1000); 

      return myText.substring((index - 1) * 1000, index * 1000);
    } else {
      if ((index - 1) * 1000 > myText.length) {
        return "Your Text Done"; // This is for your text finishes but your index still increases
      }
      return myText.substring((index - 1) * 1000, myText.length);
    }
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              backgroundColor: Color.fromRGBO(245, 205, 121, 1.0),
              title: Text('MyTitle'),
            ),
            body:
            new PageView.builder(
              physics: ScrollPhysics(),
              controller: controller,
              itemBuilder: (context, index) {
                return new Center(
                  child: new RichText(
                    text: TextSpan(
                        style: TextStyle(fontSize: 25.0, color: Colors.black),
                        text: currentPageText(index, myText)),
                    textAlign: TextAlign.start,
                    textScaleFactor: 1.0,
                    textDirection: TextDirection.ltr,

                  ),
                );
              },
            )
        )

    );
  }

example to see in dartpad