Reputation: 38733
I am using this code to navigate to an new page in 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,
)
),
),
but it show error:
the specific widget that could not find a material localizations ancestor in flutter
and this is the detail error:
what should I do to fix this? this is the full code of the class :
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),
),
],
),
],
),
),
));
}
}
I add MaterialApp on the entry of app:
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(),
);
}
}
Upvotes: 0
Views: 214
Reputation: 315
try wrap your Container
with Scaffold
This container
return Container(
color: Theme
.of(context)
.scaffoldBackgroundColor,
.......
);
Upvotes: 1