Flutter StatefulWidget传递数据,多级控件传递数据

Posted 一叶飘舟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter StatefulWidget传递数据,多级控件传递数据相关的知识,希望对你有一定的参考价值。

父类传递给子类可以通过构造方法。

那么子类给父类呢?

答案也是构造方法 直接传递VoidCallback进来

这个VoidCallback是通过查看onPress类型得到的

import 'package:flutter/material.dart';
 
class StateManagementDemo2 extends StatefulWidget 
  @override
  _StateManagement2State createState() => _StateManagement2State();

 
class _StateManagement2State extends State<StateManagementDemo2> 
  int _count = 0;
 
  void _increaseCount() 
    setState(() 
      _count += 1;
    );
  
 
  @override
  Widget build(BuildContext context) 
    return Scaffold(
      appBar: AppBar(
        title: Text("StatueManagementDemo"),
        elevation: 0.0,
      ),
      body: Counter(_count, _increaseCount),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () 
          setState(() 
            _count += 1;
          );
          print(_count);
        ,
      ),
    );
  

 
class Counter extends StatelessWidget 
  final int count;
  final VoidCallback increaseCount;
 
  Counter(this.count, this.increaseCount);
 
  @override
  Widget build(BuildContext context) 
    return Center(
      child: ActionChip(
        label: Text('$count'),
        onPressed: increaseCount,
      ),
    );
  


多级别当然就是多个构造

import 'package:flutter/material.dart';
 
class StateManagementDemo3 extends StatefulWidget 
  @override
  _StateManagement3State createState() => _StateManagement3State();

 
class _StateManagement3State extends State<StateManagementDemo3> 
  int _count = 0;
 
  void _increaseCount() 
    setState(() 
      _count += 1;
    );
  
 
  @override
  Widget build(BuildContext context) 
    return Scaffold(
      appBar: AppBar(
        title: Text("StatueManagementDemo"),
        elevation: 0.0,
      ),
      body: ConunterWrapper(_count, _increaseCount),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () 
          setState(() 
            _count += 1;
          );
          print(_count);
        ,
      ),
    );
  

 
class ConunterWrapper extends StatelessWidget 
  final int count;
  final VoidCallback increaseCount;
 
  ConunterWrapper(this.count, this.increaseCount);
 
  @override
  Widget build(BuildContext context) 
    return Center(
      child: Counter(count, increaseCount),
    );
  

 
class Counter extends StatelessWidget 
  final int count;
  final VoidCallback increaseCount;
 
  Counter(this.count, this.increaseCount);
 
  @override
  Widget build(BuildContext context) 
    return Center(
      child: ActionChip(
        label: Text('$count'),
        onPressed: increaseCount,
      ),
    );
  

以上是关于Flutter StatefulWidget传递数据,多级控件传递数据的主要内容,如果未能解决你的问题,请参考以下文章

Flutter StatefulWidget传递数据,多级控件传递数据

Flutter StatefulWidget传递数据,多级控件传递数据

将 StatefulWidget 数据传递给 State 类而不使用构造函数

Flutter学习-有状态的StatefulWidget

如何从 Flutter 中的其他 StatefulWidget 设置/更新 StatefulWidget 的状态?

Flutter 中的 statefulWidget 中的屏幕未更新