Вызов функции для события из другого класса Flutter

#dart #flutter #flutter-layout

#dart #flutter #flutter-макет

Вопрос:

У меня возникла эта проблема с Flutter, и я не знаю, как с этим справиться. Я пытался просмотреть некоторые учебные пособия, а затем мне захотелось сделать это.

Что я хочу сделать, так это вызвать из FloatingActionButton для onPresssed события ._increment , чтобы я мог увеличить _counter . Но затем я получаю эту ошибку. Чего я не понимаю?

 void main() {
  runApp(MaterialApp(
    title: 'Flutter Tutorial',
    home: TutorialHome(),
  ));
}

class TutorialHome extends StatelessWidget {


  @override
  Widget build(BuildContext context) {
    // Scaffold is a layout for the major Material Components.
    SystemChrome.setEnabledSystemUIOverlays([]); //Hide Status Bar
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.menu),
          tooltip: 'Navigation menu',
          onPressed: null,
        ),
        title: Text('Example title'),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.search),
            tooltip: 'Search',
            onPressed: null,
          ),
        ],
      ),
      // body is the majority of the screen.
      body: Center(
        child: Container(
          height: 100,
          width: 100,
          child: Column(
            children: <Widget>[
              Counter(),
            ]
          )
        )
      ),
      floatingActionButton: FloatingActionButton(
        tooltip: 'Add', // used by assistive technologies
        child: Icon(Icons.add),
        onPressed: _CounterState()._increment,
      ),
    );
  }
}


//class CounterIncrementor extends StatelessWidget {
//  CounterIncrementor({this.onPressed});
//
//  final VoidCallback onPressed;
//
//  @override
//  Widget build(BuildContext context) {
//    return RaisedButton(
//      onPressed: onPressed,
//      child: Text('Increment'),
//    );
//  }
//}

class Counter extends StatefulWidget {
  // This class is the configuration for the state. It holds the
  // values (in this case nothing) provided by the parent and used by the build
  // method of the State. Fields in a Widget subclass are always marked "final".

  @override
  _CounterState createState() => _CounterState();
}

class _CounterState extends State<Counter> {
  int _counter = 0;

  void _increment() {
    setState(() {
      // This call to setState tells the Flutter framework that
      // something has changed in this State, which causes it to rerun
      // the build method below so that the display can reflect the
      // updated values. If we changed _counter without calling
      // setState(), then the build method would not be called again,
      // and so nothing would appear to happen.
      print("Increasing counter valuen");
      _counter  ;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance
    // as done by the _increment method above.
    // The Flutter framework has been optimized to make rerunning
    // build methods fast so that you can just rebuild anything that
    // needs updating rather than having to individually change
    // instances of widgets.
    return Column(
      children: <Widget>[
        RaisedButton(
          onPressed: _increment,
          child: Text('Increment'),
        ),
        Text('Count: $_counter'),
      ],
    );
  }
}
  

Это ошибка:

  ************************ ERROR *************************
══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter (20840): The following assertion was thrown while handling a gesture:
I/flutter (20840): setState() called in constructor: _CounterState#452ba(lifecycle state: created, no widget, not
I/flutter (20840): mounted)
I/flutter (20840): This happens when you call setState() on a State object for a widget that hasn't been inserted into
I/flutter (20840): the widget tree yet. It is not necessary to call setState() in the constructor since the state is
I/flutter (20840): already assumed to be dirty when it is initially created.
  

Ответ №1:

Итак, проверяя ваш код, я увидел, что у вас есть 2 кнопки:

 FloatingActionButton
  

И

 RaisedButton
  

Разница между ними в том, что

  • RaisedButton при нажатии вызывает метод _increment из объекта экземпляра _CounterState, и этот объект уже смонтирован (этот объект _CounterState создает RaisedButton)
  • FloatingActionButton при нажатии вызывает метод для создания НОВОГО объекта _CounterState(), а затем для этого объекта увеличивает счетчик. Новое состояние не имеет ничего общего с тем, что отображается. Это просто новый объект (не вставленный в дерево виджетов).

Вы создаете экземпляр счетчика в TutorialHome

 //...
child: Column(
                  children: <Widget>[
                    Counter(), 
                  ]
              )

//...
  

Этот счетчик отвечает за создание состояний для самого себя (объектов _CounterState).

Обновление по запросу:

Это не чистый код, и он предназначен только для демонстрации.

Ниже приведен пример того, как настроить обмен информацией между двумя объектами с помощью контроллера потока (ваш счетчик и кнопка, нажатая откуда-то еще)… неаккуратно.

 import 'dart:async';

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: 'Flutter Tutorial',
    home: TutorialHome(),
  ));
}

class TutorialHome extends StatelessWidget {

  StreamController<void> buttonPressStream = StreamController<bool>.broadcast();

  @override
  Widget build(BuildContext context) {
    // Scaffold is a layout for the major Material Components.

    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.menu),
          tooltip: 'Navigation menu',
          onPressed: null,
        ),
        title: Text('Example title'),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.search),
            tooltip: 'Search',
            onPressed: null,
          ),
        ],
      ),
      // body is the majority of the screen.
      body: Center(
          child: Container(
              height: 100,
              width: 100,
              child: Column(
                  children: <Widget>[
                    Counter(buttonPressStream),
                  ]
              )
          )
      ),
      floatingActionButton: FloatingActionButton(
        tooltip: 'Add', // used by assistive technologies
        child: Icon(Icons.add),
        onPressed: () => buttonPressStream.add(null),
      ),
    );
  }
}


//class CounterIncrementor extends StatelessWidget {
//  CounterIncrementor({this.onPressed});
//
//  final VoidCallback onPressed;
//
//  @override
//  Widget build(BuildContext context) {
//    return RaisedButton(
//      onPressed: onPressed,
//      child: Text('Increment'),
//    );
//  }
//}

class Counter extends StatefulWidget {
  // This class is the configuration for the state. It holds the
  // values (in this case nothing) provided by the parent and used by the build
  // method of the State. Fields in a Widget subclass are always marked "final".

  final StreamController<void> buttonPressStream;

  const Counter(this.buttonPressStream);

  @override
  _CounterState createState() => _CounterState(buttonPressStream);
}

class _CounterState extends State<Counter> {
  int _counter = 0;

  StreamController<void> buttonPressStream;
  _CounterState(this.buttonPressStream);

  void _increment() {
    setState(() {
      // This call to setState tells the Flutter framework that
      // something has changed in this State, which causes it to rerun
      // the build method below so that the display can reflect the
      // updated values. If we changed _counter without calling
      // setState(), then the build method would not be called again,
      // and so nothing would appear to happen.
      print("Increasing counter valuen");
      _counter  ;
    });
  }

  @override
  void initState() {
    super.initState();
    buttonPressStream.stream.listen( (_) {
      setState(() {});
    });
  }



  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance
    // as done by the _increment method above.
    // The Flutter framework has been optimized to make rerunning
    // build methods fast, so that you can just rebuild anything that
    // needs updating rather than having to individually change
    // instances of widgets.
    return Column(
      children: <Widget>[
        RaisedButton(
          onPressed: _increment,
          child: Text('Increment'),
        ),
        Text('Count: $_counter'),
      ],
    );
  }
}
  

Комментарии:

1. Да, я это понял. В моем случае, если я хочу обновить счетчик, могу ли я сделать это из FloatingActionButton ?

2. Нет проблем, Бенни