我有一个ListView.builder,在每个项目中我打开一个配置文件页面
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ProfileScreen(
peerId: document['userId'],
)),
);
问题是我有一个_shuffle()
函数,该函数将所有文档随机化,当我关闭该ProfileScreen()
时,整个ListView再次被随机化,并且由于失去滚动状态,使得UX非常困难。 / p>
有什么建议吗?
编辑 检查过滤器的代码:
Future<QuerySnapshot> _filteredSnapshot() {
if (countryFilter) {
return Firestore.instance.collection('users').where('country', isEqualTo: country).getDocuments();
} else {
return Firestore.instance.collection('users').getDocuments();
}
}
FutureBuilder<QuerySnapshot>
中的代码:
if (snapshot.data.documents.length > 0) {
print('STREAM ALERT');
final List<DocumentSnapshot> userList = snapshot.data.documents
.where((DocumentSnapshot documentSnapshot) => documentSnapshot['userid'] != id)
.toList();
List<DocumentSnapshot> userShuffle = _shuffle(userList);
return GridView.builder(
padding: EdgeInsets.all(10.0),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 10.0,
crossAxisSpacing: 10.0,
childAspectRatio: 0.85),
itemBuilder: (context, index) {
return _buildItem(context, userShuffle[index]);
},
itemCount: userShuffle.length,
);
}
答案 0 :(得分:0)
这是给您的示例代码。这可能对您有帮助。该示例包含2个文件main.dart和profile_screen.dart。我已经在main.dart中生成了随机列表,然后从main.dart中导航到profile_screen.dart并返回,该列表保持不变(不再再次生成随机数),并且滚动也保持在相同位置。
main.dart代码
import 'package:flutter/material.dart';
import './profile_screen.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<String> items = [];
@override
void initState() {
_generateRandomItemsAndShuffle();
super.initState();
}
_generateRandomItemsAndShuffle() {
items = List<String>.generate(30, (int index) => "ITEM $index");
items.shuffle();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Stack Overflow',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Stackoverflow'),
),
body: ListView.builder(
itemCount: items == null ? 0 : items.length,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
child: ListTile(
title: Text(items[index]),
trailing: Icon(Icons.arrow_right),
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ProfileScreen(
itemName: items[index],
),
),
);
},
);
},
),
),
);
}
}
profile_screen.dart的代码
import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
class ProfileScreen extends StatefulWidget {
String itemName;
ProfileScreen({this.itemName});
@override
_ProfileScreenState createState() => _ProfileScreenState();
}
class _ProfileScreenState extends State<ProfileScreen> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('${widget.itemName}'),
),
body: Center(
child: Column(
children: [Text('${widget.itemName}')],
),
),
);
}
}
class ProfileScreen extends StatefulWidget {
String itemName;
ProfileScreen({this.itemName});
@override
_ProfileScreenState createState() => _ProfileScreenState();
}
class _ProfileScreenState extends State<ProfileScreen> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('${widget.itemName}'),
),
body: Center(
child: Column(
children: [Text('${widget.itemName}')],
),
),
);
}
}
此代码示例可以帮助您解决问题。