【问题标题】:Achieving this layout in flutter在颤动中实现这种布局
【发布时间】:2021-10-08 19:23:08
【问题描述】:

我试图在颤振中实现这样的目标。我有一个带有这些圆形容器的水平滚动条。如果可滚动中的元素超过 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))),
            ),
          ],
        )
      ],
    ),
  ),
);
}

上述构建方法产生以下结果。(此处的值是硬编码的,仅用于演示)。列表值将是动态的,所需的结果应该像视频中的那样。我该如何处理?

https://streamable.com/w142je

【问题讨论】:

  • 请分享Minimal, Reproducible Example,以便我们更好地帮助您
  • @rckrd 我已经进行了编辑,你能看看吗?在此先感谢..如果您愿意,我们可以对此进行进一步讨论...

标签: flutter flutter-layout


【解决方案1】:
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),
    );
  }
}

【讨论】:

    猜你喜欢
    • 2021-10-05
    • 2013-11-16
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多