Equivalent of RelativeLayout in Flutter

前端 未结 4 1881
后悔当初
后悔当初 2021-01-29 18:00

Is there a way to implement something similar to what RelativeLayout does on Android?

In particular I\'m looking for something similar to centerInPare

4条回答
  •  心在旅途
    2021-01-29 18:04

    Here is another example to show how Stack along with Positioned can be used to make it work like RelativeLayout.

    double _containerHeight = 120, _imageHeight = 80, _iconTop = 44, _iconLeft = 12, _marginLeft = 110;
    
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        backgroundColor: Colors.white,
        body: Stack(
          children: [
            Positioned(
              left: 0,
              right: 0,
              height: _containerHeight,
              child: Container(color: Colors.blue),
            ),
            Positioned(
              left: _iconLeft,
              top: _iconTop,
              child: Icon(Icons.settings, color: Colors.white),
            ),
            Positioned(
              right: _iconLeft,
              top: _iconTop,
              child: Icon(Icons.bubble_chart, color: Colors.white),
            ),
            Positioned(
              left: _iconLeft,
              top: _containerHeight - _imageHeight / 2,
              child: ClipOval(child: Image.asset("assets/images/profile.jpg", fit: BoxFit.cover, height: _imageHeight, width: _imageHeight)),
            ),
            Positioned(
              left: _marginLeft,
              top: _containerHeight - (_imageHeight / 2.5),
              child: Text("CopsOnRoad", style: TextStyle(color: Colors.white, fontWeight: FontWeight.w500, fontSize: 18)),
            ),
            Positioned.fill(
              left: _marginLeft,
              top: _containerHeight + (_imageHeight / 4),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    children: [
                      Text("2", style: TextStyle(fontWeight: FontWeight.bold)),
                      Text("Gold", style: TextStyle(color: Colors.grey)),
                    ],
                  ),
                  Column(
                    children: [
                      Text("22", style: TextStyle(fontWeight: FontWeight.bold)),
                      Text("Silver", style: TextStyle(color: Colors.grey)),
                    ],
                  ),
                  Column(
                    children: [
                      Text("28", style: TextStyle(fontWeight: FontWeight.bold)),
                      Text("Bronze", style: TextStyle(color: Colors.grey)),
                    ],
                  ),
                  Container(),
                ],
              ),
            ),
          ],
        ),
      );
    }
    

提交回复
热议问题