Home > Enterprise >  flutter background image carousel
flutter background image carousel

Time:02-18

I'm new to flutter and working on developing my flutter coding skills. I want to display 3 background images in a carousel while other texts and button remain same on the screen. in the below code, image carousel is working properly but my Get Started button and other texts not displaying at all. what should I do for correct this issue. appreciate your help on this.

import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Test extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.leanBack);
    return Scaffold(
      body: ListView(
        children: [
          CarouselSlider(
            items: [
              //1st Image of Slider
              Container(
                color: Colors.black,
                child: Stack(
                  children: [
                    Positioned.fill(
                      child: Opacity(
                        opacity: 0.8,
                        child: Image.asset('assets/images/person1.png',
                            fit: BoxFit.cover),
                      ),
                    ),
                    Container(
                      color: Color.fromRGBO(5, 65, 196, 0.19),
                    ),

                  ],
                ),
              ),
              //2nd Image of Slider
              Container(
                color: Colors.black,
                child: Stack(
                  children: [
                    Positioned.fill(
                      child: Opacity(
                        opacity: 0.8,
                        child: Image.asset('assets/images/person2.png',
                            fit: BoxFit.cover),
                      ),
                    ),
                    Container(
                      color: Color.fromRGBO(5, 65, 196, 0.19),
                    ),

                  ],
                ),
              ),
              //3rd Image of Slider
              Container(
                color: Colors.black,
                child: Stack(
                  children: [
                    Positioned.fill(
                      child: Opacity(
                        opacity: 0.8,
                        child: Image.asset('assets/images/person3.png',
                            fit: BoxFit.cover),
                      ),
                    ),
                    Container(
                      color: Color.fromRGBO(5, 65, 196, 0.19),
                    ),

                  ],
                ),
              ),
            ],

            //Slider Container properties
            options: CarouselOptions(
              height: 810.0,
              enlargeCenterPage: true,
              autoPlay: true,
              aspectRatio: 16 / 16,
              autoPlayCurve: Curves.fastOutSlowIn,
              enableInfiniteScroll: true,
              autoPlayAnimationDuration: Duration(milliseconds: 800),
              viewportFraction: 1,
            ),
          ),


          Padding(
            padding: const EdgeInsets.only(left: 40, bottom: 230),
            child: Align(
                alignment: Alignment.bottomLeft,
                child: Text('Meet \nYour Doctor\nHere',
                    style: TextStyle(
                      height: 1.2,
                      fontFamily: 'Dubai',
                      fontSize: 35,
                      color: Color(0xffFAFAFA),
                      fontWeight: FontWeight.w500,
                    ))),
          ),


          Padding(
            padding: const EdgeInsets.only(left: 30, top: 26),
            child: Align(
                alignment: Alignment.topLeft,
                child: Text('DOCTOR',
                    style: TextStyle(
                      fontFamily: 'Dubai',
                      fontSize: 30,
                      color: Colors.white,
                      fontWeight: FontWeight.w500,
                    ))),
          ),
          Padding(
            padding: const EdgeInsets.only(bottom: 60),
            child: Align(
                alignment: Alignment.bottomCenter,
                child: SizedBox(
                  width: 320,
                  height: 65,
                  child: ElevatedButton(
                      onPressed: () {},
                      child: Text(
                        'Get Started',
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 25,
                            fontWeight: FontWeight.normal),
                      ),
                      style: ButtonStyle(
                          backgroundColor:
                          MaterialStateProperty.all<Color>(
                              Color(0xff05ABA3)),
                          shape: MaterialStateProperty.all<
                              RoundedRectangleBorder>(
                            RoundedRectangleBorder(
                              borderRadius:
                              BorderRadius.circular(50.0),
                            ),
                          ))),
                )),
          ),
          Padding(
              padding: const EdgeInsets.only(bottom: 20),
              child: Align(
                  alignment: Alignment.bottomCenter,
                  child: Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(5.0),
                      color: Colors.white,
                    ),
                    height: 5,
                    width: 120,
                  ))),
        ],
      ),
    );
  }
}

CodePudding user response:

you can set image in background using BoxDecoration in container-

Container(
  height: 120.0,
  width: 120.0,
  decoration: BoxDecoration(
    image: DecorationImage(
      image: AssetImage(
          'imagename'),
      fit: BoxFit.fill,
    ),
    shape: BoxShape.circle,
  ),
)

CodePudding user response:

On your Scaffold's body: you are using ListView. You can still find those widgets by scrolling down.

For your case, you like to have text and button widgets over the UI.

You need to replace ListView with Stack on body.

return Scaffold(
  body: Stack( // this
    children: [
      CarouselSlider

More about Stack and ListView.

  • Related