flutter - 如何在我的应用程序中添加新的英雄页面?
问题描述
在我的设备页面上的应用程序中,我目前有 2 个示例设备(作为英雄小部件),但最后我希望用户能够添加和删除设备。所以我添加了一个浮动操作按钮来添加一个新设备(第一张图片)。当它被按下时,应该会弹出一个字段来输入名称。选择名称后,新设备应在设备(英雄)页面上可见。如果它被选中,我想进入第二张图片上的设备页面。
如果有人知道我怎么能意识到我会非常感激!
英雄设备页面代码:
class DevicesPageHero extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: color_1,
title: Text('Devices'),
),
body: Center(
child:Row(
children: [
Hero(
tag: 'matrix1',
child: GestureDetector(
onTap: () => Navigator.push(context, MaterialPageRoute(builder: (context) => MatrixPageOne())),
child: Column(
children: <Widget>[
Image( image: new AssetImage('imgs/matrix1.png'),
height: 100,
width: 100,),
Text("Matrix Kitchen", style: mytextStyle,),
]
)
),
),
Hero(
tag: 'matrix2',
child: GestureDetector(
onTap: () => Navigator.push(context, MaterialPageRoute(builder: (context) => MatrixPageTwo())),
child: Column(
children: <Widget>[
Image( image: new AssetImage('imgs/matrix2.png'),
height: 100,
width: 100,),
Text("PARTY ROOM", style: mytextStyle,),
]
)
),
),
] // wrap children
)
),
floatingActionButton: FloatingActionButton(
onPressed: () {
// Create new matrix page
},
child: Icon(Icons.add),
backgroundColor: color_3,
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat
);
}
}
所选设备页面的代码
class MatrixPageOne extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: color_1,
title: Text('Matrix Kitchen'),
),
body: Column(
children: [
Hero(
tag: 'matrix1',
child:Image( image: new AssetImage('imgs/matrix1.png'),
height: 150,
width: 150
)
),
Container(
width: MediaQuery.of(context).size.width, // returns Screen width
child: Text(" Live update Matrix Kitchen", style: TextStyle( fontSize: 15, fontFamily: "Arial", fontWeight: FontWeight.bold) ,/*textAlign: TextAlign.left*/),
),
Divider(),
Spacer(flex: 1),
Container(
width: MediaQuery.of(context).size.width,
child:Text(" Option 1 ...", textAlign: TextAlign.left,),
),
Divider(),
Spacer(flex: 1),
Container(
width: MediaQuery.of(context).size.width,
child:Text(" Option 2 ...", textAlign: TextAlign.left,),
),
Divider(),
Spacer(flex: 80),
IconButton(
icon: Icon(Icons.delete, color: Colors.red,),
onPressed: (){
//Delete Hero Page
}
)
], // Column children
),
);
}
}
解决方案
您可以创建一个新的单个页面,然后将 tagName 和 Image 等参数发送到该页面。在该页面上,您可以根据自己的喜好显示以前发送的数据。
例如:
..
onTap: () => Navigator.push(context, MaterialPageRoute(builder: (context) => MatrixPageTwo(
'tagName': 'matrix2',
'image': new AssetImage('imgs/matrix2.png'), height: 100, width: 100)
))),
..
然后,在 MatrixPageTwo 页面上:
class MatrixPageTwo extends StatefulWidget {
const MatrixPageTwo({ Key key, this.image, this.tagName }) : super(key: key);
final Image image;
final String tagName;
@override
_MatrixPageTwoState createState() => _MatrixPageTwoState();
}
class _MatrixPageTwoState extends State<MatrixPageTwo> {
@override
Widget build(BuildContext context) {
return Center(
child: Text(widget.tagName),
);
}
}
推荐阅读
- sugarcrm - 如何取消合并 SugarCRM 中的编辑和删除按钮?
- asp.net-mvc - ASP.NET MVC API - 通过电子邮件 ID 获取记录
- c++ - 绕轴旋转矢量时得到错误的值
- php - 在 Laravel 中唯一的员工 ID 号
- html - 弹性容器属性是否永远不会应用于其中的弹性容器的子级?
- c++ - 全新的 C++ 和如何计算系列产品有点卡住。(下面给出的说明)
- ruby-on-rails - Rspec capybara Selenium::WebDriver::Error::ElementNotInteractableError 无法滚动到视图中
- regex - 内容正则表达式检查所有字符是否都是小写
- python - 私有 GitHub 的 repos + SSH + pip = 头疼
- javascript - Electron - 在主窗口和 BrowserVIew() 中加载内容时,主窗口内容可以与视图重叠吗?