build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context) {
  return Theme(
    data: context.currentTheme,
    child: Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: ListenableBuilder(
          listenable: mainDataProvider,
          builder: (context, child) {
            return Text(
              'HttpRecords( ${mainDataProvider.httpRecords.length} )',
            );
          },
        ),
        // bottom: TabBar(tabs: tabs),
        leading: leading,
        actions: [
          TitleBarActionWidget(
            iconData: Icons.help_outline,
            onPressed: () async {
              final url = viewConfig.manualUrl ??
                  'https://github.com/sunbird89629/fancy_dio_inspector/blob/main/docs/manual.md';
              final opener = viewConfig.onOpenManual;
              if (opener != null) {
                opener(url);
                return;
              }

              // Capture navigation and messenger before async gaps to avoid
              // using BuildContext across async boundaries.
              final navigator = Navigator.of(context);
              final messenger = ScaffoldMessenger.of(context);

              // Try to open with url_launcher first
              final uri = Uri.parse(url);
              final launched = await launchUrl(
                uri,
                mode: LaunchMode.externalApplication,
              );

              if (!launched) {
                if (!navigator.mounted) return;
                // Fallback: show dialog with copy-to-clipboard
                // ignore: use_build_context_synchronously
                await showDialog<void>(
                  context: navigator.context,
                  builder: (ctx) {
                    return AlertDialog(
                      title: const Text('打开使用手册'),
                      content: SelectableText(url),
                      actions: [
                        TextButton(
                          onPressed: () {
                            Clipboard.setData(ClipboardData(text: url));
                            Navigator.of(ctx).pop();
                            messenger.showSnackBar(
                              const SnackBar(content: Text('链接已复制')),
                            );
                          },
                          child: const Text('复制链接'),
                        ),
                        TextButton(
                          onPressed: () => Navigator.of(ctx).pop(),
                          child: const Text('关闭'),
                        ),
                      ],
                    );
                  },
                );
              }
            },
          ),
          TitleBarActionWidget(
            iconData: Icons.tune,
            onPressed: () {},
          ),
          TitleBarActionWidget(
            iconData: Icons.clear_all,
            onPressed: () {
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(
                  content: Text('请长按以清空历史记录'),
                  duration: Duration(seconds: 3),
                ),
              );
              // ScaffoldMessenger.of(context).showMaterialBanner(
              //   const MaterialBanner(
              //     content: Text('为防止误触,请长按以删除历史记录'),
              //     actions: [
              //       SizedBox.shrink(),
              //     ],
              //   ),
              // );
            },
            onLongPress: () {
              FancyDioLogger.instance.records.clear();
              mainDataProvider.httpRecords = FancyDioLogger.instance.records;
            },
          ),
        ],
      ),
      // body: TabBarView(children: tabBarViews),
      body: _buildBody(),
    ),
  );
}