gpt4 book ai didi

flutter - 在Flutter中首次启动时的屏幕选择器

转载 作者:行者123 更新时间:2023-12-03 03:14:34 24 4
gpt4 key购买 nike

首先让我解释一下。
我的应用程序中有三个屏幕。这些是 MyHome.dart OtherHome.dart Selector.dart
我想在初次启动时启动选择器屏幕。在选择器屏幕中,有两个用户选项。一个是MyHome,另一个是OtherHome。首次启动后,该应用将始终在首次启动时打开用户最后选择的屏幕。正确的代码是什么?
Main.dart :

import 'selector.dart';
import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: Selector(),
));
}
Selector.dart :
import 'package:device_monitor/home.dart';
import 'package:flutter/material.dart';
import 'home.dart';
import 'myhome.dart';

class Selector extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyHome()),
);
},
child: Text('My Device'),
),
SizedBox(height: 30),
RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Home()),
);
},
child: Text('Others Device'),
),
],
),
);
}
}

最佳答案

以下代码可以为您提供帮助:


import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(MaterialApp(
home: Selector(),
));
}
您需要获得 共享首选项软件包,这是 link
class Selector extends StatefulWidget {
@override
_SelectorState createState() => _SelectorState();
}

class _SelectorState extends State<Selector> {

bool pageReady = false;

/// This checks the whether page has been selected earlier,
/// should be placed in an initstate function
_checkPages() async {
SharedPreferences local = await SharedPreferences.getInstance();
if(local.getString('page-selected') != null){
if(local.getString('page-selected') == "1"){
//navigate to MyHome

Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyHome()),
);

} else {
//Navigate to Home
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Home()),
);
}
} else {
setState(() {
pageReady = true;
});
}
}

@override
void initState() {
// TODO: implement initState
super.initState();
_checkPages();
}

savePage(String type) async {
if(type == "1"){
SharedPreferences local = await SharedPreferences.getInstance();
local.setString('page-selected', type);
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyHome()),
);
} else {
SharedPreferences local = await SharedPreferences.getInstance();
local.setString('page-selected', type);
Navigator.push(
context,
MaterialPageRoute(builder: ( context ) => Home()),
);
}
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: pageReady ? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: () {
savePage("1");
},
child: Text('My Device'),
),
SizedBox(height: 30),
RaisedButton(
onPressed: () {
savePage("2");
},
child: Text('Others Device'),
),
],
) : Center(child: CircularProgressIndicator()),
);
}
}

class MyHome extends StatefulWidget {
@override
_MyHomeState createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
@override
Widget build(BuildContext context) {
return Container();
}
}


class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container();
}


因此,我将 Selector()更改为有状态的小部件,并使用 initState()检查用户先前是否已选择页面,如果是,它将用户转到该页面,否则它将打开选择器页面,并且一旦用户选择了页面,我便保存了 session 中的页面也可以使用 savePage()函数。

关于flutter - 在Flutter中首次启动时的屏幕选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62730457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com