admin管理员组文章数量:1296351
I want to create the following L shaped design : -
This can be created using simple Column , Row and Expanded Widgets but I am not able to think it through. Design must be responsive as the Size of the Square changes. I thought about using Container Widget and its left and bottom Border in order to make the L shape but the issue with that is Container will be nested and hence might have to use stack but Expanded Widget wont work properly inside it.
I want to create the following L shaped design : -
This can be created using simple Column , Row and Expanded Widgets but I am not able to think it through. Design must be responsive as the Size of the Square changes. I thought about using Container Widget and its left and bottom Border in order to make the L shape but the issue with that is Container will be nested and hence might have to use stack but Expanded Widget wont work properly inside it.
Share Improve this question edited Feb 15 at 4:15 Ramji asked Feb 12 at 9:38 RamjiRamji 1622 gold badges7 silver badges26 bronze badges 5 |2 Answers
Reset to default 1 +50Following is the minimum required code for the given design:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('LShaped Design'),
backgroundColor: Colors.blue,
),
body: SizedBox(
height: double.maxFinite,
width: double.maxFinite,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Stack(
children: <Widget>[
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
color: Colors.white60,
border: Border.all(color: Colors.black, style: BorderStyle.solid, width: 2)
),
),
_lShapeContainer(lPos: 0, side: 100),
_lShapeContainer(lPos: 10, side: 90),
_lShapeContainer(lPos: 20, side: 80),
_lShapeContainer(lPos: 30, side: 70),
_lShapeContainer(lPos: 40, side: 60),
_lShapeContainer(lPos: 50, side: 50),
_lShapeContainer(lPos: 60, side: 40),
],
),
],
),
),
),
);}
Widget _lShapeContainer({required double lPos, required double side}) {
return Positioned(
left: lPos,
child: Container(
width: side - 15,
height: side,
decoration: BoxDecoration(
color: Colors.white60,
border: Border.all(color: Colors.black, style: BorderStyle.solid, width: 2)
),
),
);}}
The output:
Updated: Responsive design
Following is code where you can have a responsive shape. You need to adjust the height, width and multiplier according to your needs.
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
final double height = 100;
final double width = 100;
final double multiplier = 10
return Scaffold(
appBar: AppBar(
title: const Text('LShaped Design'),
backgroundColor: Colors.blue,
),
body: SizedBox(
height: double.maxFinite,
width: double.maxFinite,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: height,
width: width + multiplier,
color: Colors.red,
child: AspectRatio(
aspectRatio: 1/2,
child: Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Colors.white60,
border: Border.all(color: Colors.black, style: BorderStyle.solid, width: 2)
),
),
_lShapeContainer(lPos: 0, side: height, color: Colors.blue),
_lShapeContainer(lPos: (multiplier*1), side: height - (multiplier*1), color: Colors.white24),
_lShapeContainer(lPos: (multiplier*2), side: height - (multiplier*2), color: Colors.black26),
_lShapeContainer(lPos: (multiplier*3), side: height - (multiplier*3), color: Colors.green),
_lShapeContainer(lPos: (multiplier*4), side: height - (multiplier*4), color: Colors.amberAccent),
_lShapeContainer(lPos: (multiplier*5), side: height - (multiplier*5), color: Colors.redAccent),
_lShapeContainer(lPos: (multiplier*6), side: height - (multiplier*6), color: Colors.brown),
],
),
),
),
],
),
),
);}
Widget _lShapeContainer({required double lPos, required double side, required Color color}) {
return Positioned(
left: lPos,
child: Container(
width: side - 15,
height: side,
decoration:
BoxDecoration(color: color, border: Border.all(color: Colors.black, style: BorderStyle.solid, width: 2)),
),
);}}
- Make the squares bigger:
final double height = 200;
final double width = 200;
final double multiplier = 20;
- Change the right side vertical container width according to your need:
width: width + multiplier,
OR
width: width,
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: Scaffold(
body: Center(
child: LShapedWidget(),
),
),
));
}
class LShapedWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Vertical part of the "L"
Container(
width: 50,
height: 150,
color: Colors.blue,
),
Row(
children: [
// Base of the "L"
Container(
width: 150,
height: 50,
color: Colors.blue,
),
// Nested L inside the bigger L
Column(
children: [
Container(
width: 50,
height: 100,
color: Colors.red,
),
Row(
children: [
Container(
width: 100,
height: 50,
color: Colors.red,
)
],
)
],
)
],
)
],
),
);
}
}
本文标签: How to create Nested L shaped Widget in FlutterStack Overflow
版权声明:本文标题:How to create Nested L shaped Widget in Flutter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741611226a2388270.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
CustomPainter
class. – Reza Farjam Commented Feb 12 at 10:44CustomPainter
, which gives you the size of the canvas, and is probably also more efficient, you might be interested in theAspectRatio
widget. – JaffaKetchup Commented Feb 15 at 13:57