Flutter 带文本的颤振转盘滑块 类我的页面扩展了StatefulWidget{ @凌驾 _my_pageState createState()=>\u my_pageState(); } 类_我的页面状态扩展状态{ @凌驾 小部件构建(构建上下文){ 返回脚手架( appBar:appBar(标题:文本('mypage'),), 主体:旋转滑翔机( 自动播放:对, 项目:['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){ 返回生成器( 生成器:(BuildContext上下文){ 返回容器( 宽度:MediaQuery.of(context).size.width, 儿童:手势检测器( 子:堆栈( 儿童:[ Image.asset(i,fit:BoxFit.fill,), 对齐( 对齐:alignment.topCenter, 子项:文本('foodname',样式:TextStyle(fontSize:30,颜色:Colors.white,fontWeight:fontWeight.bold),) ) ], ), onTap:(){ 导航器。推( 上下文 MaterialPackageRoute(生成器:(上下文)=>HomePage()), ); }, ), ); } ); })托利斯先生() ), ); } }
这是我的密码。我想在每个图像上添加文本。应更改文本“食品名称”。有什么办法吗?它是堆栈形式,文本需要在图像上。第一张图片是“汉堡”,第二张图片是“比萨饼”,依此类推 请帮我创建列表:Flutter 带文本的颤振转盘滑块 类我的页面扩展了StatefulWidget{ @凌驾 _my_pageState createState()=>\u my_pageState(); } 类_我的页面状态扩展状态{ @凌驾 小部件构建(构建上下文){ 返回脚手架( appBar:appBar(标题:文本('mypage'),), 主体:旋转滑翔机( 自动播放:对, 项目:['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){ 返回生成器( 生成器:(BuildContext上下文){ 返回容器( 宽度:MediaQuery.of(context).size.width, 儿童:手势检测器( 子:堆栈( 儿童:[ Image.asset(i,fit:BoxFit.fill,), 对齐( 对齐:alignment.topCenter, 子项:文本('foodname',样式:TextStyle(fontSize:30,颜色:Colors.white,fontWeight:fontWeight.bold),) ) ], ), onTap:(){ 导航器。推( 上下文 MaterialPackageRoute(生成器:(上下文)=>HomePage()), ); }, ), ); } ); })托利斯先生() ), ); } },flutter,Flutter,这是我的密码。我想在每个图像上添加文本。应更改文本“食品名称”。有什么办法吗?它是堆栈形式,文本需要在图像上。第一张图片是“汉堡”,第二张图片是“比萨饼”,依此类推 请帮我创建列表: class my_page extends StatefulWidget { @override _my_pageState createState() => _my_pageState(); } class _my_pageState extends State<my_page> {
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()
),
);
}
}
现在你可以使用它们了:
List myList = [['image url', 'burger'], [''image', 'pizza'] , ..etc];
myList.map((i){
返回生成器(
生成器:(BuildContext上下文){
返回容器(
宽度:MediaQuery.of(context).size.width,
儿童:手势检测器(
子:堆栈(
儿童:[
Image.asset(i[0],fit:BoxFit.fill,),
对齐(
对齐:alignment.topCenter,
子项:文本(i[1],样式:TextStyle(fontSize:30,颜色:Colors.white,fontWeight:fontWeight.bold),)
)
],
),
onTap:(){
导航器。推(
上下文
MaterialPackageRoute(生成器:(上下文)=>HomePage()),
);
},
),
);
}
创建模型列表,并为模型列表中的图像和文本指定值
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()),
);
},
),
);
}
导入“包装:颤振/材料.省道”;
最终颜色深蓝色=颜色。来自argb(255,18,32,47);
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
主题:ThemeData.dark().copyWith(脚手架背景颜色:深蓝色),
debugShowCheckedModeBanner:false,
家:脚手架(
正文:
我的页面(),
),
);
}
}
类MyPageItem{
字符串itemName;
字符串路径;
MyPageItem(this.itemName,this.path);
}
类my_页面扩展StatefulWidget{
@凌驾
_my_pageState createState()=>\u my_pageState();
}
类_我的页面状态扩展状态{
清单项目=[
MyPageItem(“项目1”,“资产/图像/1.jpg”),
MyPageItem(“项目2”,“资产/图像/2.jpg”),
MyPageItem(“项目3”,“资产/图像/3.jpg”),
MyPageItem(“第4项”,“资产/图像/4.jpg”),
MyPageItem(“项目5”,“资产/图像/5.jpg”),
];
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:文本('mypage'),),
主体:旋转滑翔机(
自动播放:对,
items:items.map((i){
返回生成器(
生成器:(BuildContext上下文){
返回容器(
宽度:MediaQuery.of(context).size.width,
儿童:手势检测器(
子:堆栈(
儿童:[
Image.asset(i.path,fit:BoxFit.fill,),
对齐(
对齐:alignment.topCenter,
子项:文本(i.itemName,样式:TextStyle(fontSize:30,颜色:Colors.white,fontWeight:fontWeight.bold),)
)
],
),
onTap:(){
导航器。推(
上下文
MaterialPackageRoute(生成器:(上下文)=>HomePage()),
);
},
),
);
}
);
})托利斯先生()
),
);
}
}
这可能会有帮助哦,我的上帝……你是上帝吗???这很有效,你是一个了不起的人我试过了,但是i.path和i.itemname给了我一个错误。你能附加日志吗
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()
),
);
}
}