flutter 该值不是来自警报窗口

yws3nbqq  于 2023-04-22  发布在  Flutter
关注(0)|答案(2)|浏览(92)

当在警报中按下onConfirmBtnTap按钮时,控制转移到前一屏幕,在导航处理功能中显示该值。但是,出现空值。
为什么值不是来自alert?(它是在onTapFunction中)

// start
class FirstPage extends StatefulWidget {...}
class _FirstPageState extends State<FirstPage> {

  @override
  Widget build(BuildContext context) {
    debugPrint('-------------building outer----------'); //true
    return Scaffold(
      body: Column(
        children: [
          ...
          InnerWidget(key: UniqueKey()), //unique
        ],
      ),
    );
  }
}
    
// Inner widget
class InnerWidget extends StatefulWidget {...}
class _InnerWidgetState extends State<InnerWidget> {
  ...
  @override
  Widget build(BuildContext context) {
    debugPrint('building inner widget...');
    ...
    // in gesture detector
    onTap: () {
        var productData = ...;
        onTapFunction(context, Group(productData));
    },
  }

  // onTapFunction is defined in InnerWidget
  onTapFunction(BuildContext context, Widget route) async {
    final reLoadPage = await Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => route,
      ),
    );
    if (reLoadPage ?? false) { // the value of reloadPage is null
      debugPrint('---------------reLoadPage: $reLoadPage');
      setState(() {});
    }
  }
} // Inner widget end
    
// on Group page a button opens alert dialog 
onPressed: () {
    storage.removeProduct(productData);
    if (!context.mounted) return;
    productDeletedSuccessDialog(context); //opens alert dialog
},

// in alert dialog on confirm it pops back and pass value
onConfirmBtnTap: () {
  debugPrint('product $productData removed');
  Navigator.pop(context, true); // it should pass `true` into InnerWidget (?)
});

p.s.我试图尽可能严格和清晰地解释和展示代码,以便理解正在发生的事情的本质。

osh3o9ms

osh3o9ms1#

我更新了onTapFunction。一旦从第二个屏幕弹出,then子句将触发。

// onTapFunction used and defined in InnerWidget
onTapFunction(BuildContext context, Widget route) async {
    final reLoadPage = await Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => route,
      ),
    ).then((value){
      if (reLoadPage ?? false) {
          debugPrint('---------------reLoadPage: $reLoadPage');
          setState(() {});
      }
  });
}
kadbb459

kadbb4592#

像这样试试

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  MyHomePageState createState() => MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [InnerWidget()],
        ),
      ),
    );
  }
}

class SecondPage extends StatefulWidget {
  SecondPage({Key? key}) : super(key: key);

  @override
  State<SecondPage> createState() => _SecondPageState();
}

class _SecondPageState extends State<SecondPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.pop(context, true);
          },
          child: const Text('Go Back'),
        ),
      ),
    );
  }
}

class InnerWidget extends StatefulWidget {
  InnerWidget({Key? key}) : super(key: key);

  @override
  State<InnerWidget> createState() => _InnerWidgetState();
}

class _InnerWidgetState extends State<InnerWidget> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ElevatedButton(
          onPressed: () async {
            final reLoadPage = await Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => SecondPage(),
              ),
            );
            if (reLoadPage ?? false) {
              debugPrint('---------------reLoadPage: $reLoadPage');
              setState(() {});
            }
          },
          child: const Text('Open Page'),
        ),
      ],
    );
  }
}

相关问题