【发布时间】:2018-08-18 17:07:25
【问题描述】:
我正在创建一个基本的 Material 应用程序,并且我有一个用于导航的抽屉。 通过推送路线的简单方法,整个小部件将被替换,这就像打开一个全新的页面,其中包括一个全新的抽屉。 我的目标是营造页面和抽屉的氛围,当用户点击抽屉项目时,抽屉会折叠,只有页面的内容会被替换。
我找到了这两个问题/答案:
- Replace initial Route in MaterialApp without animation?
- Flutter Drawer Widget - change Scaffold.body content
我的问题是实现我想要做的最好/正确的方法是什么?
第一种方法只是通过删除push/pop 动画来创建错觉,尽管它实际上仍然像我描述的原始方法一样。
第二种方法实际上只是替换了内容,我想到的解决方案是代替更改文本来创建多个Container 小部件并在它们之间进行更改。
由于我还是新手并且正在学习 Flutter,我想知道这样做的正确做法是什么。
编辑: 我创建了this,效果很好。我仍然不知道它的有效性/效率,但现在这正是我想要实现的目标:
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blueGrey,
),
home: new TestPage(),
);
}
}
class TestPage extends StatefulWidget {
@override
_TestPageState createState() => new _TestPageState();
}
class _TestPageState extends State<TestPage> {
static final Container info = new Container(
child: new Center(
child: new Text('Info')
),
);
static final Container save = new Container(
child: new Center(
child: new Text('Save')
),
);
static final Container settings = new Container(
child: new Center(
child: new Text('Settings')
),
);
Container activeContainer = info;
@override
Widget build(BuildContext context) {
return new Scaffold(
drawer: new Drawer(
child: new ListView(
children: <Widget>[
new Container(child: new DrawerHeader(child: new Container())),
new Container (
child: new Column(
children: <Widget>[
new ListTile(leading: new Icon(Icons.info), title: new Text('Info'),
onTap:(){
setState((){
activeContainer = info;
});
Navigator.of(context).pop();
}
),
new ListTile(leading: new Icon(Icons.save), title: new Text('Save'),
onTap:(){
setState((){
activeContainer = save;
});
Navigator.of(context).pop();
}
),
new ListTile(leading: new Icon(Icons.settings), title: new Text('Settings'),
onTap:(){
setState((){
activeContainer = settings;
});
Navigator.of(context).pop();
}
),
]
),
)
],
),
),
appBar: new AppBar(title: new Text("Test Page"),),
body: activeContainer,
);
}
}
【问题讨论】:
-
This 可能会有所帮助