flutter - 在颤动中实现这种布局
问题描述
我试图在颤动中实现这样的目标。我有一个带有这些圆形容器的水平滚动条。如果可滚动中的元素大于 3,我希望这些容器的宽度缩小,如果元素小于 2,它应该根据图像扩展。我想要的和这张图像完全一样,我一直在阅读有关灵活的内容小部件,但是当我将它包装在可滚动的行中时,它会给出特定于布局的问题。任何解决方法来实现这一点?
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SingleChildScrollView(
padding: EdgeInsets.zero,
scrollDirection: Axis.horizontal,
child: Row(
children: [
...List.generate(
9,
(index) => Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.green,
),
height: 100,
width: 100,
),
),
)
],
)),
SizedBox(
height: 20,
),
Row(
children: [
...List.generate(
2,
(index) => Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 100,
width: 180,
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(20))),
))
],
),
SizedBox(
height: 20,
),
Row(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 100,
width: 350,
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(20))),
),
],
)
],
),
),
);
}
上述构建方法产生以下结果。(此处的值是硬编码的,仅用于演示)。列表值将是动态的,所需的结果应该像视频中的那样。我该如何进行?
解决方案
import 'package:flutter/material.dart';
void main() {
runApp(const 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 {
final String hintText = 'hing';
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
@override
Widget build(BuildContext context) {
var list1 = List.filled(2, '2');
var list2 = List.filled(4, '4');
var list3 = List.filled(1, '1');
return SafeArea(
child: Scaffold(
body: Column(
children: [
_getList(context, list1),
_getList(context, list2),
_getList(context, list3),
],
),
),
);
}
Widget _getList(BuildContext context, List<String> list) {
bool ownSize = list.length == 2;
if (ownSize) {
return SingleChildScrollView(
padding: EdgeInsets.zero,
scrollDirection: Axis.horizontal,
child: Row(
children: list.map((t) => rowItem(t, 250)).toList(),
),
);
} else {
return Row(
children: list
.map(
(t) => Expanded(child: rowItem(t)),
)
.toList(),
);
}
}
Widget rowItem(String text, [double? width]) {
return Container(
margin: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.green,
),
height: 100,
width: width,
alignment: Alignment.center,
child: Text(text),
);
}
}
推荐阅读
- gps - 我想使用 STM32F0 和 Truestudio 从 NMEA 语句中解析 GNSS 数据
- javascript - 如何为正则表达式添加空格和换行符?
- web-scraping - 适当的设置以避免在抓取时阻塞
- qt - 使用 UI 自动化获取具有可访问名称的 QLabel 的值
- plugins - 自定义 maxscript 修饰符保存在哪里?
- python - 如何在不重新启动 jupyter notebook 的情况下在 Keras 中重新训练神经网络?
- symfony - 电子邮件验证错误 Symfony 4 后会话终止
- session - 为什么我在刷新 facebook 等页面时看不到任何加载程序?
- node.js - Battle.net API 使用 OAuth Token 返回 401 错误
- javascript - 我需要从不同的 javascript 文件中访问数据,并将其更改为字符串