Flutter - 单击时呈现新的小部件
Flutter - Render new Widgets on click
标题基本上说明了一切。一个非常愚蠢的问题......我有这个基本代码来创建我的应用程序的初始状态:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Some title'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text(config.title)),
body: new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
new InputWidget(),
]
),
);
}
}
现在,我如何在用户单击按钮时呈现新的 Widget?假设我想实例化另一个 InputWidget。
谢谢
您的构建函数是 "just code",因此您可以使用类似
的方法动态构建传递给列的数组
var children = [];
children.add(new InputWidget());
if (_showAnother)
children.add(new InputWidget());
...
body: new Column(
...
children: children,
...
...其中 _showAnother
是您在点击按钮时设置的某种布尔字段。
希望我能正确理解你的问题...
我认为要点是您不应该考虑 "another" 小部件 - 如果您将 MyHomePage
的内容从第一个 child 然后两个更改为您不真的 保留 第一个 child 然后 添加另一个 child。你只需先说 "I want one child" 然后你改变主意说 "I want two children".
在您的代码中,您通过在 _MyHomePageState
中调用 setState
来完成此操作。 Flutter 负责保留第一个并添加第二个 child.
import 'dart:core';
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Some title'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int count = 1;
@override
Widget build(BuildContext context) {
List<Widget> children = new List.generate(count, (int i) => new InputWidget(i));
return new Scaffold(
appBar: new AppBar(title: new Text(widget.title)),
body: new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: children
),
floatingActionButton: new FloatingActionButton(
child: new Icon(Icons.add),
onPressed: () {
setState(() {
count = count + 1;
});
},
)
);
}
}
class InputWidget extends StatelessWidget {
final int index;
InputWidget(this.index);
@override
Widget build(BuildContext context) {
return new Text("InputWidget: " + index.toString());
}
}
你是这个意思吗?
标题基本上说明了一切。一个非常愚蠢的问题......我有这个基本代码来创建我的应用程序的初始状态:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Some title'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text(config.title)),
body: new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
new InputWidget(),
]
),
);
}
}
现在,我如何在用户单击按钮时呈现新的 Widget?假设我想实例化另一个 InputWidget。
谢谢
您的构建函数是 "just code",因此您可以使用类似
的方法动态构建传递给列的数组var children = [];
children.add(new InputWidget());
if (_showAnother)
children.add(new InputWidget());
...
body: new Column(
...
children: children,
...
...其中 _showAnother
是您在点击按钮时设置的某种布尔字段。
希望我能正确理解你的问题...
我认为要点是您不应该考虑 "another" 小部件 - 如果您将 MyHomePage
的内容从第一个 child 然后两个更改为您不真的 保留 第一个 child 然后 添加另一个 child。你只需先说 "I want one child" 然后你改变主意说 "I want two children".
在您的代码中,您通过在 _MyHomePageState
中调用 setState
来完成此操作。 Flutter 负责保留第一个并添加第二个 child.
import 'dart:core';
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Some title'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int count = 1;
@override
Widget build(BuildContext context) {
List<Widget> children = new List.generate(count, (int i) => new InputWidget(i));
return new Scaffold(
appBar: new AppBar(title: new Text(widget.title)),
body: new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: children
),
floatingActionButton: new FloatingActionButton(
child: new Icon(Icons.add),
onPressed: () {
setState(() {
count = count + 1;
});
},
)
);
}
}
class InputWidget extends StatelessWidget {
final int index;
InputWidget(this.index);
@override
Widget build(BuildContext context) {
return new Text("InputWidget: " + index.toString());
}
}
你是这个意思吗?