【问题标题】:Flutter - How 2 Give different width to different container enclosed in a single column?Flutter - 如何为单列中的不同容器赋予不同的宽度?
【发布时间】:2021-12-04 08:15:32
【问题描述】:

我正在尝试从 dribble 开发一个 App 创意的克隆,可以在 here 找到。

你能告诉我我要写的代码是否正确吗? 当我尝试使用包含在容器中的两个文本小部件(单独)包装一列时,当我尝试更改一个容器的宽度时,它也会更改另一个容器的边距。

我的代码是:-

class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            Container(
              margin: const EdgeInsets.only(top: 30, left: 20),
              child: const Text(
                'Find Intrested Events to Join',
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 30.0,
                  wordSpacing: 2,
                ),
              ),
              width: 200,
            ),
            // -----------------------------------------------------------------
            Container(
              margin: EdgeInsets.only(left: 20, top: 20),
              width: 220,
              child: Text(
                'We share all events like Charity, workshop, Blood drive, etc.',
                style: TextStyle(wordSpacing: 2, height: 1.4),
              ),
            )
          ],
        ),
      ),
    );
  

你能告诉我如何达到那个阶段,我可以改变容器的宽度而不影响其他人的边距。

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    在这种情况下最好使用Stack,因为它包含背景图像,还可以从flutter.dev了解更多关于StackAlignPositionedLayoutBuilder的信息

    玩这个小部件,

    
    class HomeScreen extends StatelessWidget {
      const HomeScreen({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SafeArea(child: LayoutBuilder(
            builder: (context, constraints) {
              return Stack(
                children: [
                  Container(
                    color: Colors.amber.withOpacity(.4),
                  ), //background image
                  Positioned(
                    left: 20,
                    top: 30,
                    child: SizedBox(
                      width: constraints.maxWidth * .5, // 50% of width
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          const Text(
                            'Find Intrested Events to Join',
                            style: TextStyle(
                              fontWeight: FontWeight.bold,
                              fontSize: 30.0,
                              wordSpacing: 2,
                            ),
                          ),
    
                          SizedBox(
                            height: 20,
                          ), //spaces between text
                          Text(
                            'We share all events like Charity, workshop, Blood drive, etc.',
                            style: TextStyle(wordSpacing: 2, height: 1.4),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Align(
                    // also Postisioned can be use
                    alignment: Alignment.bottomCenter,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Text("hereh"),
                        ElevatedButton(onPressed: () {}, child: Text("Button"))
                      ],
                    ),
                  )
                ],
              );
            },
          )),
        );
      }
    }
    
    

    【讨论】:

    • 谢谢我也会好好学习一下,因为我刚刚完成了一个教程,并第一次开始自己开发谢谢你的建议顺便说一句,你太快了,我没有没想到你会为我的应用程序提供完整的课程(这与我选择的课程非常接近)
    猜你喜欢
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多