Skip to content

Commit

Permalink
[#5] Implement Splash screen
Browse files Browse the repository at this point in the history
  • Loading branch information
nkhanh44 committed Aug 8, 2023
1 parent e79bd33 commit c66507b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
21 changes: 5 additions & 16 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,25 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:survey_flutter/gen/assets.gen.dart';
import 'package:go_router/go_router.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:survey_flutter/screens/splash/splash.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await FlutterConfig.loadEnvVariables();
runApp(MyApp());
}

const routePathRootScreen = '/';
const routePathSecondScreen = 'second';
const routePathSplashScreen = '/';

class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);

final GoRouter _router = GoRouter(
routes: <GoRoute>[
GoRoute(
path: routePathRootScreen,
path: routePathSplashScreen,
builder: (BuildContext context, GoRouterState state) =>
const HomeScreen(),
routes: [
GoRoute(
path: routePathSecondScreen,
builder: (BuildContext context, GoRouterState state) =>
const SecondScreen(),
),
],
const SplashScreen()
),
],
);
Expand Down Expand Up @@ -83,11 +76,7 @@ class HomeScreen extends StatelessWidget {
FlutterConfig.get('SECRET'),
style: const TextStyle(color: Colors.black, fontSize: 24),
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: () => context.go('/$routePathSecondScreen'),
child: const Text("Navigate to Second Screen"),
),
const SizedBox(height: 24)
],
),
),
Expand Down
64 changes: 64 additions & 0 deletions lib/screens/splash/splash.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'package:flutter/material.dart';
import 'package:survey_flutter/gen/assets.gen.dart';
class SplashScreen extends StatefulWidget {
const SplashScreen({Key? key}) : super(key: key);

@override
State<StatefulWidget> createState() => SplashScreenState();
}

class SplashScreenState extends State<SplashScreen> {
bool _isLogoVisible = false;
bool _shouldStartAnimate = false;

@override
void initState() {
super.initState();
_delayedLogoVisibility();
}

void _delayedLogoVisibility() async {
await Future.delayed(const Duration(seconds: 1));
setState(() {
_isLogoVisible = true;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: LayoutBuilder(builder: (context, constraints) {
return Stack(
alignment: AlignmentDirectional.center,
fit: StackFit.expand,
children: [
Image.asset(
Assets.images.splashBackground.path,
fit: BoxFit.cover,
),
_buildAnimatedLogo(constraints),
],
);
}),
);
}

Widget _buildAnimatedLogo(BoxConstraints constraints) {
return AnimatedPositioned(
duration: const Duration(seconds: 1),
top: _shouldStartAnimate ? 153.0 : constraints.maxHeight / 2.0,
child: AnimatedOpacity(
opacity: _isLogoVisible ? 1.0 : 0.0,
duration: const Duration(milliseconds: 500),
child: Image.asset(
Assets.images.splashLogoWhite.path,
),
onEnd: () {
setState(() {
_shouldStartAnimate = true;
});
},
),
);
}
}

0 comments on commit c66507b

Please sign in to comment.