简体   繁体   English

flutter 旋转木马 slider 带文字

[英]flutter carousel slider with text

class my_page extends StatefulWidget {
  @override
  _my_pageState createState() => _my_pageState();
}

class _my_pageState extends State<my_page> {

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('mypage'),),
      body: CarouselSlider(
        autoPlay: true,
        items:['assets/images/0.jpg','assets/images/1.jpg',
            'assets/images/2.jpg',
          'assets/images/3.jpg',
          'assets/images/4.jpg',
          'assets/images/5.jpg'].map((i){
          return Builder(
            builder:(BuildContext context){
             return Container(

                width: MediaQuery.of(context).size.width,
               child: GestureDetector(
                child:Stack(
                  children: <Widget>[
                    Image.asset(i,fit: BoxFit.fill,),
                    Align(
                      alignment: Alignment.topCenter,
                        child: Text('foodname',style: TextStyle(fontSize: 30,color: Colors.white,fontWeight: FontWeight.bold),)
                    )
                  ],
                ),
                 onTap: (){
                   Navigator.push(
                     context,
                     MaterialPageRoute(builder: (context) => HomePage()),
                   );
                 },
               ),
              );
            }
          );
        }).toList()
      ),
    );
  }
}

this is my code.这是我的代码。 I want to add text on each image.我想在每张图片上添加文字。 the text food name should be changed.应更改文本食物名称。 Is there some way to do it?有什么办法吗? It is in stack form and text needs to be on the image.它是堆栈形式,文本需要在图像上。 for 1st image 'burger' for 2nd image 'pizza' and so on.对于第一张图片“汉堡”,对于第二张图片“披萨”,依此类推。

please help me guys请帮帮我

Create List of List:创建列表列表:

List myList = [['image url', 'burger'], [''image', 'pizza'] , ..etc];

Now u can use them:现在你可以使用它们了:

myList.map((i){
      return Builder(
        builder:(BuildContext context){
         return Container(
            width: MediaQuery.of(context).size.width,
           child: GestureDetector(
            child:Stack(
              children: <Widget>[
                Image.asset(i[0],fit: BoxFit.fill,),
                Align(
                  alignment: Alignment.topCenter,
                    child: Text(i[1],style: TextStyle(fontSize: 30,color: Colors.white,fontWeight: FontWeight.bold),)
                )
              ],
            ),
             onTap: (){
               Navigator.push(
                 context,
                 MaterialPageRoute(builder: (context) => HomePage()),
               );
             },
           ),
          );
        }

create a model list and assign values for image and text from the model list创建 model 列表并为 model 列表中的图像和文本分配值

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: 
          my_page(),

      ),
    );
  }
}
class MyPageItem{
  String itemName;
  String path;
  MyPageItem(this.itemName,this.path);
}
class my_page extends StatefulWidget {
  @override
  _my_pageState createState() => _my_pageState();
}

class _my_pageState extends State<my_page> {
  List<MyPageItem> items=[
    MyPageItem("item 1",'assets/images/1.jpg'),
    MyPageItem("item 2",'assets/images/2.jpg'),
    MyPageItem("item 3",'assets/images/3.jpg'),
    MyPageItem("item 4",'assets/images/4.jpg'),
    MyPageItem("item 5",'assets/images/5.jpg'),


  ];

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('mypage'),),
      body: CarouselSlider(
        autoPlay: true,
        items:items.map((i){
          return Builder(
            builder:(BuildContext context){
             return Container(

                width: MediaQuery.of(context).size.width,
               child: GestureDetector(
                child:Stack(
                  children: <Widget>[
                    Image.asset(i.path,fit: BoxFit.fill,),
                    Align(
                      alignment: Alignment.topCenter,
                        child: Text(i.itemName,style: TextStyle(fontSize: 30,color: Colors.white,fontWeight: FontWeight.bold),)
                    )
                  ],
                ),
                 onTap: (){
                   Navigator.push(
                     context,
                     MaterialPageRoute(builder: (context) => HomePage()),
                   );
                 },
               ),
              );
            }
          );
        }).toList()
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM