Listview 调整平面按钮的大小,并在颤振中提供水平滚动选项
如果你能看到顶部,我已经创建了4列,带有一个平面按钮,包含一个图像和一个文本。我想给出扁平按钮的确切宽度和高度,这可能吗?此外,我想给一个水平滚动,以便我可以添加更多的列在同一行。此外,单击图像后,相应的列表应显示在下面。这是我的代码正常运行了Listview 调整平面按钮的大小,并在颤振中提供水平滚动选项,listview,button,scroll,horizontal-scrolling,flutter,Listview,Button,Scroll,Horizontal Scrolling,Flutter,如果你能看到顶部,我已经创建了4列,带有一个平面按钮,包含一个图像和一个文本。我想给出扁平按钮的确切宽度和高度,这可能吗?此外,我想给一个水平滚动,以便我可以添加更多的列在同一行。此外,单击图像后,相应的列表应显示在下面。这是我的代码正常运行了 import 'package:flutter/material.dart'; import 'widgets.dart'; void main() => runApp(new MaterialApp(
import 'package:flutter/material.dart';
import 'widgets.dart';
void main() => runApp(new MaterialApp(
title: 'Booze Up',
theme: new ThemeData(
primarySwatch: Colors.red,
),
home: new MyHomePage('Booze Up'),
routes: <String,WidgetBuilder>{
"/MyHomePage2": (BuildContext context) => new MyHome()
}
));
class MyHomePage extends StatelessWidget {
int counter =0;
String title;
MyHomePage(this.title);
@override
Widget build(BuildContext context) {
Text text = new Text("dev");
Text text2 = new Text("Sneha");
Text text3 = new Text("Prashant");
var pad = const EdgeInsets.all(10.0);
Padding pad1 = new Padding(child: text, padding:pad);
Padding pad2 = new Padding(child: text2, padding:pad);
Padding pad3 = new Padding(child: text3, padding:pad);
ListView listView = new ListView(children: <Widget>[new
Image.asset('images/party.jpg'),pad1,pad2,pad3]);
Drawer drawer = new Drawer(child:listView);
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return new Scaffold(
drawer: drawer,
appBar: new AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: new Text(title),
),
body:
new Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Icon(Icons.camera),
new Padding(padding: new EdgeInsets.fromLTRB(0.0,0.0, 5.0,0.0)),
new RaisedButton(
color: Colors.black,
child:
new Text( 'Party Home',
style: new TextStyle(fontSize: 20.0,fontStyle:
FontStyle.italic,color: Colors.white),
),
onPressed: () {
Navigator.of(context).pushNamed("/MyHomePage2");
},
)]),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Icon(Icons.favorite),
new Padding(padding: new EdgeInsets.fromLTRB(0.0,0.0, 5.0,0.0)),
new Text(
'Pub Hopping',
style: new TextStyle(fontSize: 20.0,fontStyle: FontStyle.italic),
),
]),
new Image.asset('images/party.jpg'),
],
)
,
),
floatingActionButton: new FloatingActionButton(
onPressed: null,
child: new Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class MyHome extends StatefulWidget
{
@override
State<StatefulWidget> createState() => new MyHomePage2();
}
class MyHomePage2 extends State<MyHome> {
List items = new List() ;
@override
Widget build(BuildContext context) {
var abc = MediaQuery.of(context).size;
print(abc.width);
var width = abc.width/4;
Text text = new Text("Dev");
Text text2 = new Text("Sneha");
Text text3 = new Text("Prashant");
Text text4 = new Text("Vikesh");
var pad = const EdgeInsets.all(10.0);
Padding pad1 = new Padding(child: text, padding:pad);
Padding pad2 = new Padding(child: text2, padding:pad);
Padding pad3 = new Padding(child: text3, padding:pad);
Padding pad4 = new Padding(child: text4, padding:pad);
ListView listView = new ListView(children: <Widget>[new
Image.asset('images/party.jpg'),pad1,pad2,pad3,pad4]);
ListView listView2 = new ListView(children: <Widget>[new Text("Jacob
Creek"),new Text("Sula red"),new Text("Yellow tale"),new
Text("Chenin Blanc")]);
Drawer drawer = new Drawer(child: listView);
return new Scaffold(
drawer: drawer,
appBar: new AppBar(
title: new Text('Booze Up'),
),
body:
new Column(
children: <Widget>[
new Row(
children: <Widget>[
new Column(
children: <Widget>[
new FlatButton(child: new
Image.asset('images/party.jpg',width:width,height:
width),onPressed: buildlist('button1'),),
new Text('Juice', style: new TextStyle(fontSize: 20.0,fontStyle:
FontStyle.italic)),
]),
new Column(
children: <Widget>[
new FlatButton(child: new
Image.asset('images/party.jpg',width:width,height: width),onPressed:
buildlist('button2')),
new Text('Cocktail', style: new TextStyle(fontSize:
20.0,fontStyle: FontStyle.italic)) ,
]),
new Column(
children: <Widget>[
new FlatButton(child: new Image.asset('images/party.jpg',width:width,height: width),onPressed: buildlist('button3')),
new Text('Moocktail', style: new TextStyle(fontSize: 20.0,fontStyle: FontStyle.italic)),
]),
new Column(
children: <Widget>[
new FlatButton(child: new Image.asset('images/party.jpg',width:width,height: width),onPressed: buildlist('button4')),
new Text('Juice', style: new TextStyle(fontSize: 20.0,fontStyle: FontStyle.italic)),
])
]),
new Expanded(
child: new ListView(
padding: new EdgeInsets.fromLTRB(10.0, 10.0, 0.0,10.0),
children: items,
),
)]
),
floatingActionButton: new FloatingActionButton(
onPressed: null,
child: new Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
buildlist(String s ) {
print("entered buildlist"+ s);
List refresh = new List();
if(s=='button1')
{
refresh = [
new Refreshments("Watermelon",250),
new Refreshments("Orange",275),
new Refreshments("Pine",300),
new Refreshments("Papaya",225),
new Refreshments("Apple",250),
];
}
else if(s=='button2')
{ refresh = [
new Refreshments("Pina Colada",250),
new Refreshments("Bloody Mary",275),
new Refreshments("Long Island Ice tea",300),
new Refreshments("Screwdriver",225),
new Refreshments("Fusion Cocktail",250),
];
}
else if(s=='button3')
{
refresh = [
new Refreshments("Virgin Pina Colada",250),
new Refreshments("Virgin Mary",275),
new Refreshments("Strawberry Flush",300),
new Refreshments("Mango Diver",225),
new Refreshments("Peach Delight",250),
];
}
else{
refresh = [
new Refreshments("Absolute",250),
new Refreshments("Smirnoff",275),
new Refreshments("White Mischief",300),
new Refreshments("Romanov",225),
new Refreshments("Blender's Pride",250),
];
}
for(var item in refresh)
{
items.add(new ItemsList(item));
}
}
}
class Refreshments
{
String name;
int price;
Refreshments(this.name,this.price);
}
class ItemsList extends StatelessWidget
{
final Refreshments refreshments;
ItemsList (this.refreshments);
@override
Widget build(BuildContext context) {
return new ListTile(
onTap: null,
title: new Text(refreshments.name),
);
}
}
导入“包装:颤振/材料.省道”;
导入“widgets.dart”;
void main()=>runApp(新材料应用(
标题:“豪饮”,
主题:新主题数据(
主样本:颜色。红色,
),
主页:新的MyHomePage(“酗酒”),
路线:{
“/MyHomePage2”:(BuildContext上下文)=>new MyHome()
}
));
类MyHomePage扩展了无状态小部件{
int计数器=0;
字符串标题;
我的主页(这个标题);
@凌驾
小部件构建(构建上下文){
文本文本=新文本(“开发”);
Text text2=新文本(“Sneha”);
Text text3=新文本(“Prashant”);
var pad=所有常数边集(10.0);
Padding pad1=新的填充(子项:文本,填充:pad);
Padding pad2=新的填充(子:text2,Padding:pad);
Padding pad3=新的填充(子:text3,Padding:pad);
ListView ListView=新建ListView(子项:[新建
Image.asset('images/party.jpg')、pad1、pad2、pad3]);
抽屉=新抽屉(子项:listView);
//每次调用setState时都会重新运行此方法,例如,按done
//通过上面的_incrementCounter方法。
//
//对颤振框架进行了优化,以制定重新运行的构建方法
//快速,这样你就可以重建任何需要更新的东西
//而不是必须单独更改小部件的实例。
归还新脚手架(
抽屉:抽屉,
appBar:新的appBar(
//在这里,我们从MyHomePage对象中获取由创建的值
//使用App.build方法,并使用它设置appbar标题。
标题:新文本(标题),
),
正文:
新中心(
//中心是一个布局小部件。它接受一个子元素并对其进行定位
/在父母的中间。
子:新列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
新行(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
新图标(图标。摄像头),
新填充(填充:来自LTRB(0.0,0.0,5.0,0.0)的新边集),
新升起的按钮(
颜色:颜色,黑色,
儿童:
新文本(‘派对主页’,
样式:新文本样式(fontSize:20.0,fontStyle:
字体样式。斜体,颜色:颜色。白色),
),
已按下:(){
Navigator.of(context.pushNamed(“/MyHomePage2”);
},
)]),
新行(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
新图标(Icons.favorite),
新填充(填充:来自LTRB(0.0,0.0,5.0,0.0)的新边集),
新文本(
“跳酒吧”,
样式:新文本样式(fontSize:20.0,fontStyle:fontStyle.italic),
),
]),
新建Image.asset('images/party.jpg'),
],
)
,
),
floatingActionButton:新的floatingActionButton(
onPressed:null,
子:新图标(Icons.add),
),//此尾随逗号使生成方法的自动格式设置更方便。
);
}
}
类MyHome扩展了StatefulWidget
{
@凌驾
State createState()=>新建MyHomePage2();
}
类MyHomePage2扩展了状态{
列表项=新列表();
@凌驾
小部件构建(构建上下文){
var abc=MediaQuery.of(context).size;
打印(abc宽度);
变量宽度=abc.width/4;
文本文本=新文本(“开发”);
Text text2=新文本(“Sneha”);
Text text3=新文本(“Prashant”);
文本文本4=新文本(“Vikesh”);
var pad=所有常数边集(10.0);
Padding pad1=新的填充(子项:文本,填充:pad);
Padding pad2=新的填充(子:text2,Padding:pad);
Padding pad3=新的填充(子:text3,Padding:pad);
Padding pad4=新的填充(子:text4,Padding:pad);
ListView ListView=新建ListView(子项:[新建
Image.asset('images/party.jpg')、pad1、pad2、pad3、pad4]);
ListView listView2=新建ListView(子项:[新建文本]
新文本(“苏拉红”)、新文本(“黄色故事”)、新
文本(“Chenin Blanc”)];
抽屉=新抽屉(子项:listView);
归还新脚手架(
抽屉:抽屉,
appBar:新的appBar(
标题:新文本(“酗酒”),
),
正文:
新专栏(
儿童:[
新行(
儿童:[
新专栏(
儿童:[
新建扁平按钮(子级:新建
Image.asset('images/party.jpg',宽度:宽度,高度:
宽度),按下时:buildlist('button1'),),
新文本(“Juice”,样式:新文本样式(fontSize:20.0,fontStyle:
FontStyle.italic),
]),
新专栏(
儿童:[
新建扁平按钮(子级:新建
Image.asset('images/party.jpg',宽度:宽度,高度:宽度),按下时:
构建列表(“按钮2”),
新文本(“鸡尾酒”,样式:新文本样式(字体大小:
20.0,fontStyle:fontStyle.italic),
]),
新专栏(
儿童:[
新建FlatButton(子项:new Image.asset('images/party.jpg',width:width,height:width),onPressed:buildlist('button3'),
新文本('Moocktail',样式:新文本样式(fontSize:20.0,fontStyle:fontStyle.italic)),
]),
新专栏(
儿童:[
新建FlatButton(子项:new Image.asset('images/party.jpg',width:width,height:width),onPressed:buildlist('button4'),
新文本('Juice',样式:新文本样式(fontSize:20.0,fontStyle:fontStyle.italic)),
])
]),
import "package:flutter/material.dart";
class LetsParty extends StatefulWidget {
@override
_LetsPartyState createState() => new _LetsPartyState();
}
class _LetsPartyState extends State<LetsParty> {
Image _partyImage = new Image.network(
"http://www.freshcardsgifts.co.uk/images/_lib/animal-party-greetings-card-3003237-0-1344698261000.jpg");
final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return new Scaffold(
key: scaffoldKey,
appBar: new AppBar(title: new Text("Let's Party"),),
body: new ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 10, //add the length of your list here
itemBuilder: (BuildContext context, int index) {
return new Column(
children: <Widget>[
///Flexible let's your widget flex in the given space, dealing with the overflow you have
// new Flexible(child: new FlatButton(onPressed: ()=>print("You pressed Image No.$index"), child: _partyImage)),
new Container(child: new FlatButton(onPressed: () =>
scaffoldKey.currentState.showSnackBar(new SnackBar(
content: new Text("You pressed Image No.$index"))),
child: _partyImage),
width: 100.0, height: 100.0,),
//Exact width and height, consider adding Flexible as a parent to the Container
new Text("Text$index")
],
);
}),
);
}
}