【问题标题】:Overflow visible in Expanded widget扩展小部件中可见溢出
【发布时间】:2021-03-30 19:48:32
【问题描述】:

我的布局是一行,包含三个展开的小部件。

我需要在其中一个展开的小部件中显示溢出

这是代码

Row(
  children: [
    Expanded(
      flex: 1,
      child: Container(color: Colors.red,)
    ),
    Expanded(
      flex: 2,
      child: Container(color: Colors.green,)
    ),
    Expanded(
      flex: 3,
      child: Container(color: Colors.orange,)
    ),
  ],
),

现在我需要在展开的小部件中添加这样的圆圈,它会溢出展开的小部件。我无法将 Expanded 包装在溢出框中,所以我迷路了

感谢任何帮助。

编辑:我在第三个展开时试过这个,只是想看看它是否会溢出,但它只会溢出 SizedOverflowBox,不会溢出 Expanded

Expanded(
  flex: 3,
  child: SizedOverflowBox(
    size: const Size(100.0, 100.0),
    alignment: AlignmentDirectional.bottomCenter,
    child: Container(height: 50.0, width: 1500.0, color: Colors.blue,),
  ),
),

看来是不可能了

【问题讨论】:

  • 溢出到底是什么意思?您只希望点沿边缘定位吗?
  • 只需颠倒包装顺序并将溢出框包装在扩展小部件中
  • @ScottGodfrey 里面有一个用圆圈展开的标题,就像溢出容器的子弹一样。它们是要点
  • @dm_tr 我该如何定位它?
  • @dm_tr 我认为这会溢出框,但不会溢出

标签: flutter flutter-layout flutter-web


【解决方案1】:

可以通过 Stack 轻松实现。请看下面的代码:

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Row(
          children: [
            Expanded(
                flex: 1,
                child: Container(
                  color: Colors.red,
                )),
            Expanded(
                flex: 2,
                child: Container(
                  color: Colors.green,
                )),
            Expanded(
                flex: 3,
                child: Container(
                  color: Colors.orange,
                )),
          ],
        ),
        Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Container(
                height: 25,
                width: 25,
                decoration:
                    BoxDecoration(color: Colors.black, shape: BoxShape.circle),
                child: Center(
                  child: Container(
                    height: 15,
                    width: 15,
                    decoration: BoxDecoration(
                        color: Colors.white, shape: BoxShape.circle),
                  ),
                ),
              ),
              Container(
                height: 25,
                width: 25,
                decoration:
                    BoxDecoration(color: Colors.black, shape: BoxShape.circle),
                child: Center(
                  child: Container(
                    height: 15,
                    width: 15,
                    decoration: BoxDecoration(
                        color: Colors.white, shape: BoxShape.circle),
                  ),
                ),
              ),
              Container(
                height: 25,
                width: 25,
                decoration:
                    BoxDecoration(color: Colors.black, shape: BoxShape.circle),
                child: Center(
                  child: Container(
                    height: 15,
                    width: 15,
                    decoration: BoxDecoration(
                        color: Colors.white, shape: BoxShape.circle),
                  ),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}

请查看文本项目符号点溢出的代码。

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(),
        body: MyWidget(),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          flex: 1,
          child: Container(
            color: Colors.red.withAlpha(60),
            child: OverflowBox(
              alignment: Alignment.topLeft,
              maxWidth: 400,
              child: SizedBox(
                width: 300,
                child: Text(
                  'HelloHello ?',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ),
        ),
        Expanded(
          flex: 2,
          child: Container(
            color: Colors.green.withAlpha(60),
            child: OverflowBox(
              alignment: Alignment.topLeft,
              maxWidth: 400,
              child: SizedBox(
                width: 300,
                child: Container(
                  child: Text(
                    '\n\nHelloHello HelloHello ?\n\nHelloHello HelloHello ?\n\nHelloHello HelloHello ?\n\nHelloHello HelloHello ?',
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ),
            ),
          ),
        ),
        Expanded(
          flex: 3,
          child: Container(
            color: Colors.purple.withAlpha(60),
            child: OverflowBox(
              alignment: Alignment.topLeft,
              maxWidth: 400,
              child: SizedBox(
                width: 300,
                child: Text(
                  '\n\n\n\n\n\n\n\n\n\n\n\Hello Hello Hello Hello Hello Hello ',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}

【讨论】:

  • 是的,但圆圈的确切位置取决于扩展小部件内的动态内容,如果我这样做,位置将与扩展中的内容无关,对吧?
  • 使用此代码,圆圈将始终位于屏幕中心。但是使用 Stack 和 Positioned 小部件,您可以将圆圈放置在您喜欢的任何位置。圆圈是最后添加的,因此它们将始终是所有其他小部件的on-top,并且始终可见。
  • 谢谢,但这不是我需要的,圆圈是文本中的要点,我必须计算文本的确切位置才能添加它们。我将把它用作最后的资源。
  • 我的意思是垂直位置
  • 我还没有完全理解你想要做什么,但是从我能理解的一点点我已经添加了代码来溢出文本。为了使溢出的文本可见,容器颜色需要具有一定程度的透明度。
猜你喜欢
  • 2021-10-15
  • 1970-01-01
  • 2019-02-11
  • 1970-01-01
  • 2021-11-28
  • 2020-05-08
  • 1970-01-01
  • 2020-09-11
  • 1970-01-01
相关资源
最近更新 更多