英文:
How to revert the list to the beginning on Flutter?
问题
尝试使用按钮来排序整数值列表,当升序排列时,它可以工作,但再次按下“Sort Value”按钮返回原始状态时无法工作,卡住了。
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: MyApp()));
}
class Product {
final String name;
final String category;
final int value;
Product({
required this.name,
required this.category,
required this.value,
});
}
List<Product> productlist = [
Product(name: 'Sandwich', category: 'Food', value: 2),
Product(name: 'Fish', category: 'Food', value: 1),
Product(name: 'Vegetable', category: 'Food', value: 9),
Product(name: 'Meat', category: 'Food', value: 13),
Product(name: 'Book', category: 'Item', value: 4),
Product(name: 'Hammer', category: 'Item', value: 6),
Product(name: 'Pen', category: 'Item', value: 5),
Product(name: 'Car', category: 'Vehicle', value: 8),
Product(name: 'Bike', category: 'Vehicle', value: 14),
Product(name: 'Motor', category: 'Vehicle', value: 0),
Product(name: 'Boat', category: 'Vehicle', value: 25),
];
class MyApp extends StatefulWidget {
MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final List<String> categories = ['Food', 'Item', 'Vehicle'];
List<String> selectedCategories = [];
bool isActive = true;
@override
Widget build(BuildContext context) {
final filterProducts = productlist.where((product) {
return selectedCategories.isEmpty ||
selectedCategories.contains(product.category);
}).toList();
return Scaffold(
appBar: AppBar(
title: const Text("Filter Sorting"),
centerTitle: true,
),
body: Column(
children: [
Container(
padding: const EdgeInsets.all(8),
margin: const EdgeInsets.all(8),
child: Row(
children: [
Expanded(
flex: 3,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: categories
.map((category) => FilterChip(
selected:
selectedCategories.contains(category),
label: Text(category),
onSelected: (selected) {
setState(() {
if (selected) {
selectedCategories.add(category);
} else {
selectedCategories.remove(category);
}
});
}))
.toList(),
)),
Expanded(
flex: 1,
child: ElevatedButton(
onPressed: () {
setState(() {
if (isActive) {
productlist
.sort((b, a) => b.value.compareTo(a.value));
} else {
productlist.sort((a, b) =>
a.value.compareTo(b.value));
}
isActive = !isActive;
});
},
child: const Text('Sort Value, \nLow to High',
style: TextStyle(fontSize: 10))),
),
],
)),
Expanded(
child: ListView.builder(
itemCount: filterProducts.length,
itemBuilder: ((context, index) {
final product = filterProducts[index];
return Card(
elevation: 8,
margin: const EdgeInsets.all(8),
child: Container(
decoration: BoxDecoration(color: Colors.blue),
child: ListTile(
contentPadding: const EdgeInsets.symmetric(
horizontal: 10, vertical: 10),
title: Text(
product.name,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
subtitle: Text(
'${product.value}',
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
)),
));
})))
],
));
}
}
我需要按升序排列顺序,然后还原整个列表,然后重复此操作。
英文:
Trying to use Button to sort the int value list, it works when ascending the order, but it doesn't work when going back to the original state by pressing the Sort Value
button once more, stuck.
Outcome after Sort Value
button pressed
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: MyApp()));
}
class Product {
final String name;
final String category;
final int value;
Product({
required this.name,
required this.category,
required this.value,
});
}
List<Product> productlist = [
Product(name: 'Sandwich', category: 'Food', value: 2),
Product(name: 'Fish', category: 'Food', value: 1),
Product(name: 'Vegetable', category: 'Food', value: 9),
Product(name: 'Meat', category: 'Food', value: 13),
Product(name: 'Book', category: 'Item', value: 4),
Product(name: 'Hammer', category: 'Item', value: 6),
Product(name: 'Pen', category: 'Item', value: 5),
Product(name: 'Car', category: 'Vehicle', value: 8),
Product(name: 'Bike', category: 'Vehicle', value: 14),
Product(name: 'Motor', category: 'Vehicle', value: 0),
Product(name: 'Boat', category: 'Vehicle', value: 25),
];
class MyApp extends StatefulWidget {
MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final List<String> categories = ['Food', 'Item', 'Vehicle'];
List<String> selectedCategories = [];
bool isActive = true;
@override
Widget build(BuildContext context) {
final filterProducts = productlist.where((product) {
return selectedCategories.isEmpty ||
selectedCategories.contains(product.category);
}).toList();
return Scaffold(
appBar: AppBar(
title: const Text("Fitler Sorting"),
centerTitle: true,
),
body: Column(
children: [
Container(
padding: const EdgeInsets.all(8),
margin: const EdgeInsets.all(8),
child: Row(
children: [
Expanded(
flex: 3,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: categories
.map((category) => FilterChip(
selected:
selectedCategories.contains(category),
label: Text(category),
onSelected: (selected) {
setState(() {
if (selected) {
selectedCategories.add(category);
} else {
selectedCategories.remove(category);
}
});
}))
.toList(),
)),
Expanded(
flex: 1,
child: ElevatedButton(
/** */
onPressed: () {
setState(() {
if (isActive) {
productlist
.sort((b, a) => b.value.compareTo(a.value));
} else {
productlist.toList();
}
});
},
child: const Text('Sort Value, \nLow to High',
style: TextStyle(fontSize: 10))),
),
],
)),
Expanded(
child: ListView.builder(
itemCount: filterProducts.length,
itemBuilder: ((context, index) {
final product = filterProducts[index];
return Card(
elevation: 8,
margin: const EdgeInsets.all(8),
child: Container(
decoration: BoxDecoration(color: Colors.blue),
child: ListTile(
contentPadding: const EdgeInsets.symmetric(
horizontal: 10, vertical: 10),
title: Text(
product.name,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
subtitle: Text(
'${product.value}',
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
)),
));
})))
],
));
}
}
I need to arrange the order ascendingly, then revert the whole list, rinse and repeat.
答案1
得分: 1
在列表首次创建或修改时,保存另一个变量的副本,例如
originalList = productlist.toList();
这样你的程序就变成了:
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: MyApp()));
}
class Product {
final String name;
final String category;
final int value;
Product({
required this.name,
required this.category,
required this.value,
});
}
List<Product> productlist = [
Product(name: 'Sandwich', category: 'Food', value: 2),
Product(name: 'Fish', category: 'Food', value: 1),
Product(name: 'Vegetable', category: 'Food', value: 9),
Product(name: 'Meat', category: 'Food', value: 13),
Product(name: 'Book', category: 'Item', value: 4),
Product(name: 'Hammer', category: 'Item', value: 6),
Product(name: 'Pen', category: 'Item', value: 5),
Product(name: 'Car', category: 'Vehicle', value: 8),
Product(name: 'Bike', category: 'Vehicle', value: 14),
Product(name: 'Motor', category: 'Vehicle', value: 0),
Product(name: 'Boat', category: 'Vehicle', value: 25),
];
class MyApp extends StatefulWidget {
MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final List<String> categories = ['Food', 'Item', 'Vehicle'];
List<String> selectedCategories = [];
final originalList = productlist.toList();
bool isActive = true;
@override
Widget build(BuildContext context) {
final filterProducts = productlist.where((product) {
return selectedCategories.isEmpty ||
selectedCategories.contains(product.category);
}).toList();
return Scaffold(
appBar: AppBar(
title: const Text("Fitler Sorting"),
centerTitle: true,
),
body: Column(
children: [
Container(
padding: const EdgeInsets.all(8),
margin: const EdgeInsets.all(8),
child: Row(
children: [
Expanded(
flex: 3,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: categories
.map((category) => FilterChip(
selected: selectedCategories.contains(category),
label: Text(category),
onSelected: (selected) {
setState(() {
if (selected) {
selectedCategories.add(category);
} else {
selectedCategories.remove(category);
}
});
}))
.toList(),
)),
Expanded(
flex: 1,
child: ElevatedButton(
onPressed: () {
setState(() {
if (isActive) {
productlist.sort((b, a) =>
b.value.compareTo(a.value));
} else {
productlist = originalList.toList();
}
isActive = !isActive;
});
},
child: const Text('Sort Value, \nLow to High',
style: TextStyle(fontSize: 10)),
),
),
],
)),
Expanded(
child: ListView.builder(
itemCount: filterProducts.length,
itemBuilder: ((context, index) {
final product = filterProducts[index];
return Card(
elevation: 8,
margin: const EdgeInsets.all(8),
child: Container(
decoration: BoxDecoration(color: Colors.blue),
child: ListTile(
contentPadding: const EdgeInsets.symmetric(
horizontal: 10, vertical: 10),
title: Text(
product.name,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
subtitle: Text(
'${product.value}',
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
)),
));
})))
],
));
}
}
英文:
When the list is first created or modified save a copy of it in another variable, like for example
originalList = productlist.toList();
so your program becomes
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: MyApp()));
}
class Product {
final String name;
final String category;
final int value;
Product({
required this.name,
required this.category,
required this.value,
});
}
List<Product> productlist = [
Product(name: 'Sandwich', category: 'Food', value: 2),
Product(name: 'Fish', category: 'Food', value: 1),
Product(name: 'Vegetable', category: 'Food', value: 9),
Product(name: 'Meat', category: 'Food', value: 13),
Product(name: 'Book', category: 'Item', value: 4),
Product(name: 'Hammer', category: 'Item', value: 6),
Product(name: 'Pen', category: 'Item', value: 5),
Product(name: 'Car', category: 'Vehicle', value: 8),
Product(name: 'Bike', category: 'Vehicle', value: 14),
Product(name: 'Motor', category: 'Vehicle', value: 0),
Product(name: 'Boat', category: 'Vehicle', value: 25),
];
class MyApp extends StatefulWidget {
MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final List<String> categories = ['Food', 'Item', 'Vehicle'];
List<String> selectedCategories = [];
final originalList = productlist.toList();
bool isActive = true;
@override
Widget build(BuildContext context) {
final filterProducts = productlist.where((product) {
return selectedCategories.isEmpty ||
selectedCategories.contains(product.category);
}).toList();
return Scaffold(
appBar: AppBar(
title: const Text("Fitler Sorting"),
centerTitle: true,
),
body: Column(
children: [
Container(
padding: const EdgeInsets.all(8),
margin: const EdgeInsets.all(8),
child: Row(
children: [
Expanded(
flex: 3,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: categories
.map((category) => FilterChip(
selected:
selectedCategories.contains(category),
label: Text(category),
onSelected: (selected) {
setState(() {
if (selected) {
selectedCategories.add(category);
} else {
selectedCategories.remove(category);
}
});
}))
.toList(),
)),
Expanded(
flex: 1,
child: ElevatedButton(
/** */
onPressed: () {
setState(() {
if (isActive) {
productlist
.sort((b, a) => b.value.compareTo(a.value));
} else {
productlist = originalList.toList();
}
isActive = !isActive;
});
},
child: const Text('Sort Value, \nLow to High',
style: TextStyle(fontSize: 10))),
),
],
)),
Expanded(
child: ListView.builder(
itemCount: filterProducts.length,
itemBuilder: ((context, index) {
final product = filterProducts[index];
return Card(
elevation: 8,
margin: const EdgeInsets.all(8),
child: Container(
decoration: BoxDecoration(color: Colors.blue),
child: ListTile(
contentPadding: const EdgeInsets.symmetric(
horizontal: 10, vertical: 10),
title: Text(
product.name,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
subtitle: Text(
'${product.value}',
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
)),
));
})))
],
));
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论