Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add text along with carousel images in flutter?

I tried to add carousel along with text. But As far I can only add image carousel, Have no idea on how to add text. Please help me. Totally new. I expect the output to be like image on above and text on below, but both need to swipe at same time.

I have no idea where to add the text field. I made this carousel with a example in youtube. But no example for carousel images with text. I tried something manually, But it all doesn't ended much well. Please help me fix it. Thank-you

  class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(title: 'Slider'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  PageController pageController;

  //Images List
  List<String> images = [
    '',
  ];

  @override
  void initState() {
    super.initState();
    pageController = PageController(initialPage: 1, viewportFraction: 0.6);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: PageView.builder(
            controller: pageController,
            itemCount: images.length,
            itemBuilder: (context, position) {
              return imageSlider(position);
            }
        )
    );
  }

  imageSlider(int index) {
    return AnimatedBuilder(
        animation: pageController,
        builder: (context, widget) {
          double value = 1;
          if(pageController.position.haveDimensions){
            value = pageController.page - index;
            value = (1 - (value.abs() * 0.3.clamp(0.0, 1.0)));
          }
          return Center(
            child: SizedBox(
              height: Curves.easeInOut.transform(value) * 400.0,
              width: Curves.easeInOut.transform(value) * 350.0,
              child: widget,
            ),
          );
        },
      child: Container(
        margin: EdgeInsets.all(10.0),
        child: Image.asset(images[index],fit: BoxFit.cover),
      ),
    );
  }
}

enter image description here

like image 580
Ranto Berk Avatar asked Dec 27 '25 16:12

Ranto Berk


1 Answers

Use column instead of container, so just replace this:

child: Container(
    margin: EdgeInsets.all(10.0),
    child: Image.asset(images[index],fit: BoxFit.cover),
),

with this:

child: Column(
    // To centralize the children.
    mainAxisAlignment: MainAxisAlignment.center,
    crossAxisAlignment: CrossAxisAlignment.center,
    children: <Widget>[
        // First child
        Container(
          margin: EdgeInsets.all(10.0),
          child: Image.asset(images[index],fit: BoxFit.cover),
        ),
        // Second child
        Text(
          'foo',
          style: TextStyle(
             // Add text's style here
          ),
        ),
    ]
),

OR instead of building your own carousal, you can use a ready to use one, e.g: carousel_slider or flutter_swiper

like image 166
Jehad Nasser Avatar answered Dec 31 '25 19:12

Jehad Nasser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!