I want to generate a random name without repeating the name
I tried a lot but did not get any result
Is there a way for me to do it
In fact, I've tried a lot on whether a variable is a number, The operation was completed successfully,
But I encountered difficulties in case the variable is of type name
import 'package:flutter/material.dart';
import 'dart:math';
class RandomName extends StatefulWidget {
static const id = 'RandomName';
const RandomName({super.key});
@override
State<RandomName> createState() => _RandomNameState();
}
class _RandomNameState extends State<RandomName> {
List<String> names = [];
Random random = Random();
TextEditingController nameController = TextEditingController();
This is the second part:
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[900],
appBar: AppBar(),
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: names.length,
itemBuilder: ((context, index) {
return Dismissible(
key: UniqueKey(),
onDismissed: (direction) {
setState(() {
names.removeAt(index);
});
},
child: ListTile(
title: Text(names[index]),
),
);
}))),
Expanded(
child: Padding(
padding: EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
"add new",
style: TextStyle(
color: Colors.grey.shade500,
fontWeight: FontWeight.w600),
),
Row(
children: <Widget>[
Expanded(
child: TextField(
autocorrect: false,
autofocus: true,
maxLines: 1,
cursorColor: Colors.grey.shade500,
cursorWidth: 1.5,
textInputAction: TextInputAction.done,
keyboardAppearance: Brightness.dark,
style: TextStyle(
fontSize: 20, color: Colors.grey.shade500),
textAlign: TextAlign.center,
keyboardType: TextInputType.name,
controller: nameController,
decoration: const InputDecoration(
border: InputBorder.none,
contentPadding:
EdgeInsets.only(top: 10, bottom: 10),
isDense: true,
),
),
),
ElevatedButton(
child: Text('Add'),
onPressed: () {
addToList();
nameController.clear();
},
)
],
),
Builder(
builder: (context) => ElevatedButton(
child: Text('Get Random Name'),
onPressed: () {
showAlertDialog(context);
}))
],
),
),
),
],
),
);
}
This is the third part:
The code is required to print four random names without repeating inside "Text"
Future<void> showAlertDialog(BuildContext context) {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Container(
margin: const EdgeInsets.all(8),
padding: const EdgeInsets.all(5),
width: MediaQuery.of(context).size.width / 1.3,
height: MediaQuery.of(context).size.height / 3.5,
color: Colors.grey.shade900,
alignment: Alignment.center,
child: Row(
children: <Widget>[
Text(
names[random.nextInt(names.length)],
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 15),
),
Text(
names[random.nextInt(names.length)],
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 15),
),
Text(
names[random.nextInt(names.length)],
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 15),
),
Text(
names[random.nextInt(names.length)],
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 15),
),
],
),
),
backgroundColor: Colors.grey.shade900,
);
});
}
void addToList() {
if (nameController.text.isNotEmpty) {
setState(() {
names.add(nameController.text);
});
}
}
}
CodePudding user response:
Copy the list into a temporary list and give it a shuffle to randomize the order. And then simply pick the last element, print it, and remove it from the list. You can do it as many times as the length of the original list to print all the elements or limit the number accordingly.
void main() {
List<String> names = ['a', 'b', 'c', 'd'];
List<String> namesCopy = List.from(names);
final total = names.length; // Set it to whatever number you want
for (var i = 0; i < total; i ) {
printRandom(namesCopy);
}
}
void printRandom(List<String> namesCopy) {
if (namesCopy.isEmpty) return; // or return print('empty list');
namesCopy.shuffle();
print(namesCopy.last);
namesCopy.removeLast();
}
If you need to implement it in the way you have shown in the code sample, you can modify it like so:
import 'dart:math';
void main() {
List<String> names = ['a', 'b', 'c', 'd'];
Random random = Random();
String name = names[random.nextInt(names.length)];
print(name);
names.remove(name);
name = names[random.nextInt(names.length)];
print(name);
names.remove(name);
name = names[random.nextInt(names.length)];
print(name);
names.remove(name);
name = names[random.nextInt(names.length)];
print(name);
names.remove(name);
}
CodePudding user response:
Check this one:
import 'dart:math';
List<String> names = [
"Alice",
"Bob",
"Charlie",
"David",
"Eve",
"Frank",
"Gina",
"Henry",
"Ivy",
"Joan",
];
Set<String> usedNames = Set();
Random random = Random();
String getRandomName() {
String name;
do {
name = names[random.nextInt(names.length)];
} while (usedNames.contains(name));
usedNames.add(name);
return name;
}