(Flutter)一种简单布局
CODE
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(debugShowCheckedModeBanner: false, home: MyHomePage());
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final List _posts = ['post1', 'post2', 'post3'];
final List _stories = [
'story1',
'story2',
'story3',
'story4',
'story5',
'story6'
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Container(
height: 150,
child: ListView.builder(
itemCount: _stories.length,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
return MyCircle(child: _stories[index]);
},
),
),
Expanded(
flex: 5,
child: ListView.builder(
itemCount: _posts.length,
itemBuilder: (context, index) {
return MySquare(
child: _posts[index],
);
},
),
)
],
));
}
}
class MySquare extends StatelessWidget {
final String child;
MySquare({required this.child});
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(8),
child: Container(
height: 200,
color: Colors.blue,
child: Center(
child: Text(
child,
style: TextStyle(
fontSize: 40,
),
),
),
),
);
}
}
class MyCircle extends StatelessWidget {
final String child;
MyCircle({required this.child});
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(8),
child: Container(
height: 100,
width: 100,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.pink,
),
child: Center(
child: Text(
this.child,
style: TextStyle(fontSize: 24),
),
),
),
);
}
}
预览

License:
CC BY 4.0