How to display Dialog in Flutter without a Buildcontext inside a HookWidget

Malith Kuruppu

How do I show a showDialog without a Buildcontext in the sample code given below which has within a class that extends a HookWidget?

Currently, I am getting an Undefined name 'context'.

I am using the following package for state management of my app:

flutter_hooks: ^0.10.0
hooks_riverpod: ^0.3.0

Sample Code:

class HomeScreen extends HookWidget {
  static String routeName = "/home";

  PageController _pageController = PageController(initialPage: 1);

  dynamic balanceAvailable = 0.0;
  List<TransactionModel> transactionList = [];

  _onRefresh(DragEndDetails details) async {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return Center(
            child: SizedBox(
              height: MediaQuery.of(context).size.height / 4,
              width: MediaQuery.of(context).size.width / 2,
              child: CircularProgressIndicator(
                valueColor: new AlwaysStoppedAnimation<Color>(Colors.blue),
              ),
            ),
          );
        });
    await Future.delayed(const Duration(seconds: 2));

    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GestureDetector(
        onVerticalDragEnd: _onRefresh,
        child: PageView(
          controller: _pageController,
          children: [
            MenuScreen(),
            HomeBody(
              value: balanceAvailable,
            ),
            TransactionScreen(
              transactionList: transactionList,
              value: balanceAvailable,
            ),
          ],
        ),
      ),
    );
  }
}
chunhunghan

You can copy paste run full code below
You can pass context to _onRefresh
Step 1: _onRefresh(DragEndDetails details, BuildContext context)
Step 2: onVerticalDragEnd: (details) => {_onRefresh(details, context)},

working demo

enter image description here

full code

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

class TransactionModel {}

class HomeScreen extends HookWidget {
  static String routeName = "/home";

  PageController _pageController = PageController(initialPage: 1);

  dynamic balanceAvailable = 0.0;
  List<TransactionModel> transactionList = [];

  _onRefresh(DragEndDetails details, BuildContext context) async {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return Center(
            child: SizedBox(
              height: MediaQuery.of(context).size.height / 4,
              width: MediaQuery.of(context).size.width / 2,
              child: CircularProgressIndicator(
                valueColor: new AlwaysStoppedAnimation<Color>(Colors.blue),
              ),
            ),
          );
        });
    await Future.delayed(const Duration(seconds: 2));

    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GestureDetector(
        onVerticalDragEnd: (details) => {_onRefresh(details, context)},
        child: PageView(
          controller: _pageController,
          children: [
            MenuScreen(),
            HomeBody(),
            TransactionScreen(),
          ],
        ),
      ),
    );
  }
}

class MenuScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("MenuScreen"),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'MenuScreen',
              ),
            ],
          ),
        ));
  }
}

class HomeBody extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("HomeBody"),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'HomeBody',
              ),
            ],
          ),
        ));
  }
}

class TransactionScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("TransactionScreen"),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'TransactionScreen',
              ),
            ],
          ),
        ));
  }
}

void main() {
  runApp(MyApp());
}

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get the screen size in Flutter without BuildContext?

How to get context in a simple class without BuildContext in flutter

How to display a Dialog inside a View?

Getting screen size in a class without buildcontext in Flutter

How to use the pageview inside show dialog (Flutter)?

How to dispose FocusNode and TextEditingController inside a Dialog in Flutter

Using flutter HookWidget and didChangeAppLifecycleState

How to close an Alert dialog without a button in FLUTTER

How to create a widget for map function in flutter with buildContext

How to use HookWidget with useTextEditingController

Flutter Riverpod using ProviderListener in HookWidget

Flutter: How to display Tooltip for TextSpan inside RichText

How to change text inside bottom sheet dialog in flutter?

Flutter close a Dialog inside a condition

Alignment of content inside Dialog in flutter?

How to automatically close a dialog box without clicking in flutter?

Flutter redux display alert dialog

How to display a simple dialog in SharePoint 2010 without breaking the page?

Flutter how pass additional argument to Function(BuildContext) callback

How to listen to a class change on Flutter's BuildContext.select()?

Flutter: reusable widget and BuildContext

Getting BuildContext in Flutter for localization

Flutter Riverpod disposing StreamController and using in HookWidget

Flutter HookWidget unknown read from context

Flutter HookWidget rebuild when values changed

Preload Image Without BuildContext

How to dismiss flutter dialog?

How to display pdf inside container of widget [flutter-web]

How to display a Map inside a field of a specific document in firestore using flutter?