How to Prevent Stack Children from Being Overwritten in Flutter?

Tired of UI elements being overwritten in your Flutter app when using Stack? Learn how to prevent children from overlapping each other with this simple solution. Click to find out more.

Steps to Prevent Stack Children in Flutter

If you facing the problem by using stack , means you need to follow proper guide of the stack. Means if you passing children insiding stack you have to wrap child with positioned widgets.

How to Prevent Stack Children from Being Overwritten in Flutter

Issue on code by using stack

import 'package:flutter/material.dart';


void main() {

 runApp(const MyApp());

}


class MyApp extends StatelessWidget {

 const MyApp({super.key});


 // This widget is the root of your application.

 @override

 Widget build(BuildContext context) {

   return MaterialApp(

     debugShowCheckedModeBanner: false,

     title: 'Flutter Demo',

     theme: ThemeData(

       primarySwatch: Colors.blue,

     ),

     home: const MyHomePage(title: 'Flutter Demo Home Page'),

   );

 }

}


class MyHomePage extends StatefulWidget {

 const MyHomePage({super.key, required this.title});


 final String title;


 @override

 State<MyHomePage> createState() => _MyHomePageState();

}


class _MyHomePageState extends State<MyHomePage> {

 @override

 Widget build(BuildContext context) {

   return Scaffold(

     appBar: AppBar(

       title: Text(widget.title),

     ),

     body:  Stack(

       children:const [

         Text(

           'Hello world !',

         ),

         Text(

           'How are you ? ',

         ),

       ]

     ), // This trailing comma makes auto-formatting nicer for build methods.

   );

 }

}

Check Out: [Solved] RenderBox was not laid out in Flutter

Solution

import 'package:flutter/material.dart';


void main() {

 runApp(const MyApp());

}


class MyApp extends StatelessWidget {

 const MyApp({super.key});


 // This widget is the root of your application.

 @override

 Widget build(BuildContext context) {

   return MaterialApp(

     debugShowCheckedModeBanner: false,

     title: 'Flutter Demo',

     theme: ThemeData(

       primarySwatch: Colors.blue,

     ),

     home: const MyHomePage(title: 'Flutter Demo Home Page'),

   );

 }

}


class MyHomePage extends StatefulWidget {

 const MyHomePage({super.key, required this.title});


 final String title;


 @override

 State<MyHomePage> createState() => _MyHomePageState();

}


class _MyHomePageState extends State<MyHomePage> {

 @override

 Widget build(BuildContext context) {

   return Scaffold(

     appBar: AppBar(

       title: Text(widget.title),

     ),

     body: Stack(

       children: const [

         Positioned(

           left: 10,

           top: 10,

           child: Text(

             'Hello world !',

           ),

         ),

         Positioned(

           left: 10,

           top: 40,

           child: Text(

             'How are you ? ',

           ),

         ),

       ],

     ), // This trailing comma makes auto-formatting nicer for build methods.

   );

 }

}
Share:

Ambika Dulal is a Flutter developer from Nepal who is passionate about building beautiful and user-friendly apps. She is always looking for new challenges and is eager to learn new things. She is also a strong believer in giving back to the community and is always willing to help others.

Leave a Comment

AO Logo

App Override is a leading mobile app development company based in Kathmandu, Nepal. Specializing in both Android and iOS app development, the company has established a strong reputation for delivering high-quality and innovative mobile solutions to clients across a range of industries.

Services

UI/UX Design

Custom App Development

Mobile Strategy Consulting

App Testing and Quality Assurance

App Maintenance and Support

App Marketing and Promotion

Contact

App Override

New Plaza, Kathmandu