之前MaterialApp配置路由路由是写在StateLessWidget中的,所以填充的routes也是好用的。现在由于要动态修改主题,所以把StateLessWidget 修改了 StatefulWidget,可是这样Navigator.of(context).pushName()就会崩溃…
import 'package:FlutterPlayground/blur_test_page.dart';
import 'package:FlutterPlayground/inherited/inherited_test_page.dart';
import 'package:FlutterPlayground/layout_test_page.dart';
import 'package:FlutterPlayground/lifecycle_test_page.dart';
import 'package:FlutterPlayground/router_constant.dart';
import 'package:FlutterPlayground/stateful_widget_page.dart';
import 'package:FlutterPlayground/text_line_height_page.dart';
import 'package:flutter/material.dart';
import 'package:page_life_cycle/page_life_cycle.dart';
import 'gesture_test_page.dart';
import 'state_less_widget_page.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
State<StatefulWidget> createState() {
return _HomePageState();
}
}
class _HomePageState extends State<MyApp> {
Brightness _mCurrentBrightness = Brightness.light;
Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
RouterConstant.STATELESS_PAGE: (BuildContext context) => StatelessWidgetPage(),
RouterConstant.STATEFULL_PAGE: (BuildContext context) => StatefulWidgetPage(),
RouterConstant.TEXT_LINE_HEIGHT_PAGE: (BuildContext context) => TextlineHeightPage(),
RouterConstant.BLUR_TEST_PAGE: (BuildContext context) => BlurTestPage(),
RouterConstant.LAYOUT_TEST_PAGE: (BuildContext context) => LayoutTestPage(),
RouterConstant.GESTURE_TEST_PAGE: (BuildContext context) => GestureTestPage(),
RouterConstant.LIFECYCLE_TEST_PAGE: (BuildContext context) => LifecycleTestPage(),
RouterConstant.INHERITED_TEST_PAGE: (BuildContext context) => InheritedTestPage(),
};
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
brightness: _mCurrentBrightness,
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
navigatorObservers: [PageNavigatorObserver()],
// onGenerateRoute: (RouteSettings settings) {
// return MaterialPageRoute(builder: (BuildContext context) =>
// routes[settings.name].call(context));
// },
routes: routes,
home: Scaffold(
appBar: AppBar(title: Text("FlutterPlayground")),
body: _buildBodyWidget(),
)
);
}
Widget _buildBodyWidget() {
List<Widget> buttons = new List<Widget>();
buttons.add(_buildBrightnessSwitch());
buttons.add(_buildRaisedButton("StatelessPage", () => goToTargetPage(RouterConstant.STATELESS_PAGE)));
buttons.add(_buildRaisedButton("StatefulPage", () => goToTargetPage(RouterConstant.STATEFULL_PAGE)));
buttons.add(_buildRaisedButton("Text行高测试", () => goToTargetPage(RouterConstant.TEXT_LINE_HEIGHT_PAGE)));
buttons.add(_buildRaisedButton("高斯模糊测试", () => goToTargetPage(RouterConstant.BLUR_TEST_PAGE)));
buttons.add(_buildRaisedButton("布局控件测试", () => goToTargetPage(RouterConstant.LAYOUT_TEST_PAGE)));
buttons.add(_buildRaisedButton("点击事件相关测试", () => goToTargetPage(RouterConstant.GESTURE_TEST_PAGE)));
buttons.add(_buildRaisedButton("页面生命周期相关测试", () => goToTargetPage(RouterConstant.LIFECYCLE_TEST_PAGE)));
buttons.add(_buildRaisedButton("Inherited数据共享", () => goToTargetPage(RouterConstant.INHERITED_TEST_PAGE)));
return Container(
alignment: Alignment.topCenter,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: buttons,
),
);
}
MaterialButton _buildRaisedButton(String text, VoidCallback callback) {
return RaisedButton(
onPressed: callback,
child: Text(text)
);
}
void goToTargetPage(String routerName) {
Navigator.pushNamed(context, routerName);
// Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) {
// return StatefulWidgetPage();
// }));
}
Widget _buildBrightnessSwitch() {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("夜间模式: "),
Switch(value: _mCurrentBrightness == Brightness.dark, onChanged: (bool isSelect) =>
setState(() {
_mCurrentBrightness = isSelect ? Brightness.dark : Brightness.light;
})
)
],
);
}
}
错误日志:
An Observatory debugger and profiler on sdk gphone x86 64 arm64 is available at: http://127.0.0.1:5425/dMDerlghKqA=/
I/flutter ( 1113): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter ( 1113): The following assertion was thrown while handling a gesture:
I/flutter ( 1113): Navigator operation requested with a context that does not include a Navigator.
I/flutter ( 1113): The context used to push or pop routes from the Navigator must be that of a widget that is a
I/flutter ( 1113): descendant of a Navigator widget.
I/flutter ( 1113):
I/flutter ( 1113): When the exception was thrown, this was the stack:
I/flutter ( 1113): #0 Navigator.of.<anonymous closure> (package:flutter/src/widgets/navigator.dart:2184:9)
I/flutter ( 1113): #1 Navigator.of (package:flutter/src/widgets/navigator.dart:2191:6)
I/flutter ( 1113): #2 Navigator.pushNamed (package:flutter/src/widgets/navigator.dart:1587:22)
I/flutter ( 1113): #3 _HomePageState.goToTargetPage (package:FlutterPlayground/main.dart:92:15)
I/flutter ( 1113): #4 _HomePageState._buildBodyWidget.<anonymous closure> (package:FlutterPlayground/main.dart:66:59)
I/flutter ( 1113): #5 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:993:19)
I/flutter ( 1113): #6 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1111:38)
I/flutter ( 1113): #7 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:183:24)
I/flutter ( 1113): #8 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:598:11)
I/flutter ( 1113): #9 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:287:5)
I/flutter ( 1113): #10 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:222:7)
I/flutter ( 1113): #11 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:476:9)
I/flutter ( 1113): #12 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:77:12)
I/flutter ( 1113): #13 PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:122:9)
I/flutter ( 1113): #14 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8)
I/flutter ( 1113): #15 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:120:18)
I/flutter ( 1113): #16 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:106:7)
I/flutter ( 1113): #17 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:358:19)
I/flutter ( 1113): #18 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:338:22)
I/flutter ( 1113): #19 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:267:11)
I/flutter ( 1113): #20 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:295:7)
I/flutter ( 1113): #21 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:240:7)
I/flutter ( 1113): #22 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:213:7)
I/flutter ( 1113): #26 _invoke1 (dart:ui/hooks.dart:265:10)
I/flutter ( 1113): #27 _dispatchPointerDataPacket (dart:ui/hooks.dart:174:5)
I/flutter ( 1113): (elided 3 frames from dart:async)
I/flutter ( 1113):
I/flutter ( 1113): Handler: "onTap"
I/flutter ( 1113): Recognizer:
I/flutter ( 1113): TapGestureRecognizer#46398
正在查解决方案,这个问题是不是应该在课程中指出下~
解锁Flutter开发新姿势,,系统掌握Flutter开发核心技术。
了解课程