简体   繁体   English

如何使用颤振创建如下所示的 UI?

[英]How do i create the UI shown below with flutter?

I want to recreate the ui shown in the picture.我想重新创建图片中显示的 ui。 It's supposed to allow users select an option from 3 possible choices.它应该允许用户从 3 个可能的选项中选择一个选项。 I also plan on showing different containers below the ui depending on user selection but i cant seem to find the right flutter widget to create this.我还计划根据用户选择在 ui 下方显示不同的容器,但我似乎找不到合适的颤振小部件来创建它。

在此处输入图像描述

This would do the trick.这可以解决问题。 I've used a ListView.separated(...) to create the 3 category items.我使用ListView.separated(...)创建了 3 个类别项。 And each item is an ElevatedButton .每个项目都是一个ElevatedButton

Take a look at the screenshot below and the live demo on DartPad :看看下面的截图和DartPad 上的现场演示

截屏

And this is the code:这是代码:

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(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const SelectCategory(),
    );
  }
}

const categories = ['Single', 'Couple', 'Multiple'];

class SelectCategory extends StatefulWidget {
  const SelectCategory({Key? key}) : super(key: key);

  @override
  State<SelectCategory> createState() => _SelectCategoryState();
}

class _SelectCategoryState extends State<SelectCategory> {
  String? selectedCategory;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.fromLTRB(32, 12, 32, 12),
        child: Column(
          children: [
            Row(
              children: const [
                Text(
                  'Select your category',
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 28,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ],
            ),
            const SizedBox(height: 64),
            Expanded(
              child: ListView.separated(
                itemBuilder: (context, index) {
                  final category = categories[index];
                  return ElevatedButton(
                    onPressed: () =>
                        setState(() => selectedCategory = categories[index]),
                    style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all(Colors.white),
                      overlayColor: MaterialStateProperty.all(Colors.black12),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.all(12.0),
                      child: Row(
                        children: [
                          Text(
                            category,
                            style: const TextStyle(
                                color: Colors.black, fontSize: 28),
                          ),
                          const Spacer(),
                          Icon(
                            selectedCategory == categories[index]
                                ? Icons.check_circle
                                : Icons.circle_outlined,
                            color: selectedCategory == categories[index]
                                ? const Color.fromARGB(255, 31, 102, 208)
                                : const Color.fromARGB(255, 172, 170, 170),
                            size: 64,
                          ),
                        ],
                      ),
                    ),
                  );
                },
                separatorBuilder: (_, __) => const SizedBox(height: 48),
                itemCount: categories.length,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM