[英]How to achieve a design like below in flutter?
Try this template code 试试这个模板代码
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
darkTheme: ThemeData.dark(),
title: 'Widget of the weeks',
home: Scaffold(
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(10.0, 10.0, 5, 0),
child: Text("FILTER",
style: TextStyle(
color: Colors.white30, fontWeight: FontWeight.bold)),
),
Container(
constraints: BoxConstraints(minHeight: 100, maxHeight: 100),
child: SingleChildScrollView(
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
verticalDirection: VerticalDirection.down,
runSpacing: 3.0,
spacing: 3.0,
children: <Widget>[
ChipDesign("Lifetime"),
ChipDesign("Student"),
ChipDesign("Salaried"),
ChipDesign("Corporate"),
ChipDesign("Open1"),
ChipDesign("Open2"),
ChipDesign("Open2"),
ChipDesign("Open4"),
ChipDesign("Open5"),
ChipDesign("Open6"),
ChipDesign("Open7"),
ChipDesign("Open9"),
ChipDesign("Open10"),
ChipDesign("Open11"),
ChipDesign("Open12"),
ChipDesign("My Referral Code Users"),
ChipDesign("+10"),
],
),
),
),
],
),
),
),
);
}
}
class ChipDesign extends StatelessWidget{
final String _label;
ChipDesign(this._label);
@override
Widget build(BuildContext context){
return Container(
child: Chip(
label: Text(
_label,
style: TextStyle(color: Colors.white),
),
backgroundColor: Colors.red,
padding: EdgeInsets.fromLTRB(5.0, 0.0, 5.0, 0.0),
),
margin: EdgeInsets.only(left: 10, right: 3, top: 0, bottom: 0),
);
}
}
You can use Wrap Widget that takes a list of Widgets as it's child, in this case that would be a containers with text inside.
您可以使用Wrap Widget,它将Widget列表作为其子级,在这种情况下,它将是一个内部包含文本的容器。 All the widgets that are not fitting in the row will be moved to the next line.
该行中不适合的所有小部件都将移至下一行。 You can set the spacing vertically and horizontally too.
您也可以垂直和水平设置间距。
class GridWidget extends StatefulWidget {
@override
_GridWidgetState createState() => _GridWidgetState();
}
class _GridWidgetState extends State<GridWidget> {
List<String> text = ['Android', 'IOS', 'Flutter', 'IOT', 'Text', 'Text', 'Text'];
List<Widget> widgets;
@override
void initState() {
widgets = List.generate(text.length, (index)=>Container(
width: (text[index].length * 16).toDouble(),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(15)),
border: Border.all(
color: Colors.black54,
width: 2,
)
),
child: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: Text(text[index], textAlign: TextAlign.center,),
),
),
),
), growable: false);
super.initState();
}
@override
Widget build(BuildContext context) {
return Center(
child: Wrap(
runSpacing: 10,
spacing: 10,
direction: Axis.horizontal,
children: widgets,
),
);
}
}
Although in this case I hard coded the container width. 尽管在这种情况下,我对容器的宽度进行了硬编码。 In most cases it should've just wrapped around the padding.
在大多数情况下,它应该只是包裹在填充物周围。 Needs some thinking there, but I hope you get the idea, try running this.
在那里需要一些思考,但是我希望您能理解,尝试运行此方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.