【问题标题】:Flutter: Vertical scrolling inside a containerFlutter:容器内的垂直滚动
【发布时间】:2020-04-11 15:38:25
【问题描述】:

如何添加滚动以使多个文本答案可滚动?我尝试使用 SingleChildScrollView,但无法滚动,文本答案消失,页面不滚动。

class Answer extends StatelessWidget {
  final Function selectHandler;
  final String answerText;

  Answer(this.selectHandler, this.answerText);

  @override
Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 0),
      child: RaisedButton(
        padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 16.0),
        color: Color(0xfff4f4f4),
        textColor: Color(0xff3a3535),
        child: Text(answerText, style: TextStyle(
            fontFamily: 'VT323', fontSize: 22)),
        onPressed: selectHandler,
      ),
      decoration: BoxDecoration(
        // color: Color.fromARGB(255, 238, 238, 238),
        boxShadow: [
          BoxShadow(offset: Offset(10, 10),color: Color.fromARGB(80, 0, 0, 0),blurRadius: 10),
          BoxShadow(offset: Offset(-10, -10),color: Color.fromARGB(150, 255, 255, 255),blurRadius: 10)
        ],
      ),
    );
  }
}

ListView.builder 在这里我得到以下错误:

"类型'String'不是类型'List'的子类型

class Answer extends StatelessWidget {
  final Function selectHandler;
  final List<String> answerText;

  Answer(this.selectHandler, this.answerText);

  @override
Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: 60.0,
        width: double.infinity,
        margin: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 0),
        decoration: BoxDecoration(
          // color: Color.fromARGB(255, 238, 238, 238),
          boxShadow: [
            BoxShadow(
                offset: Offset(10, 10),
                color: Color.fromARGB(80, 0, 0, 0),
                blurRadius: 10),
            BoxShadow(
              offset: Offset(-10, -10),
              color: Color.fromARGB(150, 255, 255, 255),
              blurRadius: 10,
            ),
          ],
        ),
        child: ListView.builder(
          itemBuilder: _buildAnswerItem,
          itemCount: answerText.length,
        ),
      ),
    );
  }
  Widget _buildAnswerItem( BuildContext context, int index) {
    return RaisedButton(
      padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 16.0),
      color: Color(0xfff4f4f4),
      textColor: Color(0xff3a3535),
      child: Text(answerText[index],
          style: TextStyle(fontFamily: 'VT323', fontSize: 22)),
      onPressed: selectHandler,
    );
  }
}

【问题讨论】:

标签: flutter


【解决方案1】:

我设法通过在另一个级别添加 SingleChildScrollView 来找到启用滚动的解决方案:

 appBar: AppBar(
        backgroundColor: Color(0xfff4f4f4),
        brightness: Brightness.light,
        title: Text('...', style: TextStyle(
            fontFamily: 'VT323',
            fontSize: 30,
            color: Color(0xffff7315)),),
      ),
      body: SingleChildScrollView(

而不是尝试在此处添加 SingleChildScrollView:

return Container(
      width: double.infinity,
      margin: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 0),
      child: RaisedButton(
        padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 16.0),
        color: Color(0xfff4f4f4),
        textColor: Color(0xff3a3535),

这在我的应用程序的两个不同部分都非常有效。

【讨论】:

    【解决方案2】:

    尝试使用 ListView:

    Widget build(BuildContext context) {
    return Container(
      height: 100.0,
      width: double.infinity,
      margin: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 0),
      decoration: BoxDecoration(
        // color: Color.fromARGB(255, 238, 238, 238),
        boxShadow: [
          BoxShadow(
              offset: Offset(10, 10),
              color: Color.fromARGB(80, 0, 0, 0),
              blurRadius: 10),
          BoxShadow(
            offset: Offset(-10, -10),
            color: Color.fromARGB(150, 255, 255, 255),
            blurRadius: 10,
          ),
        ],
      ),
      child: ListView(
        children: <Widget>[
          RaisedButton(
            padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 16.0),
            color: Color(0xfff4f4f4),
            textColor: Color(0xff3a3535),
            child: Text(answerText,
                style: TextStyle(fontFamily: 'VT323', fontSize: 22)),
            onPressed: () {},
          ),
        ],
      ),
    );
    }
    

    带有 ListView.builder 的代码:

    List<String> answers = [
    "Answer1",
    "Answer2",
    "Answer3",
    "Answer4",
    "Answer5",
    "Answer6",
    "Answer7"
    ];
    
    @override
    Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: 60,
        margin: EdgeInsets.only(top: 25.0),
        width: double.infinity,
        color: Colors.green,
        child: ListView.builder(
          itemCount: answers.length,
          itemBuilder: (BuildContext context, int index) {
            return Container(
              margin: EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
              child: RaisedButton(
                padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 16.0),
                color: Color(0xfff4f4f4),
                textColor: Color(0xff3a3535),
                child: Text(
                  answers[index],
                  style: TextStyle(
                    fontFamily: 'VT323',
                    fontSize: 22,
                  ),
                ),
                onPressed: () {
                  print("Answer $index tapped");
                },
              ),
            );
          },
        ),
      ),
    );
    }
    

    【讨论】:

    • 非常感谢。我仍然在列表项较长的问题上遇到错误,所以我需要它来滚动。我该怎么做?
    • @SKdev 我无法理解你的问题,你能更准确一点吗?
    • 基本上我需要滚动文本答案列表。每个问题的答案数量因测验而异,当有 5 个或更多可能的答案时,会出现错误,因为没有滚动。
    • @SKdev 我认为您应该使用 ListView.builder 而不是简单的 ListView 并传递您的答案列表。
    • 这就是我目前正在尝试的,但我收到一个错误“类型'String'不是 List 类型的子类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多