使容器内的 Stack 可滚动

Make Stack inside a Container scrollable

我目前正在尝试使我的 Container 的内容可滚动。 Container的child是一个Stack Widget,但是如果使用SingleChildScrollView作为Stack的parent,则会抛出异常。

Layout image available here

我遇到的问题有解决方案吗?

请检查以下最小可重现示例:


import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(title: const Text('Minimum reproducible code')),
        body: Card(
          child: ListTile(
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
            title: Row(children: [
              Expanded(
                flex: 1,
                child: Container(
                  decoration: BoxDecoration(border: Border.all(color: Colors.black38), borderRadius: BorderRadius.circular(10)),
                  height: 130,
                  width: 500,
                  child: SingleChildScrollView(
                    scrollDirection: Axis.horizontal,
                    child: Stack(children: const [
                      Positioned(
                        top: 16,
                        child: Text(
                          'First line: My first line is to long and needs to be scrollable',
                        ),
                      ),
                      Positioned(
                        top: 42,
                        child: Text(
                          'Second line: Short Text',
                        ),
                      ),
                    ]),
                  ),
                ),
              ),
              const SizedBox(
                width: 20,
              ),
              Expanded(
                flex: 1,
                child: Container(
                  decoration: BoxDecoration(border: Border.all(color: Colors.black38), borderRadius: BorderRadius.circular(10)),
                  height: 130,
                  width: 200,
                ),
              ),
            ]),
          ),
        ),
      ),
    );
  }
}

提前致谢!

这是一个可能的解决方案。只需将代码中的 Scaffold 小部件替换为我在下面分享的 Scaffold 小部件即可。

Scaffold(
        appBar: AppBar(title: const Text('Minimum reproducible code')),
        body: Card(
          child: ListTile(
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
            title: Row(mainAxisSize: MainAxisSize.min, children: [
                  Container(
                  decoration: BoxDecoration(border: Border.all(color: Colors.black38), borderRadius: BorderRadius.circular(10)),
                  height: 130,
                  width: 200,
                  child: SingleChildScrollView(
                    scrollDirection: Axis.horizontal,
                    child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: const [
                      Text(" First Line: A text that is tooo long to fit in the container"),
                      Text(" Second Line: short text")
                    ]),
                  ),
                ),
              const SizedBox(
                width: 20,
              ),
                Container(
                  decoration: BoxDecoration(border: Border.all(color: Colors.black38), borderRadius: BorderRadius.circular(10)),
                  height: 130,
                  width: 200,
                ),
            ]),
          ),
        ),
      )