0

I am working on a vpn app and on one of the screens i have to use stack for showing different widgtes over each other like gradient below and a globe svg on top and then a circularProgressIndicator on top of all. i have used media query to set the widgets but when I make apk and test on separate device the circular progress indicator just increases in length or width or both looking ugly. i have tried to use it with sized box and container but in vain. Any suggestion or best Solution to tackle such widgets in stack that whatever devixe they're on they should be prtoperly positioned same.

Here's my code,

    import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:vpnapp/Widgets/Connection_overlay.dart';

class ConnectionsScreen extends StatefulWidget {
  const ConnectionsScreen({super.key});

  @override
  State<StatefulWidget> createState() {
    return _ConnectionsScreenState();
  }
}

class _ConnectionsScreenState extends State<ConnectionsScreen>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 1),
      vsync: this,
    )..repeat(reverse: true);

    _animation = Tween<double>(
      begin: 0.99,
      end: 1.03,
    ).animate(CurvedAnimation(parent: _controller, curve: Curves.easeInCubic));
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(context) {
    final screenHeight = MediaQuery.of(context).size.height;
    final screenWidth = MediaQuery.of(context).size.width;
    return Stack(
      children: [
        Positioned(
          bottom: screenHeight * 0.26,
          top: screenHeight * 0.27,
          left: screenWidth * 0.17,
          right: screenWidth * 0.17,
          child: FractionallySizedBox(
            widthFactor: 1,
            heightFactor: 1,
            child: AnimatedBuilder(
              animation: _controller,
              builder: (ctx, child) {
                return Transform.scale(scale: _animation.value, child: child);
              },
              child: Container(
                decoration: BoxDecoration(
                  gradient: RadialGradient(
                    colors: [
                      const Color.fromARGB(130, 72, 217, 186),
                      const Color.fromARGB(122, 72, 217, 186),
                      const Color.fromARGB(112, 72, 217, 186),
                      const Color.fromARGB(103, 72, 217, 186),
                      const Color.fromARGB(93, 72, 217, 186),

                      const Color.fromARGB(0, 255, 255, 255),
                    ],
                    center: Alignment.center,
                  ),
                ),
              ),
            ),
          ),
        ),
        Positioned(
          bottom: screenHeight * 0.26,
          top: screenHeight * 0.29,
          left: screenWidth * 0.17,
          right: screenWidth * 0.17,
          child: FractionallySizedBox(
            widthFactor: 1,
            heightFactor: 1,
            child: AnimatedBuilder(
              animation: _controller,
              builder: (ctx, child) {
                return Transform.scale(scale: _animation.value, child: child);
              },
              child: SvgPicture.asset('assets/DISCONNECT/globe.svg'),
            ),
          ),
        ),
        Positioned(
          bottom: screenHeight * 0.27,
          top: screenHeight * 0.272,
          left: screenWidth * 0.17,
          right: screenWidth * 0.17,
          child: FractionallySizedBox(
            widthFactor: 0.78,
            heightFactor: 0.78,
            child: CircularProgressIndicator(color: Colors.red),
          ),
        ),
      ],
    );
  }
}
1
  • Try using Align widget instead of Positioned Commented Jul 27 at 6:44

1 Answer 1

0

Problem Resolved!

I used a sized box of specific width and height that i think almost every android device will be compatible of and designed my stack init with align and the problem got solved

 @override
  Widget build(context) {
    return SizedBox(
      height: 900,
      width: 500,
      child: Stack(
        children: [
          Align(
            alignment: Alignment.center,

            child: AnimatedBuilder(
              animation: _controller,
              builder: (ctx, child) {
                return Transform.scale(scale: _animation.value, child: child);
              },
              child: Container(
                height: 230,
                width: 230,
                decoration: BoxDecoration(
                  gradient: RadialGradient(
                    colors: [
                      const Color.fromARGB(130, 72, 217, 186),
                      const Color.fromARGB(122, 72, 217, 186),
                      const Color.fromARGB(112, 72, 217, 186),
                      const Color.fromARGB(116, 72, 217, 186),
                      const Color.fromARGB(110, 72, 217, 186),

                      const Color.fromARGB(0, 255, 255, 255),
                    ],
                    center: Alignment.center,
                  ),
                ),
              ),
            ),
          ),

          Align(
            alignment: Alignment.center,

            child: AnimatedBuilder(
              animation: _controller,
              builder: (ctx, child) {
                return Transform.scale(scale: _animation.value, child: child);
              },
              child: SvgPicture.asset('assets/DISCONNECT/globe.svg'),
            ),
          ),
          Consumer(
            builder: (context, ref, child) {
              final isActive = ref.watch(isAnyCountryConnectedProvider);
              final delayeConnectedTime = ref.watch(connectionTimerProvider);
              return isActive && delayeConnectedTime == false
                  ? _progressIndicator
                  : Align();
            },
          ),

          Consumer(
            builder: (context, ref, child) {
              final isActive = ref.watch(isAnyCountryConnectedProvider);
              final delayeConnectedTime = ref.watch(connectionTimerProvider);

              return AnimatedPositioned(
                duration: const Duration(milliseconds: 600),
                curve: Curves.easeInOutCubic,
                top: isActive && delayeConnectedTime == true ? 295 : 270,
                bottom: isActive && delayeConnectedTime == true ? 288 : 270,
                left: isActive && delayeConnectedTime == true ? 180 : 60,
                right: isActive && delayeConnectedTime == true ? 50 : 60,
                child: SvgPicture.asset('assets/DISCONNECT/pin.svg'),
              );
            },
          ),
          Consumer(
            builder: (context, ref, child) {
              final isActive = ref.watch(isAnyCountryConnectedProvider);
              final delayeConnectedTime = ref.watch(connectionTimerProvider);
              final mess =
                  delayeConnectedTime == true
                      ? MessBarConnectedState()
                      : MessBar();
              return Positioned(
                top: 320,
                bottom: 235,
                left: 235,
                right: 10,
                child: isActive ? mess : Align(),
              );
            },
          ),
          Consumer(
            builder: (context, ref, child) {
              final delayeConnectedTime = ref.watch(connectionTimerProvider);
              final isActive = ref.watch(isAnyCountryConnectedProvider);
              Widget content =
                  delayeConnectedTime == true
                      ? Connectedstate(
                        text: '',
                        button: CancelDisconnectButton(disconnect: disConnect),
                      )
                      : ConnectinState();
              return Align(
                alignment: Alignment.center,
                child: isActive ? content : InitialState(),
              );
            },
          ),
        ],
      ),
    );
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.