10
This commit is contained in:
14
lib/13/01____/main.dart
Normal file
14
lib/13/01____/main.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'pages/app/app.dart';
|
||||
import 'pages/app/app_router_delegate.dart';
|
||||
|
||||
|
||||
AppRouterDelegate router = AppRouterDelegate();
|
||||
|
||||
void main() {
|
||||
runApp(const App());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
29
lib/13/01____/pages/app/app.dart
Normal file
29
lib/13/01____/pages/app/app.dart
Normal file
@@ -0,0 +1,29 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
|
||||
import '../../main.dart';
|
||||
import 'app_tool_bar.dart';
|
||||
|
||||
class App extends StatelessWidget {
|
||||
const App({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
theme: ThemeData(
|
||||
appBarTheme: const AppBarTheme(
|
||||
elevation: 0,
|
||||
iconTheme: IconThemeData(color: Colors.black),
|
||||
titleTextStyle: TextStyle(
|
||||
color: Colors.black,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
))),
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
appBar: AppToolBar(),
|
||||
body: Router(routerDelegate: router),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
73
lib/13/01____/pages/app/app_router_delegate.dart
Normal file
73
lib/13/01____/pages/app/app_router_delegate.dart
Normal file
@@ -0,0 +1,73 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../page_a.dart';
|
||||
import '../page_b.dart';
|
||||
import '../page_c.dart';
|
||||
import '../home_page.dart';
|
||||
|
||||
class AppRouterDelegate extends RouterDelegate<String> with ChangeNotifier, PopNavigatorRouterDelegateMixin {
|
||||
|
||||
AppRouterDelegate({String initial = '/'}):_path=initial;
|
||||
|
||||
String _path;
|
||||
|
||||
String get path=> _path;
|
||||
|
||||
void go(String path){
|
||||
_path = path;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Navigator(
|
||||
onPopPage: _onPopPage,
|
||||
pages: parserConfig(_path).map((e) => _pageMap[e]!).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
final Map<String, Page> _pageMap = const {
|
||||
'/': MaterialPage(child: HomePage()),
|
||||
'a': MaterialPage(child: PageA()),
|
||||
'b': MaterialPage(child: PageB()),
|
||||
'c': MaterialPage(child: PageC()),
|
||||
};
|
||||
|
||||
Widget buildNavigatorByConfig(String path) {
|
||||
return Navigator(
|
||||
onPopPage: _onPopPage,
|
||||
pages: parserConfig(path).map((e) => _pageMap[e]!).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
bool _onPopPage(Route route, result) {
|
||||
if(path.length>1){
|
||||
int last = path.lastIndexOf('/');
|
||||
_path = path.substring(0,last);
|
||||
if(_path.isEmpty) _path = '/';
|
||||
notifyListeners();
|
||||
}
|
||||
return route.didPop(result);
|
||||
}
|
||||
|
||||
@override
|
||||
GlobalKey<NavigatorState>? navigatorKey = GlobalKey<NavigatorState>();
|
||||
|
||||
|
||||
@override
|
||||
Future<void> setNewRoutePath(String configuration) async{
|
||||
_path = configuration;
|
||||
}
|
||||
|
||||
List<String> parserConfig(String path){
|
||||
/// 例: /a/b/c 进栈 /,a,b,c 界面
|
||||
List<String> result = ['/'];
|
||||
if(path.startsWith('/')){
|
||||
path = path.substring(1);
|
||||
if(path.isNotEmpty){
|
||||
List<String> parts = path.split('/');
|
||||
result.addAll(parts);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
64
lib/13/01____/pages/app/app_tool_bar.dart
Normal file
64
lib/13/01____/pages/app/app_tool_bar.dart
Normal file
@@ -0,0 +1,64 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../main.dart';
|
||||
|
||||
class AppToolBar extends StatefulWidget implements PreferredSizeWidget{
|
||||
const AppToolBar({super.key});
|
||||
|
||||
@override
|
||||
State<AppToolBar> createState() => _AppToolBarState();
|
||||
|
||||
@override
|
||||
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
|
||||
}
|
||||
|
||||
class _AppToolBarState extends State<AppToolBar> {
|
||||
|
||||
TextEditingController _ctrl = TextEditingController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_changRoute();
|
||||
router.addListener(_changRoute);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
router.removeListener(_changRoute);
|
||||
_ctrl.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _changRoute() {
|
||||
_ctrl.text=router.path;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
return AppBar(
|
||||
backgroundColor: Colors.white,
|
||||
elevation: 0,
|
||||
title : TextField(
|
||||
controller: _ctrl,
|
||||
onSubmitted: _onSubmitted,
|
||||
decoration: InputDecoration( //装饰
|
||||
filled: true, //填充
|
||||
fillColor: Color(0xffF3F6F9), //填充颜色
|
||||
constraints: BoxConstraints(maxHeight: 34), //约束信息
|
||||
contentPadding: EdgeInsets.only(top: -14,left: 10),
|
||||
border: UnderlineInputBorder( //边线信息
|
||||
borderSide: BorderSide.none,
|
||||
borderRadius: BorderRadius.all(Radius.circular(6)),
|
||||
),
|
||||
hintText: "请输入路由", //提示字
|
||||
hintStyle: TextStyle(fontSize: 14) //提示字样式
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _onSubmitted(String value) {
|
||||
router.go(value);
|
||||
}
|
||||
}
|
||||
30
lib/13/01____/pages/home_page.dart
Normal file
30
lib/13/01____/pages/home_page.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../main.dart';
|
||||
|
||||
|
||||
class HomePage extends StatelessWidget {
|
||||
const HomePage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const Color bgColor = Color(0xffCCFFCC);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: bgColor,
|
||||
appBar: AppBar(
|
||||
title: const Text('主页起点'),
|
||||
backgroundColor: bgColor,
|
||||
),
|
||||
body: Center(child: ElevatedButton(
|
||||
onPressed: () => toPageA(context),
|
||||
child: const Text('Push A'),
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
void toPageA(BuildContext context) {
|
||||
router.go('/a');
|
||||
}
|
||||
}
|
||||
30
lib/13/01____/pages/page_a.dart
Normal file
30
lib/13/01____/pages/page_a.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../main.dart';
|
||||
|
||||
class PageA extends StatelessWidget {
|
||||
const PageA({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const Color bgColor = Color(0xffCCFFFF);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: bgColor,
|
||||
appBar: AppBar(
|
||||
title: const Text('A 界面'),
|
||||
backgroundColor: bgColor,
|
||||
),
|
||||
body: Center(
|
||||
child: ElevatedButton(
|
||||
onPressed: () => toPageB(context),
|
||||
child: const Text('Push B'),
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
void toPageB(BuildContext context) {
|
||||
// router.value = ['/', 'a', 'b'];
|
||||
router.go('/a/b');
|
||||
}
|
||||
|
||||
}
|
||||
32
lib/13/01____/pages/page_b.dart
Normal file
32
lib/13/01____/pages/page_b.dart
Normal file
@@ -0,0 +1,32 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../main.dart';
|
||||
|
||||
class PageB extends StatelessWidget {
|
||||
const PageB({super.key});
|
||||
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const Color bgColor = Color(0xffCCE5FF);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: bgColor,
|
||||
appBar: AppBar(
|
||||
title: const Text('B 界面'),
|
||||
backgroundColor: bgColor,
|
||||
|
||||
),
|
||||
body: Center(
|
||||
child: ElevatedButton(
|
||||
onPressed: () => toPageC(context),
|
||||
child: const Text('Push C'),
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
void toPageC(BuildContext context){
|
||||
router.go('/a/b/c');
|
||||
}
|
||||
|
||||
}
|
||||
24
lib/13/01____/pages/page_c.dart
Normal file
24
lib/13/01____/pages/page_c.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../main.dart';
|
||||
|
||||
class PageC extends StatelessWidget {
|
||||
const PageC({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const Color bgColor = Color(0xffFFE6CC);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: bgColor,
|
||||
appBar: AppBar(
|
||||
title: const Text('C 界面'),
|
||||
backgroundColor: bgColor,
|
||||
),
|
||||
body: Center(
|
||||
child: Text('到达终点'),
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user