Flutter 我想把容器的一部分放在另一个上面

Flutter 我想把容器的一部分放在另一个上面,flutter,flutter-layout,Flutter,Flutter Layout,我需要实现两个按钮。需要部分放置在另一个按钮上。 因此,当按钮更改后,内容也会更改。 我被这些按钮卡住了。救救我 以下是我迄今为止所做的工作 Container( child: Row( mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[ Container( height: 50,

我需要实现两个按钮。需要部分放置在另一个按钮上。 因此,当按钮更改后,内容也会更改。 我被这些按钮卡住了。救救我

以下是我迄今为止所做的工作

 Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: <Widget>[
            Container(
              height: 50,
              width: 75,
              decoration: BoxDecoration(
                  gradient: LinearGradient(
                    colors: [
                      Color.fromRGBO(250, 250, 250, 1),
                      Color.fromRGBO(250, 250, 250, 1)
                    ],
                  ),
                  borderRadius: BorderRadius.all(Radius.circular(50)),
                  boxShadow: [
                    BoxShadow(
                        color: Colors.grey,
                        blurRadius: 20.0,
                        // spreadRadius: 5.0,
                        offset: Offset(0, 10.0))
                  ]),
              child: Center(
                child: Text(
                  'Posts',
                  style: TextStyle(
                      color: Colors.black, fontWeight: FontWeight.bold,fontFamily: 'Montserrat'),
                ),
              ),
            ),
            Container(
              height: 50,
              width: 95,
              decoration: BoxDecoration(
                  gradient: LinearGradient(
                    colors: [Color(0xFFf45d27), Color(0xFFf5851f)],
                  ),
                  borderRadius: BorderRadius.all(Radius.circular(50))),
              child: Center(
                child: Text(
                  'Events',
                  style: TextStyle(
                      color: Colors.white, fontFamily: 'Montserrat',fontWeight: FontWeight.bold),
                ),
              ),
            )
          ],
        ),
      ),
容器(
孩子:排(
mainAxisAlignment:mainAxisAlignment.end,
儿童:[
容器(
身高:50,
宽度:75,
装饰:盒子装饰(
梯度:线性梯度(
颜色:[
颜色。来自RGBO(2502502501),
颜色。来自RGBO(2502502501)
],
),
borderRadius:borderRadius.all(半径.圆形(50)),
boxShadow:[
箱形阴影(
颜色:颜色。灰色,
半径:20.0,
//扩展半径:5.0,
偏移量:偏移量(0,10.0))
]),
儿童:中心(
子:文本(
"职位",,
样式:TextStyle(
颜色:color.black,fontWeight:fontWeight.bold,fontFamily:“蒙特塞拉特”),
),
),
),
容器(
身高:50,
宽度:95,
装饰:盒子装饰(
梯度:线性梯度(
颜色:[颜色(0xFFf45d27),颜色(0xFFf5851f)],
),
borderRadius:borderRadius.all(半径.圆形(50)),
儿童:中心(
子:文本(
“事件”,
样式:TextStyle(
颜色:Colors.white,fontFamily:“蒙特塞拉特”,fontWeight:fontWeight.bold),
),
),
)
],
),
),
我希望输出如下。


您可以使用堆栈

像这样的

Center(
        child: Container(
          height:30,
          width:50,
          child:Stack(
          alignment:Alignment.centerLeft,
        children:<Widget>[
        //the bottom widget
          Align(alignment:Alignment.centerRight,child: 
          Container(width:50,color:Colors.red),),
          //the top widget
           Align(alignment:Alignment.centerLeft,child: Container(width:30,color:Colors.black.withOpacity(.5)),),
        ]),),
      ),
中心(
子:容器(
身高:30,
宽度:50,
子:堆栈(
对齐:alignment.centerLeft,
儿童:[
//底部小部件
对齐(对齐:对齐。中间右侧,子项:
容器(宽度:50,颜色:Colors.red),),
//顶部小部件
对齐(对齐:对齐.中间偏左,子:容器(宽度:30,颜色:Colors.black.withOpacity(.5)),),
]),),
),

也许你可以这样试试?复制粘贴到以查看结果

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FirstPage(),
    );
  }
}

class FirstPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("First Page"),
      ),
      body: Center(
        child: Container(
          height: 50,
          width: 200,
          decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [
                  Color.fromRGBO(250, 250, 250, 1),
                  Color.fromRGBO(250, 250, 250, 1)
                ],
              ),
              borderRadius: BorderRadius.all(Radius.circular(50)),
              boxShadow: [
                BoxShadow(
                    color: Colors.grey,
                    blurRadius: 20.0,
                    // spreadRadius: 5.0,
                    offset: Offset(0, 10.0))
              ]),
          child: Stack(
            //change this to move position
            alignment: Alignment.center,
            children: <Widget>[
              Container(),
              Row(
                //change this to move position
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  //right container
                  Container(
                    height: 50,
                    width: 100,
                    decoration: BoxDecoration(
                        gradient: LinearGradient(
                          colors: [Color(0xFFf45d27), Color(0xFFf5851f)],
                        ),
                        borderRadius: BorderRadius.all(Radius.circular(50))),
                    child: Center(
                      child: Text(
                        'Events',
                        style: TextStyle(
                            color: Colors.white,
                            fontFamily: 'Montserrat',
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                  ),
                  //left container
                  Container(
                    height: 50,
                    width: 100,
                    child: Center(
                      child: Text(
                        'Events',
                        style: TextStyle(
                            color: Colors.black,
                            fontFamily: 'Montserrat',
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                  )
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
主页:首页(),
);
}
}
类FirstPage扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:正文(“第一页”),
),
正文:中(
子:容器(
身高:50,
宽度:200,
装饰:盒子装饰(
梯度:线性梯度(
颜色:[
颜色。来自RGBO(2502502501),
颜色。来自RGBO(2502502501)
],
),
borderRadius:borderRadius.all(半径.圆形(50)),
boxShadow:[
箱形阴影(
颜色:颜色。灰色,
半径:20.0,
//扩展半径:5.0,
偏移量:偏移量(0,10.0))
]),
子:堆栈(
//将此更改为移动位置
对齐:对齐.center,
儿童:[
容器(),
划船(
//将此更改为移动位置
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
//右容器
容器(
身高:50,
宽度:100,
装饰:盒子装饰(
梯度:线性梯度(
颜色:[颜色(0xFFf45d27),颜色(0xFFf5851f)],
),
borderRadius:borderRadius.all(半径.圆形(50)),
儿童:中心(
子:文本(
“事件”,
样式:TextStyle(
颜色:颜色,白色,
fontFamily:“蒙特塞拉特”,
fontWeight:fontWeight.bold),
),
),
),
//左容器
容器(
身高:50,
宽度:100,
儿童:中心(
子:文本(
“事件”,
样式:TextStyle(
颜色:颜色,黑色,
fontFamily:“蒙特塞拉特”,
fontWeight:fontWeight.bold),
),
),
)
],
),
],
),
),
),
);
}
}
但来自中国的答案也会起作用

int viewChoice = 0; /*This is the selection of button to maintain design of button*/

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: const Text(
            'Toolbar Title',
            style: TextStyle(
                color: Colors.black,
                fontWeight: FontWeight.bold /*fontSize,etc*/),
          ),
          actions: [
            IconButton(
                icon: Icon(Icons.account_circle),
                onPressed: () {
                  //Todo when pressed
                }),
          ]),
      body: Container(
        padding:EdgeInsets.only(top:20),
        child: Container(
          height:50.0,
                  margin: EdgeInsets.only(left: 25.0, right: 25.0),
                  alignment: Alignment.topCenter,
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(14.0),
                      border: Border.all(color: Colors.black, width: 1.0)),
                  child: Row(mainAxisSize: MainAxisSize.min, children: [
                    Expanded(
                      child: Container(
                        decoration: BoxDecoration(
                          gradient: viewChoice == 0
                              ? LinearGradient(
                                  colors: [Colors.blueAccent, Colors.blue],
                                )
                              : null,
                          borderRadius: BorderRadius.circular(13.0),
                          border: viewChoice == 0
                              ? Border.all(color: Colors.black, width: 1.0)
                              : null,
                        ),
                        child: FlatButton(
                          color: Colors.transparent,
                          onPressed: () {
                            setState(() {
                              viewChoice = 0;
                            });
                          },
                          child: Text(
                            'Button 1',
                            /*style as your requirement*/
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      child: Container(
                        decoration: BoxDecoration(
                          gradient: viewChoice == 1
                              ? LinearGradient(
                                  colors: [Colors.blueAccent, Colors.blue],
                                )
                              : null,
                          borderRadius: BorderRadius.circular(13.0),
                          border: viewChoice == 1
                              ? Border.all(color: Colors.black, width: 1.0)
                              : null,
                        ),
                        child: FlatButton(
                          onPressed: () {
                            setState(() {
                              viewChoice = 1;
                            });
                          },
                          child: Text(
                            'Button 2',
                            /*style as your requirement*/
                          ),
                        ),
                      ),
                    ),
                  ]),
                ),
        )
    );
  }