【问题标题】:how to pass data from StatelessWidget to StatefulWidget如何将数据从 StatelessWidget 传递到 StatefulWidget
【发布时间】:2020-04-26 00:14:11
【问题描述】:

请,我想传递数据(从 ItemList 到 StatefulWidget 中的 FloatingActionButton 的索引和列表)

class _mes_adresseState extends State<mes_adresse> {

  DatabaseHelper databaseHelper = new DatabaseHelper();

  _save(String token) async {
    final prefs = await SharedPreferences.getInstance();
    final key = 'token';
    final value = token;
    prefs.setString(key, value);
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        floatingActionButton: FloatingActionButton.extended(
          backgroundColor: Colors.amber,
          onPressed: () {Navigator.of(context).push(
            new MaterialPageRoute(
                builder: (BuildContext context) => new EditAdress(
                      index: i // from index in list view.builder ,
                      list : list // from list in itemlist,
                )),

class ItemList extends StatelessWidget {
  List list;
  ItemList({this.list});

  @override
  Widget build(BuildContext context) {
    return new ListView.builder(
        itemCount: list[0] == null ? 0 : list[0].length,
        itemBuilder: (context, i) {
          return new Container(
            padding: const EdgeInsets.all(1.0),
            child: new GestureDetector(
              onTap: () => Navigator.of(context).push(
                new MaterialPageRoute(
                    builder: (BuildContext context) => new EditAdress(
                      list: list,
                      index: i,
                    )),
                //      builder: (BuildContext context) => new ShowData(list:list , index:i,) ),
              ),

      );},

floatingActionButton 返回另一个页面,并从页面 mes_adresse 列出和索引

【问题讨论】:

    标签: flutter dart mobile statefulwidget statelesswidget


    【解决方案1】:

    你需要在目标类的构造函数中声明你的变量;

    class EditAdress extends StatefulWidget {
      final List list;
      final int index;
    
      const EditAdress({Key key, this.list, this.index}) : super(key: key);
      @override
      _EditAdressState createState() => _EditAdressState();
    }
    
    class _EditAdressState extends State<EditAdress> {
      @override
      Widget build(BuildContext context) {
        // you can access your data through widget
        print(widget.index);    
        return Container(
    
        );
      }
    }
    

    【讨论】:

    • 谢谢,但我需要将数据(索引和列表)从(ItemList 类扩展 StatelessWidget)传递到 _mes_adresseState 类扩展 State
    猜你喜欢
    • 2020-10-30
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 2021-05-16
    • 2021-09-19
    • 2021-06-24
    相关资源
    最近更新 更多