首页 > 解决方案 > 如何在 Flutter 中进行这种 UI 设计

问题描述

我正在制作一个简单的 Flutter 应用程序,它有一堆圆形图标按钮,它们显示在一行中,直到没有更多空间,其余按钮然后显示在下一行中,直到没有更多空间,所以上。

但是我找不到构建小部件的正确方法。(仍然是新的颤振)

这是我正在寻找的结果:(应该支持两种设备方向)

一些有趣的素描

一个真实的例子:

在此处输入图像描述

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.blue[700]),
      home: Scaffold(
        appBar: AppBar(
          title: Text("App Header"),
          centerTitle: true,
        ),
        body: Text("No idea how I should structure the icons/buttons"),
      ),
    );
  }
}

标题不是很好,但我不知道如何称呼这个设计。

标签: flutteruser-interfacedart

解决方案


您可以使用Gridview()

请看 -这是一个示例代码

我为你创建一个演示

在此处输入图像描述

你也可以复制我的代码

这是我的代码-

import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {
  MyHomePage({
    Key key,
  }) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Help from Abir"),
      ),
      body: Center(
        child: OrientationBuilder(builder: (context, orientation) {
          return GridView.count(
            padding: EdgeInsets.all(10.0),
            crossAxisCount: orientation == Orientation.portrait ? 3 : 5,
            children: <Widget>[
              Column(
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Colors.amber,
                    ),
                    child: IconButton(
                      iconSize: 60,
                      icon: Icon(Icons.ac_unit),
                      onPressed: () {},
                    ),
                  ),
                  Text("Item")
                ],
              ),
              Column(
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Colors.amber,
                    ),
                    child: IconButton(
                      iconSize: 60,
                      icon: Icon(Icons.ac_unit),
                      onPressed: () {},
                    ),
                  ),
                  Text("Item")
                ],
              ),
              Column(
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Colors.amber,
                    ),
                    child: IconButton(
                      iconSize: 60,
                      icon: Icon(Icons.ac_unit),
                      onPressed: () {},
                    ),
                  ),
                  Text("Item")
                ],
              ),
              Column(
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Colors.amber,
                    ),
                    child: IconButton(
                      iconSize: 60,
                      icon: Icon(Icons.ac_unit),
                      onPressed: () {},
                    ),
                  ),
                  Text("Item")
                ],
              ),
              Column(
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Colors.amber,
                    ),
                    child: IconButton(
                      iconSize: 60,
                      icon: Icon(Icons.ac_unit),
                      onPressed: () {},
                    ),
                  ),
                  Text("Item")
                ],
              ),
              Column(
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Colors.amber,
                    ),
                    child: IconButton(
                      iconSize: 60,
                      icon: Icon(Icons.ac_unit),
                      onPressed: () {},
                    ),
                  ),
                  Text("Item")
                ],
              ),
              Column(
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Colors.amber,
                    ),
                    child: IconButton(
                      iconSize: 60,
                      icon: Icon(Icons.ac_unit),
                      onPressed: () {},
                    ),
                  ),
                  Text("Item")
                ],
              ),
              Column(
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Colors.amber,
                    ),
                    child: IconButton(
                      iconSize: 60,
                      icon: Icon(Icons.ac_unit),
                      onPressed: () {},
                    ),
                  ),
                  Text("Item")
                ],
              ),
              Column(
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Colors.amber,
                    ),
                    child: IconButton(
                      iconSize: 60,
                      icon: Icon(Icons.ac_unit),
                      onPressed: () {},
                    ),
                  ),
                  Text("Item")
                ],
              ),
              Column(
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Colors.amber,
                    ),
                    child: IconButton(
                      iconSize: 60,
                      icon: Icon(Icons.ac_unit),
                      onPressed: () {},
                    ),
                  ),
                  Text("Item")
                ],
              ),
              Column(
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Colors.amber,
                    ),
                    child: IconButton(
                      iconSize: 60,
                      icon: Icon(Icons.ac_unit),
                      onPressed: () {},
                    ),
                  ),
                  Text("Item")
                ],
              ),
              Column(
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Colors.amber,
                    ),
                    child: IconButton(
                      iconSize: 60,
                      icon: Icon(Icons.ac_unit),
                      onPressed: () {},
                    ),
                  ),
                  Text("Item")
                ],
              ),
            ],
          );
        }),
      ),
    );
  }
}

推荐阅读