由scaffold floatingActionButton

时间:2018-05-30 03:26:15

标签: dart flutter

我有一个包含floatingActionButton的脚手架,如下所示:

return new Scaffold(
        appBar: new AppBar(
            title : new Text('Test Flutter'),
        ),
        body: new Center(
            child: new ListSection(),
        ),
        floatingActionButton: new FloatingActionButton(
            child: new Icon(Icons.add),
            onPressed: null
        ),
    );

列表部分:

class ListSection extends StatelessWidget
{
    @override
    Widget build(BuildContext context) {
        return new Container(
            child: new Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                    new WeekSection(),
//                  new MonthSection(),
//                  new YearSection()
                ]
            )
        );
    }
}

最后,WeekSection定义如下:

class WeekSection extends StatefulWidget
{
    @override
    _WeekSectionState createState() => new _WeekSectionState();
}

如何使用floatingActionButton.onPressed更新_WeekSectionState中的状态

1 个答案:

答案 0 :(得分:1)

基本方法是在MyApp脚手架中设置私有变量,并将对该字段的引用一直传递到WeekSection

所以你将这个变量通过构造函数传递给ListSection并再次通过构造函数传递给WeekSection

WeekSectionState内,您可以widget.someFieldWeekSection访问setState;

稍后,当您在FloatingActionButton的侦听器中直接调用MyApp内的WeekSection时,它将重建with connection.cursor() as cursor: cursor.execute("select a, b, c from bar") print(cursor.fetchall()) ,因为保存状态信息的Widget已更改。颤动会检查所有需要重建的孩子。