【问题标题】:how to place image on border of card in flutter?如何将图像放置在卡片的边框上?
【发布时间】:2020-04-23 09:48:14
【问题描述】:

我是 Flutter 的新手,我正在分享一张图片,其中有卡片,并且它的边框有一张图片(如您所见,图片中显示了日期,即 2020 年 7 月)。我不知道如何实现这个功能。请帮我。 我编写了以下代码来创建卡片。该代码显示卡内的日期图像。我是否需要关注其他小部件而不是卡片和列表?

BoxDecoration myBoxDecoration() {
  return BoxDecoration(
    color: Colors.grey[100],
    border: Border.all(
      width: 1, //
      //                <--- border width here
    ),
  );
}
Widget _myListView(BuildContext context) {
return new ListView.separated(
  padding: const EdgeInsets.all(8),
  itemCount: 1,
  itemBuilder: (BuildContext context, int index) {
    return  Padding(
        padding: const EdgeInsets.all(0.0),
        child:
        Column(
            children: <Widget>[
        Container(
            decoration:  myBoxDecoration(),
            height: 180,


            child :
            Card(
                    child: Ink(
                      color: Colors.grey[200],
                      child : ListTile(
                        onTap: () {
                        },
                        title: Column(
                            children: <Widget>[
                              Row(
                               children: <Widget>[
                              Container(
                                child:

                                Center(child: Text('JULY , 2020' ,  style: TextStyle(
                                    fontWeight: FontWeight.bold ,
                                    fontSize: 20,
                                    color: Colors.white
                                ),),),
                                width: 190.0,
                                height: 30,
                                decoration: BoxDecoration(
                                  image: DecorationImage(
                                    image: AssetImage("assets/images/apply_leave.png"),
                                    fit: BoxFit.fill,
                                    //  alignment: Alignment.center,
                                  ),
                                ),
                              ),
                              Container(
                                 child:Text('' ,  style: TextStyle(
                                     fontWeight: FontWeight.bold ,
                                     fontSize: 20,
                                     color: Colors.black
                                 ),)
                              )


                              ]
                              ),
                              SizedBox(height: 20.0),

                             Expanded(
                               child :
                               Row(
                                   children: <Widget>[
                                Text('FEE SCEDULE' , style: TextStyle(
                                 fontWeight: FontWeight.bold ,
                                 color: Colors.black,


                                   )),
                                     SizedBox(width: 80.0),
                                     Text('JULY-SEPT' , style: TextStyle(
                                       fontWeight: FontWeight.bold ,
                                       color: Colors.black,


                                     ))

                                   ])
                             ),
                              Expanded(
                                  child :
                                  Row(
                                      children: <Widget>[
                                        Text('DUE DATE' , style: TextStyle(
                                          fontWeight: FontWeight.bold ,
                                          color: Colors.black,


                                        )),
                                        SizedBox(width: 105.0),
                                        Text('10-06-2020' , style: TextStyle(
                                          fontWeight: FontWeight.bold ,
                                          color: Colors.black,


                                        ))

                                      ])
                              ),
                              Expanded(
                                  child :
                                  Row(
                                      children: <Widget>[
                                        Text('END DATE' , style: TextStyle(
                                          fontWeight: FontWeight.bold ,
                                          color: Colors.black,


                                        )),
                                        SizedBox(width: 105.0),
                                        Text('19-07-2020' , style: TextStyle(
                                          fontWeight: FontWeight.bold ,
                                          color: Colors.black,


                                        ))

                                      ])
                              )

                            ]




                      ),
                    ),
                  ),
                )


        ),
              Container(
                child:  Card(
                  color:  Colors.black,

                  child: Padding(
                    padding: const EdgeInsets.all(14.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,


                      children: <Widget>[
                        Text('Total Amount:',style: TextStyle(fontWeight: FontWeight.bold , color: Colors.white),),
                        Text('254684'+'/-',style: TextStyle(fontWeight: FontWeight.bold , color: Colors.white),),
                      ],
                    ),
                  ),
                  //
                ),

              )
    ]
        )
    );
  },
  separatorBuilder: (BuildContext context,
      int index) => const Divider(),
);

}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    如果我理解正确,您想在带有边框的容器上方放置一个图像。您可以使用Stack。

    将该容器包装在其中并将其放在子列表的开头,这样它将显示在图像下方,以及其他所有内容。使用 Positioned 重新排列堆栈中的小部件。您可能希望将堆栈包装在 Container 中以更好地定位它。

    【讨论】:

      【解决方案2】:

      随意使用值以获得最理想的结果:

      BoxDecoration myBoxDecoration() {
        return BoxDecoration(
          color: Colors.grey[100],
          border: Border.all(
            width: 1, //
            //                <--- border width here
          ),
        );
      }
      
      Widget _myListView(BuildContext context) {
        return Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: <Widget>[
              Container(
                decoration: myBoxDecoration(),
                height: 180,
                child: Stack(
                  children: <Widget>[
                    Align(
                      alignment: Alignment.center,
                      child: Card(
                        child: Ink(
                          color: Colors.green[200],
                          child:  Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Column(
                              children: <Widget>[
                                Row(
                                  children: <Widget>[
                                    Container(
                                      child: Text(
                                        '',
                                        style: TextStyle(
                                            fontWeight: FontWeight.bold,
                                            fontSize: 20,
                                            color: Colors.black),
                                      ),
                                    ),
                                  ],
                                ),
                                SizedBox(height: 20.0),
                                Expanded(
                                    child: Row(children: <Widget>[
                                      Text('FEE SCEDULE',
                                          style: TextStyle(
                                            fontWeight: FontWeight.bold,
                                            color: Colors.black,
                                          )),
                                      SizedBox(width: 80.0),
                                      Text('JULY-SEPT',
                                          style: TextStyle(
                                            fontWeight: FontWeight.bold,
                                            color: Colors.black,
                                          ))
                                    ])),
                                Expanded(
                                  child: Row(
                                    children: <Widget>[
                                      Text('DUE DATE',
                                          style: TextStyle(
                                            fontWeight: FontWeight.bold,
                                            color: Colors.black,
                                          )),
                                      SizedBox(width: 105.0),
                                      Text('10-06-2020',
                                          style: TextStyle(
                                            fontWeight: FontWeight.bold,
                                            color: Colors.black,
                                          ))
                                    ],
                                  ),
                                ),
                                Expanded(
                                  child: Row(
                                    children: <Widget>[
                                      Text('END DATE',
                                          style: TextStyle(
                                            fontWeight: FontWeight.bold,
                                            color: Colors.black,
                                          )),
                                      SizedBox(width: 105.0),
                                      Text(
                                        '19-07-2020',
                                        style: TextStyle(
                                          fontWeight: FontWeight.bold,
                                          color: Colors.black,
                                        ),
                                      )
                                    ],
                                  ),
                                )
                              ],
                            ),
                          ),
                        ),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(3.0),
                      child: Align(
                        alignment: Alignment.topLeft,
                        child: _buildBorderImage(),
                      ),
                    )
                  ],
                ),
              ),
              Container(
                child: Card(
                  color: Colors.black,
      
                  child: Padding(
                    padding: const EdgeInsets.all(14.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          'Total Amount:',
                          style: TextStyle(
                              fontWeight: FontWeight.bold, color: Colors.white),
                        ),
                        Text(
                          '254684' + '/-',
                          style: TextStyle(
                              fontWeight: FontWeight.bold, color: Colors.white),
                        ),
                      ],
                    ),
                  ),
                  //
                ),
              ),
            ],
          ),
        );
      }
      
      Container _buildBorderImage() {
        return Container(
          child: Center(
            child: Text(
              'JULY , 2020',
              style: TextStyle(
                  fontWeight: FontWeight.bold, fontSize: 20, color: Colors.white),
            ),
          ),
          width: 190.0,
          height: 30,
          decoration: BoxDecoration(
            color: Colors.green,
      //      image: DecorationImage(
      //        image: AssetImage("assets/images/apply_leave.png"),
      //        fit: BoxFit.fill,
              //  alignment: Alignment.center,
      //      ),
          ),
        );
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-03
        • 2023-04-02
        • 1970-01-01
        • 2012-02-06
        • 1970-01-01
        相关资源
        最近更新 更多