最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Flutter Splash Screen transition to Home Screen not smooth - Stack Overflow

programmeradmin0浏览0评论

The following is my code basically explaining how the splash screen is implemented in my flutter app. A blank screen appears instead of displaying the app logo and then transitions to the home page. On some devices the transition is either delayed or too fast.

I am trying to implement a splash screen in my Flutter app that stays for a few seconds before navigating to the home screen. I want it to show my app logo and transition smoothly to the main screen.

Problem I Am Facing:

The splash screen appears, but it is inconsistent to what I have expected.

  • Transition from splash screen to main screen feels laggy or delayed.
  • On some devices the splash screen duration is not to the intended duration.
  • flutter_native_splash works fine, except the splash screen transition is not smooth.

What I have tried:

  • Used Timer(Duration(seconds: 3)) inside the initState() to add a delay for the transition.
  • Used Future.delayed(Duration(seconds: 3)) inside the initState() as well.
  • Implemented flutter_native_splash, that is laggy as well.
  • Used WidgetsBinding.instance.addPostFrameCallback()
    import 'package:flutter/material.dart';

        void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      int _currentStep = 0;
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(title: Text("Stepper Example")),
            body: Stepper(
              currentStep: _currentStep,
              onStepContinue: () {
                if (_currentStep < 2) {
                  setState(() => _currentStep++);
                }
              },
              onStepCancel: () {
                if (_currentStep > 0) {
                  setState(() => _currentStep--);
                }
              },
              steps: [
                Step(title: Text("Step 1"), content: Text("Content for step 1")),
                Step(title: Text("Step 2"), content: Text("Content for step 2")),
                Step(title: Text("Step 3"), content: Text("Content for step 3")),
              ],
            ),
          ),
        );
      }
    }
    
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: DefaultTabController(
            length: 2,
            child: Scaffold(
              appBar: AppBar(
                title: Text("Tab Bar Example"),
                bottom: TabBar(
                  tabs: [Tab(text: "Tab 1"), Tab(text: "Tab 2")],
                ),
              ),
              body: TabBarView(
                children: [
                  Center(child: Text("Page 1")),
                  Center(
                    child: Builder(
                      builder: (context) => ElevatedButton(
                        onPressed: () {
                          ScaffoldMessenger.of(context).showSnackBar(
                            SnackBar(content: Text("This is a SnackBar!"))
                          );
                        },
                        child: Text("Show SnackBar"),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }
    
    
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: HomeScreen(),
        );
      }
    }
    
    class HomeScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text("Home Screen")),
          body: Center(
            child: ElevatedButton(
              onPressed: () {
                Navigator.push(
                  context, MaterialPageRoute(builder: (context) => SecondScreen())
                );
              },
              child: Text("Go to Second Screen"),
            ),
          ),
        );
      }
    }
    
    class SecondScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text("Second Screen")),
          body: Center(
            child: ElevatedButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text("Go Back"),
            ),
          ),
        );
      }
    }
    
    
    import 'package:flutter/material.dart';
    import 'package:curved_navigation_bar/curved_navigation_bar.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      int _selectedIndex = 0;
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            bottomNavigationBar: CurvedNavigationBar(
              backgroundColor: Colors.blueAccent,
              items: [
                Icon(Icons.home, size: 30),
                Icon(Icons.search, size: 30),
                Icon(Icons.person, size: 30),
              ],
              onTap: (index) {
                setState(() => _selectedIndex = index);
              },
            ),
            body: Center(child: Text("Selected Index: $_selectedIndex")),
          ),
        );
      }
    }
    
    
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(title: Text("Buttons Example")),
            body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                    onPressed: () => print("Elevated Button Pressed"),
                    child: Text("Elevated Button"),
                  ),
                  TextButton(
                    onPressed: () {},
                    child: Text("Text Button"),
                  ),
                  IconButton(
                    icon: Icon(Icons.thumb_up),
                    onPressed: () {},
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }
    
    
        import 'package:flutter/material.dart';
    import 'dart:async';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: SplashScreen(),
        );
      }
    }
    
    class SplashScreen extends StatefulWidget {
      @override
      _SplashScreenState createState() => _SplashScreenState();
    }
    
    class _SplashScreenState extends State<SplashScreen> {
      double _opacity = 0.0; // Start invisible
      double _scale = 0.5; // Start small
    
      @override
      void initState() {
        super.initState();
    
        // Start the fade-in and scale animation
        Future.delayed(Duration(milliseconds: 250), () {
          setState(() {
            _opacity = 1.0; // Fully visible
            _scale = 1.2; // Slightly larger
          });
        });
    
        // Fade-out and navigate to Home after 3 seconds
        Timer(Duration(seconds: 1), () {
          setState(() {
            _opacity = 0.0; // Start fading out
          });
        });
    
        // Navigate after fade-out completes
        Timer(Duration(seconds: 2), () {
          Navigator.pushReplacement(
            context,
            MaterialPageRoute(builder: (_) => HomeScreen()),
          );
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: AnimatedOpacity(
              duration: Duration(seconds: 1),
              opacity: _opacity,
              child: TweenAnimationBuilder(
                duration: Duration(seconds: 1),
                tween: Tween<double>(begin: _scale, end: _scale),
                builder: (context, double scale, child) {
                  return Transform.scale(
                    scale: scale,
                    child: Imagework(
                      '.png', // Replace with your image URL
                      width: 250,
                      height: 250,
                    ),
                  );
                },
              ),
            ),
          ),
        );
      }
    }
    
    class HomeScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text("Home Screen")),
          body: Center(child: Text("Welcome to Home Screen")),
        );
      }
    }
    CircleAvatar(
      radius: 50,
      backgroundImage: AssetImage('assets/profile.jpg'),
    )
    
    
    showModalBottomSheet(
      context: context,
      builder: (context) {
        return Container(
          height: 200,
          child: Center(child: Text("This is a bottom sheet")),
        );
      },
    );
    
    
    Drawer(
      child: ListView(
        children: [
          DrawerHeader(child: Text("Menu")),
          ListTile(title: Text("Item 1"), onTap: () {}),
          ListTile(title: Text("Item 2"), onTap: () {}),
        ],
      ),
    )
    
    
    ReorderableListView(
      onReorder: (oldIndex, newIndex) {
        setState(() {
          final item = myList.removeAt(oldIndex);
          myList.insert(newIndex, item);
        });
      },
      children: List.generate(myList.length, (index) {
        return ListTile(
          key: ValueKey(myList[index]),
          title: Text(myList[index]),
        );
      }),
    )
    ReorderableListView(
      onReorder: (oldIndex, newIndex) {
        setState(() {
          final item = myList.removeAt(oldIndex);
          myList.insert(newIndex, item);
        });
      },
      children: List.generate(myList.length, (index) {
        return ListTile(
          key: ValueKey(myList[index]),
          title: Text(myList[index]),
        );
      }),
    )
    
    
    Checkbox(
      value: isChecked,
      onChanged: (value) {
        setState(() {
          isChecked = value!;
        });
      },
    )
    
    
    
    Dismissible(
      key: Key(myList[index]),
      onDismissed: (direction) {
        setState(() => myList.removeAt(index));
      },``
      background: Container(color: Colors.red),
      child: ListTile(title: Text(myList[index])),
    )

发布评论

评论列表(0)

  1. 暂无评论