Flutter 如何将快照数据传递给颤振浮动操作按钮?

Flutter 如何将快照数据传递给颤振浮动操作按钮?,flutter,Flutter,我正在构建一个从API获取数据的颤振应用程序 如何将快照数据从颤振屏幕传递到小部件生成器上下文之外的浮动操作按钮 然后,浮动按钮将数据传递给PDF builder函数 这是我的密码 @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Colors.blue,

我正在构建一个从API获取数据的颤振应用程序

如何将快照数据从颤振屏幕传递到小部件生成器上下文之外的浮动操作按钮

然后,浮动按钮将数据传递给PDF builder函数

这是我的密码

  @override
  Widget build(BuildContext context) {
    return Scaffold(
              appBar: AppBar( 
                        backgroundColor: Colors.blue,
                        automaticallyImplyLeading: false,
                        centerTitle : true,

                        title: Text(widget.bill.billNumber.toString(), 
                                    style: TextStyle(color: Colors.white)
                                    ),

                        leading: IconButton(icon:Icon(Icons.arrow_back),
                                  onPressed:() =>

                        Navigator.of(context).pushAndRemoveUntil(
                          MaterialPageRoute(
                             builder: (context) =>
                               BillsScreen(table: widget.table)), (Route<dynamic> route) => false)
                        )
              ),
              body: SafeArea(
                child: Container(
                       child: 
                          StreamBuilder<SalesResponse>(
                              stream: salesBloc.subject.stream,
                              builder: (context, AsyncSnapshot<SalesResponse> snapshot) {
        
                                     if (snapshot.hasData) {
                                         return  _buildSalesListWidget(snapshot.data);
                                     } 
                                     
                                     else if (snapshot.hasError) {
                                         return _buildErrorWidget(snapshot.error);
                                     } 
                              },
                          )
              ),
        ),

              floatingActionButtonLocation:  FloatingActionButtonLocation.centerDocked,
                 floatingActionButton: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: <Widget>[
    
                                 FloatingActionButton(
                                    heroTag: 1,
                                    backgroundColor: Colors.yellow,
                                    onPressed: () {
                                        Printing.layoutPdf(onLayout:(format)=> 
                                           buildReceipt(widget.table, 
                                                        widget.bill, 
                                                        data.sales));
                                    },
                                    child: Icon(FontAwesomeIcons.print),
                                 ),
                              ],
                    ),
                )    
    );    
  }

  Widget _buildSalesListWidget(SalesResponse data) {
          return ListView.builder(
                     itemCount: data.sales.length,
                     itemBuilder: (context, index) {
                         return ListTile(
                                  title: Text(
                                              data.sales[index].item,
                                              style: TextStyle(
                                                         fontWeight: FontWeight.w500,
                                                         fontSize: 20,
                                              ),
                                  ),
                                  subtitle: Text('qty' + ' ' + data.sales[index].qty.toString() + ', ' + 'price' + ' ' + data.sales[index].price.toStringAsFixed(2)),

                                  leading: Icon(
                                                FontAwesomeIcons.beer,
                                                color: Colors.blue[500],
                                  ),           
                                  onTap: () {},
                         );
                     },
        );

  }
}
@覆盖
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
背景颜色:Colors.blue,
自动嵌入:false,
标题:对,
标题:文本(widget.bill.billNumber.toString(),
样式:TextStyle(颜色:Colors.white)
),
前导:图标按钮(图标:图标(图标。箭头向后),
按下:()=>
Navigator.of(context).pushandremoveintil(
材料路线(
生成器:(上下文)=>
BillsScreen(表:widget.table)),(路由)=>false)
)
),
正文:安全区(
子:容器(
儿童:
StreamBuilder(
流:salesBloc.subject.stream,
生成器:(上下文,异步快照){
if(snapshot.hasData){
return\u buildSalesListWidget(snapshot.data);
} 
else if(snapshot.hasrerror){
返回_buildErrorWidget(snapshot.error);
} 
},
)
),
),
floatingActionButtonLocation:floatingActionButtonLocation.centerDocked,
浮动操作按钮:填充(
填充:常数边集全部(8.0),
孩子:排(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
浮动操作按钮(
heroTag:1,
背景颜色:Colors.yellow,
已按下:(){
打印.布局PDF(仅布局:(格式)=>
BuildReceive(widget.table,
比尔先生,
数据(销售);;
},
子:图标(FontAwesomeIcons.print),
),
],
),
)    
);    
}
Widget\u buildSalesListWidget(SalesResponse数据){
返回ListView.builder(
itemCount:data.sales.length,
itemBuilder:(上下文,索引){
返回列表块(
标题:正文(
data.sales[index]。物料,
样式:TextStyle(
fontWeight:fontWeight.w500,
尺寸:20,
),
),
字幕:文本('qty'+''数据.sales[index].qty.toString()+','+'价格'+''+数据.sales[index].price.toStringAsFixed(2)),
领先:图标(
丰塔威啤酒,
颜色:颜色。蓝色[500],
),           
onTap:(){},
);
},
);
}
}
类屏幕扩展StatefulWidget{
@凌驾
_ScreenState createState();
}
类_ScreenState扩展了状态{
字符串snapshotData=“”;
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
孩子:未来建设者(
future:future.delayed(常量持续时间(秒:1),()=>“要传递的数据”),
生成器:(上下文,异步快照){
如果(!snapshot.hasData){
返回文本(“无数据”);
}否则{
snapshotData=snapshot.data;
返回文本(“数据加载”);
}
},
),
),
浮动操作按钮:浮动操作按钮(
按下:()=>打印(快照数据),
),
);
}
}
类屏幕扩展StatefulWidget{
@凌驾
_ScreenState createState();
}
类_ScreenState扩展了状态{
字符串snapshotData=“”;
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
孩子:未来建设者(
future:future.delayed(常量持续时间(秒:1),()=>“要传递的数据”),
生成器:(上下文,异步快照){
如果(!snapshot.hasData){
返回文本(“无数据”);
}否则{
snapshotData=snapshot.data;
返回文本(“数据加载”);
}
},
),
),
浮动操作按钮:浮动操作按钮(
按下:()=>打印(快照数据),
),
);
}
}
class Screen extends StatefulWidget {
  @override
  _ScreenState createState() => _ScreenState();
}

class _ScreenState extends State<Screen> {

  String snapshotData = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: FutureBuilder(
          future: Future.delayed(const Duration(seconds: 1), () => "data to pass"),
          builder: (context, AsyncSnapshot<String> snapshot) {
            if(!snapshot.hasData) {
             return Text("NO Data");
            } else {
              snapshotData = snapshot.data;
              return Text("Data loaded");
            }
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => print(snapshotData),
      ),
    );
  }
}