如何在Flutter的手势控件中滚动文本

o2g1uqev  于 2023-02-13  发布在  Flutter
关注(0)|答案(1)|浏览(182)

我有一个带有一些小部件的Flutter应用程序。
我正在使用GestureDetector小工具进行滑动,我有很多小工具在堆栈中。有时我会在其他小工具的顶部显示一些文本,有时文本太大而无法容纳。所以我想添加滚动作为一个自定义。
Positioned小部件打破了滚动。如果我删除小部件,甚至删除“顶部/左侧/右侧”选项,它工作正常!?
下面是一个可重现的示例。

import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(home: StackOverflowExample()));
}

class StackOverflowExample extends StatelessWidget {
  const StackOverflowExample({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        // ignore: avoid_print
        onTap: () {
          print('tap');

        },
        child: Stack(
          children: [
            Center(
              child: Container(
                width: 400,
                height: 400,
                color: Colors.red,
              ),
            ),
            const Positioned(
              top: 50,
              left: 0,
              right: 0,
              child: SingleChildScrollView(
                scrollDirection: Axis.vertical,
                child: Text(
                  'Lot and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots and lots ',
                  style: TextStyle(fontSize: 50, fontWeight: FontWeight.bold),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
k97glaaz

k97glaaz1#

我建议把你的SingleChildScrollView Package 成SizedBox
而不是将Text小工具的maxLines固定为2。

const Center(
  child: SizedBox(
    height: 150,
    child: SingleChildScrollView(
      padding: EdgeInsets.all(16.0),
      scrollDirection: Axis.vertical,
      child: Text(
        '3 lines of text\nsecond line\n3rd invisible line',
        // maxLines: 2,
        ...
      ),
    ),
  ),
),

相关问题