首页 > 解决方案 > 在颤振中找不到材料本地化祖先的特定小部件

问题描述

我正在使用此代码导航到 Flutter 中的新页面:

               if (item.domain != "")
                  Padding(
                    padding: const EdgeInsets.only(bottom: 8.0),
                    child: InkWell(
                        onTap: () async {
                        Channel channel = await Repo.fetchChannelItem(int.parse(item.subSourceId));
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                          builder: (context) =>
                              ChannelInformation(item:channel)),
                        );
                      }, child: Text(
                      item.domain,
                      style: Theme
                          .of(context)
                          .textTheme
                          .caption,
                      )
                    ),
                  ),

但它显示错误:

the specific widget that could not find a material localizations ancestor in flutter

这是详细错误:

在此处输入图像描述

我应该怎么做才能解决这个问题?这是该类的完整代码:

import 'package:Cruise/src/common/article_action.dart';
import 'package:Cruise/src/common/net/rest/http_result.dart';
import 'package:Cruise/src/models/Channel.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_html/style.dart';
import 'package:flutter_icons/flutter_icons.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:Cruise/src/component/part_snippet.dart';
import 'package:Cruise/src/common/helpers.dart';
import 'package:Cruise/src/models/Item.dart';
import 'package:Cruise/src/page/profile.dart';
import 'package:Cruise/src/common/Repo.dart';
import 'package:url_launcher/url_launcher.dart';

import 'channel_information.dart';

final partsProvider = FutureProvider.family((ref, int id) async {
  return await Repo.fetchArticleItem(id);
});

class StoryInformation extends HookWidget {
  const StoryInformation({
    Key key,
    @required this.item,
  }) : super(key: key);

  final Item item;

  void launchUrl(url) async {
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  @override
  Widget build(BuildContext context) {
    final parts = item.parts.map((i) => useProvider(partsProvider(i))).toList();

    Offset _initialSwipeOffset;
    Offset _finalSwipeOffset;

    void _onHorizontalDragStart(DragStartDetails details) {
      _initialSwipeOffset = details.globalPosition;
    }

    void _onHorizontalDragUpdate(DragUpdateDetails details) {
      _finalSwipeOffset = details.globalPosition;
    }

    void _onHorizontalDragEnd(DragEndDetails details) {
      if (_initialSwipeOffset != null) {
        final offsetDifference = _initialSwipeOffset.dx - _finalSwipeOffset.dx;
        if (offsetDifference < 0) {
          Navigator.pop(context);
        }
      }
    }

    void touchFav(String action) async {
      HttpResult result = await ArticleAction.fav(
        articleId: item.id.toString(),
        action: action
      );

      if (result.result == Result.error) {
        Fluttertoast.showToast(
            msg: "添加收藏失败",
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.CENTER,
            timeInSecForIosWeb: 1,
            backgroundColor: Colors.red,
            textColor: Colors.white,
            fontSize: 16.0
        );
      } else {
        Fluttertoast.showToast(
            msg: "添加收藏成功",
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.CENTER,
            timeInSecForIosWeb: 1,
            backgroundColor: Colors.red,
            textColor: Colors.white,
            fontSize: 16.0
        );
      }
    }

    return Container(
        color: Theme
            .of(context)
            .scaffoldBackgroundColor,
        child: GestureDetector(
          onHorizontalDragStart: _onHorizontalDragStart,
          onHorizontalDragUpdate: _onHorizontalDragUpdate,
          onHorizontalDragEnd: _onHorizontalDragEnd,
          child: Padding(
            padding: const EdgeInsets.all(
              16.0,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                InkWell(
                  onTap: () => launchUrl(item.link),
                  child: Padding(
                    padding: const EdgeInsets.only(bottom: 8.0),
                    child: Container(
                      child: Text(
                        item.title == "" ? "Comment" : item.title,
                        style: Theme
                            .of(context)
                            .textTheme
                            .headline5
                            .copyWith(
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                  ),
                ),
                if (item.domain != "")
                  Padding(
                    padding: const EdgeInsets.only(bottom: 8.0),
                    child: InkWell(
                        onTap: () async {
                        Channel channel = await Repo.fetchChannelItem(int.parse(item.subSourceId));
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                          builder: (context) =>
                              ChannelInformation(item:channel)),
                        );
                      }, child: Text(
                      item.domain,
                      style: Theme
                          .of(context)
                          .textTheme
                          .caption,
                      )
                    ),
                  ),
                InkWell(
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) =>
                              ProfilePage(username: item.author)),
                    );
                  },
                  child: RichText(
                    text: TextSpan(
                      children: <TextSpan>[
                        TextSpan(
                          text: item.author,
                          style: Theme
                              .of(context)
                              .textTheme
                              .caption
                              .copyWith(
                            color: Theme
                                .of(context)
                                .primaryColor,
                          ),
                        ),
                        TextSpan(
                          text: " ${String.fromCharCode(8226)} ",
                          style: Theme
                              .of(context)
                              .textTheme
                              .caption,
                        ),
                        TextSpan(
                          text: item.ago,
                          style: Theme
                              .of(context)
                              .textTheme
                              .caption,
                        ),
                      ],
                    ),
                  ),
                ),
                if (item.content != "")
                  Html(
                    data: item.content,
                    style: {
                      "body": Style(
                        fontSize: FontSize(19.0),
                      ),
                    },
                    onLinkTap: (url) => launchUrl(url),
                  ),
                if (item.parts.isNotEmpty)
                  Padding(
                    padding: const EdgeInsets.only(top: 8.0),
                    child: ListView.builder(
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      itemCount: item.parts.length,
                      itemBuilder: (context, index) {
                        return PartSnippet(part: parts[index]);
                      },
                    ),
                  ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      children: [
                        Padding(
                          padding: const EdgeInsets.only(right: 16.0),
                          child: Row(
                            children: [
                              if(item.isFav == 1)
                                IconButton(
                                  icon:Icon(Feather.bookmark,
                                      color: Theme
                                      .of(context)
                                      .primaryColor),
                                  onPressed: () => touchFav("unfav"),
                                ),
                              if(item.isFav != 1)
                                IconButton(
                                icon:Icon(Feather.bookmark,
                                    color: Theme
                                    .of(context)
                                    .primaryColor),
                                onPressed: () => touchFav("fav"),
                                ),
                              Padding(
                                padding: const EdgeInsets.only(left: 8.0),
                                child: Text(
                                  "${item.score}",
                                  textAlign: TextAlign.center,
                                  style: Theme
                                      .of(context)
                                      .textTheme
                                      .caption
                                      .copyWith(
                                    color: Theme
                                        .of(context)
                                        .primaryColor,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.only(right: 16.0),
                          child: Row(
                            children: [
                              Icon(
                                Feather.arrow_up,
                              ),
                              Padding(
                                padding: const EdgeInsets.only(left: 8.0),
                                child: Text(
                                  "${item.score}",
                                  textAlign: TextAlign.center,
                                  style: Theme
                                      .of(context)
                                      .textTheme
                                      .caption
                                      .copyWith(
                                    color: Theme
                                        .of(context)
                                        .primaryColor,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.only(right: 16.0),
                          child: Row(
                            children: [
                              Icon(
                                Feather.message_square,
                              ),
                              Padding(
                                padding: const EdgeInsets.only(left: 8.0),
                                child: Text(
                                  item.descendants.toString(),
                                  textAlign: TextAlign.center,
                                  style: Theme
                                      .of(context)
                                      .textTheme
                                      .caption,
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                    IconButton(
                      icon: Icon(
                        Feather.share_2,
                      ),
                      onPressed: () =>
                          handleShare(
                              id: item.id,
                              title: item.title,
                              postUrl: item.link),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ));
  }
}

我在应用程序的条目上添加了 MaterialApp:

class CruiseApp extends HookWidget {
  const CruiseApp({@required this.theme, @required this.view});

  final ThemeData theme;
  final ViewType view;

  @override
  Widget build(BuildContext context) {
    final themeManager = useProvider(themeProvider);
    final viewManager = useProvider(viewProvider);
    useMemoized(() {
      // TODO: Right now this triggers a rebuild, so unfortunately you'll see a flash of default theme.
      themeManager.setTheme(theme);
      viewManager.setView(view);
    });

    final currentTheme = useProvider(themeProvider.state);
    return MaterialApp(
      title: 'Cruise',
      theme: currentTheme,
      routes: {
        "home": (context) => HomeNew(),
      },
      home: HomeNew(),
    );
  }
}

标签: flutter

解决方案


试着ContainerScaffold

这个容器

return Container(
        color: Theme
            .of(context)
            .scaffoldBackgroundColor,
       .......
);

推荐阅读