如何在颤振中制作可滚动列表

时间:2021-02-15 04:38:24

标签: flutter dart

当我尝试生成比我的页面可以显示的更多的文本时,我收到此错误 enter image description here

有没有办法让内容可以滚动? 我遵循了一些一般说明,只要我使用 Text() 手动编写文本,一切都可以正常工作,遗憾的是这不符合我的要求,因为我需要自动生成数据。

import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    void main() => runApp(MaterialApp(
          debugShowCheckedModeBanner: false,
          home: QuoteList(),
        ));
    class QuoteList extends StatefulWidget {
      @override
      _QuoteListState createState() => _QuoteListState();
    }
    class _QuoteListState extends State<QuoteList> {
      List<Quote> quotes = [
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
        Quote(author: 'aaa', text: 'aaa'),
      ];
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            backgroundColor: Colors.grey[300],
            body: Container(
                width: double.infinity,
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                      begin: Alignment.topLeft,
                      end: Alignment.bottomRight,
                      colors: [
                        Colors.black,
                        Colors.black,
                      ]),
                ),
                child: Padding(
                  padding: EdgeInsets.fromLTRB(0.0, 3.0, 0.0, 3.0),
                  child: Column(
                    children: <Widget>[
                      Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: quotes
                            .map((quote) => QuoteCard(
                                quote: quote,
                                delete: () {
                                  setState(() {
                                    quotes.remove(quote);
                                  });
                                }))
                            .toList(),
                      )
                    ],
                  ),
                )));
      }
    }
    class QuoteCard extends StatelessWidget {
      final Quote quote;
      final Function delete;
      QuoteCard({this.quote, this.delete});
      @override
      Widget build(BuildContext context) {
        return Padding(
            padding: EdgeInsets.fromLTRB(0.0, 3.0, 0.0, 3.0),
            child: Container(
              child: Card(
                color: Colors.grey[800],
                child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: <Widget>[
                      Container(
                        child: Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: <Widget>[
                              SizedBox(
                                width: 20.0,
                              ),
                              Text(
                                '${quote.text}',
                                style: TextStyle(
                                  fontSize: 18.0,
                                  color: Colors.white,
                                  letterSpacing: 3.0,
                                ),
                              ),
                            ]),
                      ),
                      ListTile(
                        title: Row(
                          children: <Widget>[
                            Expanded(
                                child: RaisedButton(
                              child: Text("delete"),
                              onPressed: delete,
                              color: Colors.grey[900],
                              textColor: Colors.white,
                              padding: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
                            )),
                            SizedBox(
                              width: 20.0,
                            ),
                          ],
                        ),
                      )
                    ]),
              ),
            ));
      }
    }
    class Quote {
      String text;
      String author;
      Quote({String this.text, String this.author});
    }

4 个答案:

答案 0 :(得分:2)

请运行下面的代码。我已经从您现有的代码中解决了这个问题,

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
  debugShowCheckedModeBanner: false,
  home: QuoteList(),
));
class QuoteList extends StatefulWidget {
  @override
  _QuoteListState createState() => _QuoteListState();
}
class _QuoteListState extends State<QuoteList> {
  List<Quote> quotes = [
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey[300],
        body: Container(
            width: double.infinity,
            decoration: BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                  colors: [
                    Colors.black,
                    Colors.black,
                  ]),
            ),
            child: Padding(
              padding: EdgeInsets.fromLTRB(0.0, 3.0, 0.0, 3.0),
              child: SingleChildScrollView(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: quotes
                      .map((quote) => QuoteCard(
                      quote: quote,
                      delete: () {
                        setState(() {
                          quotes.remove(quote);
                        });
                      }))
                      .toList(),
                ),
              ),
            )));
  }
}
class QuoteCard extends StatelessWidget {
  final Quote quote;
  final Function delete;
  QuoteCard({this.quote, this.delete});
  @override
  Widget build(BuildContext context) {
    return Padding(
        padding: EdgeInsets.fromLTRB(0.0, 3.0, 0.0, 3.0),
        child: SingleChildScrollView(
          child: Container(
            child: Card(
              color: Colors.grey[800],
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: <Widget>[
                    Container(
                      child: Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: <Widget>[
                            SizedBox(
                              width: 20.0,
                            ),
                            Text(
                              '${quote.text}',
                              style: TextStyle(
                                fontSize: 18.0,
                                color: Colors.white,
                                letterSpacing: 3.0,
                              ),
                            ),
                          ]),
                    ),
                    ListTile(
                      title: Row(
                        children: <Widget>[
                          Expanded(
                              child: RaisedButton(
                                child: Text("delete"),
                                onPressed: delete,
                                color: Colors.grey[900],
                                textColor: Colors.white,
                                padding: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
                              )),
                          SizedBox(
                            width: 20.0,
                          ),
                        ],
                      ),
                    )
                  ]),
            ),
          ),
        ));
  }
}
class Quote {
  String text;
  String author;
  Quote({String this.text, String this.author});
}

enter image description here

答案 1 :(得分:1)

您可以使用 Listview.builder 使列表可滚动

答案 2 :(得分:0)

使用 SingleChildScrollView 小部件。

有关使用以下链接的 SingleChildScrollView 小部件的更多详细信息: https://api.flutter.dev/flutter/widgets/SingleChildScrollView-class.html

工作代码:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
      debugShowCheckedModeBanner: false,
      home: QuoteList(),
    ));
class QuoteList extends StatefulWidget {
  @override
  _QuoteListState createState() => _QuoteListState();
}
class _QuoteListState extends State<QuoteList> {
  List<Quote> quotes = [
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
    Quote(author: 'aaa', text: 'aaa'),
  ];
  @override
Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey[300],
        body: SingleChildScrollView(
        child: Container(
            width: double.infinity,
            decoration: BoxDecoration(
                gradient: LinearGradient(
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                    colors: [
                    Colors.black,
                    Colors.black,
                    ]),
            ),
            child: Padding(
                padding: EdgeInsets.fromLTRB(0.0, 3.0, 0.0, 3.0),
                child: Column(
                children: <Widget>[
                    Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: quotes
                        .map((quote) => QuoteCard(
                            quote: quote,
                            delete: () {
                                setState(() {
                                quotes.remove(quote);
                                });
                            }))
                        .toList(),
                    )
                ],
                ),
            )),
        ));
}
}
class QuoteCard extends StatelessWidget {
  final Quote quote;
  final Function delete;
  QuoteCard({this.quote, this.delete});
  @override
  Widget build(BuildContext context) {
    return Padding(
        padding: EdgeInsets.fromLTRB(0.0, 3.0, 0.0, 3.0),
        child: Container(
          child: Card(
            color: Colors.grey[800],
            child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  Container(
                    child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          SizedBox(
                            width: 20.0,
                          ),
                          Text(
                            '${quote.text}',
                            style: TextStyle(
                              fontSize: 18.0,
                              color: Colors.white,
                              letterSpacing: 3.0,
                            ),
                          ),
                        ]),
                  ),
                  ListTile(
                    title: Row(
                      children: <Widget>[
                        Expanded(
                            child: RaisedButton(
                          child: Text("delete"),
                          onPressed: delete,
                          color: Colors.grey[900],
                          textColor: Colors.white,
                          padding: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
                        )),
                        SizedBox(
                          width: 20.0,
                        ),
                      ],
                    ),
                  )
                ]),
          ),
        ));
  }
}
class Quote {
  String text;
  String author;
  Quote({String this.text, String this.author});
}

答案 3 :(得分:0)

将您的 cointner 包裹在 SingleChildScrollview 中。它将使您的列表可滚动。喜欢

child: SingleChildScrollView(
          child: Container()
  )