10
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user