Reputation: 35
I want to recreate the ui shown in the picture. It’s supposed to allow users select an option from 3 possible choices. 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.
Upvotes: 1
Views: 176
Reputation: 10389
This would do the trick. I've used a ListView.separated(...) to create the 3 category items. And each item is an ElevatedButton.
Take a look at the screenshot below and the live demo on 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,
),
),
],
),
),
);
}
}
Upvotes: 3