gpt4 book ai didi

flutter - Flutter:如何在主页中添加SplashScreen

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

嗨,我正在尝试将SplashScreen页面添加到main.dart中,但是在将SplashScreen()添加到我的家时遇到了问题:

我的家目前是脚手架。我想知道如何在其中包含我的SplashScreen。我试图弄清楚如何更换脚手架。有人可以帮我吗。提前非常感谢您。

void main() => runApp(MyApp());


class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MyAppState();
}
}

class MyAppState extends State<MyApp> {

int _selectedPage = 0;
final _pageOptions = [
Home(),
Category(),
Video(),

];

@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',


initialRoute: '/',
onGenerateRoute: RouteGenerator.generateRoute,


home: Scaffold(



body: _pageOptions[_selectedPage],
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: _selectedPage,
onTap: (int index) {
setState(() {
_selectedPage = index;
});
},
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home), title: Text('Home')),
BottomNavigationBarItem(
icon: Icon(Icons.format_list_bulleted), title: Text('Category')),
BottomNavigationBarItem(
icon: Icon(Icons.subscriptions), title: Text('Videos')),

]

),
)

);

}
}

最佳答案

试试下面的代码:

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SplashScreen(),
);
}
}

class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
@override
void initState() {
super.initState();
countDownTime();
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text("Splash Screen"),
),
);
}

countDownTime() async {
return Timer(
Duration(seconds: 2),
() async {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => HomeScreen()),
);
},
);
}
}

class HomeScreen extends StatefulWidget {
@override
HomeScreenState createState() => HomeScreenState();
}

class HomeScreenState extends State<HomeScreen> {
int _selectedPage = 0;
final _pageOptions = [
Scaffold(body: Center(child: Text("Home")),),
Scaffold(body: Center(child: Text("Category")),),
Scaffold(body: Center(child: Text("Video")),),
];

@override
Widget build(BuildContext context) {
return Scaffold(
body: _pageOptions[_selectedPage],
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: _selectedPage,
onTap: (int index) {
setState(() {
_selectedPage = index;
});
},
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home), title: Text('Home')),
BottomNavigationBarItem(
icon: Icon(Icons.format_list_bulleted),
title: Text('Category')),
BottomNavigationBarItem(
icon: Icon(Icons.subscriptions), title: Text('Videos')),
]),
);
}
}

关于flutter - Flutter:如何在主页中添加SplashScreen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62355518/

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