Flutter 如何保持页面的状态处于颤振状态?
我在Flutter 如何保持页面的状态处于颤振状态?,flutter,dart,Flutter,Dart,我在DragSelectGridView中有一个项目列表,因此我希望保留所选项目的状态 假设我的应用程序中有5个页面,我使用pushReplacement从第1页导航到第2页。第2页是我从DragSelectGridView中选择itmes,然后使用pushReplacement导航到第3页,并从第3页导航到第4页。当我使用pushReplacement导航回第2页时,我选择的所有项目都将被处理 即使关闭应用程序,我也希望保留在第2页中选择的项目的状态。 我曾尝试使用SharePrefrence
DragSelectGridView
中有一个项目列表,因此我希望保留所选项目的状态
假设我的应用程序中有5个页面,我使用pushReplacement
从第1页导航到第2页。第2页是我从DragSelectGridView
中选择itmes,然后使用pushReplacement
导航到第3页,并从第3页导航到第4页。当我使用pushReplacement
导航回第2页时,我选择的所有项目都将被处理
即使关闭应用程序,我也希望保留在第2页中选择的项目的状态。
我曾尝试使用SharePrefrences
,但没有成功
这是我的代码:
class _IntresetState extends State<Intereset> {
final controller = DragSelectGridViewController();
final scrollcontrol = ScrollController();
@override
void initState() {
super.initState();
controller.addListener(scheduleRebuild);
}
@override
void dispose() {
controller.removeListener(scheduleRebuild);
super.dispose();
}
void scheduleRebuild() => setState(() {});
List<InterestModel> interestIn = [
InterestModel('assets/Art & Design.jpeg', 'Art & Design'),
InterestModel('assets/Tech.jpeg', 'Tech'),
InterestModel('assets/Food.jpeg', 'Food'),
InterestModel('assets/Animal.jpeg', 'Animal'),
InterestModel('assets/Fitness & Health.jpeg', 'Fitness & Health'),
InterestModel('assets/Car.jpeg', 'Car'),
InterestModel('assets/Sport.jpeg', 'Sport'),
InterestModel('assets/Book.jpeg', 'Book'),
InterestModel('assets/Game.jpeg', 'Game'),
InterestModel('assets/Film.jpeg', 'Film'),
InterestModel('assets/Travelling.jpeg', 'Travelling'),
InterestModel('assets/Tv & Music.jpeg', 'Tv & Music'),
InterestModel('assets/Dancing.jpeg', 'Dancing'),
InterestModel('assets/Cooking.jpeg', 'Cooking'),
InterestModel('assets/Learning.jpeg', 'Learning'),
InterestModel('assets/Pet.jpeg', 'Pet'),
InterestModel('assets/Politics.jpeg', 'Politics'),
InterestModel('assets/Photography.jpeg', 'Photography')
];
@override
Widget build(BuildContext context) {
return ProgressHUD(
child: Builder(
builder: (context) => Scaffold(
backgroundColor: Theme.of(context).backgroundColor,
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.transparent,
leading: Container(),
actions: [
GestureDetector(
onTap: () async {
final progress = ProgressHUD.of(context);
progress?.show();
final getLength = controller.value.selectedIndexes
.map((e) => interestIn[e].name)
.toList();
if (getLength.length <= 0) {
print(getLength.length);
progress?.dismiss();
await Flushbar(
title: 'Ops!',
message: 'Select one Interest !!!',
duration: Duration(seconds: 3),
).show(context);
} else {
SharedPreferences pref =
await SharedPreferences.getInstance();
await pref.setBool('interest', true);
await firestore
.collection('interest')
.doc(auth.currentUser?.uid)
.set({'Interest': getLength}).then(
(value) => Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (_) => Done(),
),
),
);
progress?.dismiss();
}
},
child: Container(
height: 40,
width: 40,
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(
colors: [
Constants.color1,
Constants.color2,
],
),
),
child: Center(
child: Icon(
Icons.check_outlined,
size: 18,
color: Colors.white,
)),
),
),
SizedBox(
width: 20,
),
],
),
extendBodyBehindAppBar: true,
body: SafeArea(
minimum: EdgeInsets.all(10.0),
child: Container(
height: double.infinity,
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 10.0),
child: Text('Select Interests',
style: GoogleFonts.raleway(
textStyle: TextStyle(
color: Theme.of(context).primaryColor,
fontSize: 30,
fontWeight: FontWeight.bold,
),
)),
),
Expanded(
child: DragSelectGridView(
shrinkWrap: false,
scrollController: scrollcontrol,
triggerSelectionOnTap: true,
gridController: controller,
itemCount: interestIn.length,
gridDelegate:
new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3, childAspectRatio: 0.7),
itemBuilder:
(BuildContext context, int index, isSelected) {
return Stack(
children: [
Padding(
padding: const EdgeInsets.all(4.0),
child: GestureDetector(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topRight: Radius.circular(40),
bottomLeft: Radius.circular(40),
),
image: DecorationImage(
image:
AssetImage(interestIn[index].url),
fit: BoxFit.fill,
colorFilter: ColorFilter.mode(
Colors.red, BlendMode.darken),
),
),
height: 180,
width: 200,
child: Padding(
padding: const EdgeInsets.only(
left: 25.0, bottom: 10),
child: Center(
child: Align(
alignment: Alignment.bottomLeft,
child: Text(
interestIn[index].name,
style: TextStyle(
fontSize: 20,
color: Colors.white,
fontWeight: FontWeight.w500,
),
),
),
),
),
),
),
),
isSelected
? Positioned(
height: 60,
left: 90,
child: Container(
height: 30,
width: 30,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.white,
),
child: Center(
child: Icon(
Icons.check_outlined,
size: 18,
color: Colors.red,
),
),
),
)
: Container()
],
);
}),
class\u IntresetState扩展状态{
最终控制器=DragSelectGridViewController();
最终scrollcontrol=ScrollController();
@凌驾
void initState(){
super.initState();
controller.addListener(scheduleRebuild);
}
@凌驾
无效处置(){
controller.removeListener(scheduleRebuild);
super.dispose();
}
void scheduleRebuild()=>setState((){});
列表利息=[
兴趣模型(“资产/艺术与设计.jpeg”,“艺术与设计”),
利息模型('assets/Tech.jpeg','Tech'),
兴趣模型('assets/Food.jpeg','Food'),
兴趣模型('assets/Animal.jpeg','Animal'),
兴趣模型('assets/Fitness&Health.jpeg','Fitness&Health'),
兴趣模型('assets/Car.jpeg','Car'),
兴趣模型('assets/Sport.jpeg','Sport'),
兴趣模型('assets/Book.jpeg','Book'),
兴趣模型('assets/Game.jpeg','Game'),
兴趣模型('assets/Film.jpeg','Film'),
兴趣模型('assets/Traveling.jpeg','Traveling'),
兴趣模型(“资产/电视和音乐.jpeg”,“电视和音乐”),
兴趣模型('assets/Dancing.jpeg','Dancing'),
兴趣模型('assets/Cooking.jpeg','Cooking'),
兴趣模型('assets/Learning.jpeg','Learning'),
兴趣模型('assets/Pet.jpeg','Pet'),
兴趣模型('assets/Politics.jpeg','Politics'),
兴趣模型('assets/Photography.jpeg','Photography')
];
@凌驾
小部件构建(构建上下文){
返回进度HUD(
孩子:建筑工人(
生成器:(上下文)=>Scaffold(
背景色:主题。背景色,
appBar:appBar(
海拔:0,
背景颜色:颜色。透明,
前导:Container(),
行动:[
手势检测器(
onTap:()异步{
最终进度=进度(上下文);
进度?.show();
最终getLength=controller.value.selectedIndex
.map((e)=>interestIn[e].name)
.toList();
如果(getLength.length Navigator.pushReplacement(
上下文
材料路线(
生成器:()=>Done(),
),
),
);
进步?.discover();
}
},
子:容器(
身高:40,
宽度:40,
装饰:盒子装饰(
形状:BoxShape.circle,
梯度:线性梯度(
颜色:[
常数1,
常数2,
],
),
),
儿童:中心(
子:图标(
图标。请检查所列出的图标,
尺码:18,
颜色:颜色,白色,
)),
),
),
大小盒子(
宽度:20,
),
],
),
extendedBodyBehindAppBar:true,
正文:安全区(
最小值:所有边缘设置(10.0),
子:容器(
高度:双无限,
宽度:double.infinity,
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
填充物(
填充:仅限常量边集(底部:10.0),
子项:文本('选择兴趣',
风格:GoogleFonts.ralway(
textStyle:textStyle(
颜色:主题。背景。原色,
尺寸:30,
fontWeight:fontWeight.bold,
),
)),
),
扩大(
子项:DragSelectGridView(
收缩膜:假,
scrollController:scrollcontrol,
TriggerSelectionNTAP:正确,
网格控制器:控制器,
itemCount:interestIn.length,
gridDelegate:
新SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount:3,childAspectRatio:0.7),
项目生成器:
(BuildContext上下文,int索引,isSelected){
返回堆栈(
儿童:[
填充物(
填充:常数边集全部(4.0),
儿童:手势检测器(
子:容器(
装饰:盒子装饰(
borderRadius:仅限borderRadius(
右上角:半径。圆形(40),
左下角:半径。圆形(40),
),