【问题标题】:Using Stack to align Containers in FlutterFlutter 中使用 Stack 对齐容器
【发布时间】:2020-05-01 05:40:02
【问题描述】:

我希望搜索 (TextFormField) 栏看起来像这样,并且背景图像在容器中呈曲线状:



这是代码,我尝试将容器和搜索栏堆叠在一起,然后使用定位对齐搜索栏,但无法做到。

Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Container(
          color: Colors.white,
          child: Column(
            children: <Widget>[
              Stack(
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.only(bottom: 20.0),
                    alignment: Alignment.topCenter,
                    color:Colors.blueAccent,
                    height:250.0,
                    /*decoration: BoxDecoration(
                      borderRadius: BorderRadius.only(bottomLeft: Radius.circular(15.0))
                    ),*/
                    //child: Image.asset("assets/bgImage.jpg"),
                  ),
                  Positioned(
                    top: 155.0,
                    right: 0.0,
                    left: 0.0,
                    child: Container(
                      //color: Colors.white,
                      width: 400.0,             
                      padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 55.0),
                      child: TextField(
                        decoration: InputDecoration(
                          fillColor: Colors.white,
                          contentPadding: EdgeInsets.symmetric(vertical: 15.0),
                          enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.grey),
                            borderRadius: BorderRadius.all(Radius.circular(20.0)),
                          ),
                          focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.grey),
                            borderRadius: BorderRadius.all(Radius.circular(20.0)),
                          ),
                          hintText: 'Search',
                          hintStyle: TextStyle(
                            fontSize: 18.0
                          ),
                          prefixIcon: Icon(
                            Icons.search,
                            size: 30.0,
                          ),
                          /*suffixIcon: IconButton(
                            icon: Icon(
                              Icons.clear,
                            ),
                            onPressed: _clearSearch,
                          ),*/
                          filled: true,
                        ),
                        //onSubmitted :
                      ),
                    ),
                  ),
                ],
              ),
              Padding(
                padding: const EdgeInsets.only(left: 20.0,right: 20.0,),
                child: Card(
                  elevation: 6.0,
                  child: Padding(
                    padding: const EdgeInsets.all(15.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Column(
                            children: <Widget>[
                              CircleAvatar(
                                radius: 30.0,
                                backgroundColor: Colors.blue,
                              ),
                              SizedBox(height: 5.0,),
                              Text('Jaipur')
                            ],
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Column(
                            children: <Widget>[
                              CircleAvatar(
                                radius: 30.0,
                                backgroundColor: Colors.blue,
                              ),
                              SizedBox(height: 5.0,),
                              Text('Jaipur')
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ]
          ),
        ),
      ),

我试图将它们全部放在一个 Stack 中,但它们相互堆叠,我无法将它们一个接一个垂直排列。

【问题讨论】:

    标签: android flutter dart stack alignment


    【解决方案1】:

    我已对您的代码进行了更改,以实现您想要实现的目标。请看:

    SingleChildScrollView(
      child: Container(
        color: Colors.white,
        child: Column(
          children: <Widget>[
            Stack(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(bottom: 20.0),
                  alignment: Alignment.topCenter,
                  height:250.0,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.only(
                      bottomLeft: Radius.elliptical(30,8),
                      bottomRight: Radius.elliptical(30,8),
                    ),
                    color:Colors.blueAccent,
                  ),
                  //child: Image.asset("assets/bgImage.jpg"),
                ),
                Container(
                  //color: Colors.white,
                  width: 400.0,
                  padding: EdgeInsets.only(top: 223, left: 55, right: 55),
                  child: TextField(
                    decoration: InputDecoration(
                      fillColor: Colors.white,
                      contentPadding: EdgeInsets.symmetric(vertical: 15.0),
                      enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.grey),
                        borderRadius: BorderRadius.all(Radius.circular(20.0)),
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.grey),
                        borderRadius: BorderRadius.all(Radius.circular(20.0)),
                      ),
                      hintText: 'Search',
                      hintStyle: TextStyle(
                        fontSize: 18.0
                      ),
                      prefixIcon: Icon(
                        Icons.search,
                        size: 30.0,
                      ),
                      filled: true,
                    ),
                    //onSubmitted :
                  ),
                ),
              ],
            ),
            Padding(
              padding: const EdgeInsets.only(left: 20.0,right: 20.0, top: 20),
              child: Card(
                elevation: 6.0,
                child: Padding(
                  padding: const EdgeInsets.all(15.0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Column(
                          children: <Widget>[
                            CircleAvatar(
                              radius: 30.0,
                              backgroundColor: Colors.blue,
                            ),
                            SizedBox(height: 5.0,),
                            Text('Jaipur')
                          ],
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Column(
                          children: <Widget>[
                            CircleAvatar(
                              radius: 30.0,
                              backgroundColor: Colors.blue,
                            ),
                            SizedBox(height: 5.0,),
                            Text('Jaipur')
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ]
        ),
      ),
    );
    

    【讨论】:

    • 感谢您的解决方案。
    【解决方案2】:

    这应该适合你

    Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Container(
          color: Colors.white,
          child: Column(
            children: <Widget>[
              Stack(
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.only(bottom: 30.0),
                    alignment: Alignment.topCenter,
                    height:250.0,
                    decoration: BoxDecoration(
                      color:Colors.blueAccent,
                      borderRadius: BorderRadius.only(
                        bottomLeft: Radius.elliptical(25, 10),
                        bottomRight: Radius.elliptical(25, 10),
                      )
                    ),
                    //child: Image.asset("assets/bgImage.jpg"),
                  ),
                  Positioned(
                    top: 200.0,
                    right: 0.0,
                    left: 0.0,
                    child: Container(
                      //color: Colors.white,
                      width: 400.0,             
                      padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 55.0),
                      child: TextField(
                        decoration: InputDecoration(
                          fillColor: Colors.white,
                          contentPadding: EdgeInsets.symmetric(vertical: 15.0),
                          enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.grey),
                            borderRadius: BorderRadius.all(Radius.circular(20.0)),
                          ),
                          focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.grey),
                            borderRadius: BorderRadius.all(Radius.circular(20.0)),
                          ),
                          hintText: 'Search',
                          hintStyle: TextStyle(
                            fontSize: 18.0
                          ),
                          prefixIcon: Icon(
                            Icons.search,
                            size: 30.0,
                          ),
                          /*suffixIcon: IconButton(
                            icon: Icon(
                              Icons.clear,
                            ),
                            onPressed: _clearSearch,
                          ),*/
                          filled: true,
                        ),
                        //onSubmitted :
                      ),
                    ),
                  ),
                ],
              ),
              Padding(
                padding: EdgeInsets.only(
                  top: 10.0,
                  left: 20.0,
                  right: 20.0,
                  bottom: 20.0,
                ),
                child: Card(
                  elevation: 6.0,
                  child: Padding(
                    padding: const EdgeInsets.all(15.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Column(
                            children: <Widget>[
                              CircleAvatar(
                                radius: 30.0,
                                backgroundColor: Colors.blue,
                              ),
                              SizedBox(height: 5.0,),
                              Text('Jaipur')
                            ],
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Column(
                            children: <Widget>[
                              CircleAvatar(
                                radius: 30.0,
                                backgroundColor: Colors.blue,
                              ),
                              SizedBox(height: 5.0,),
                              Text('Jaipur')
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ]
          ),
        ),
      ),
    );
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-22
      • 1970-01-01
      • 2021-09-20
      • 2017-11-15
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 2020-07-28
      • 1970-01-01
      相关资源
      最近更新 更多