//marin.dart
import 'package:flutter/material.dart';
import 'page2.dart';
void main() {
runApp(GlobalData(
child: MaterialApp(
initialRoute: '/',
routes: {
'/': (BuildContext context) => HomePage(),
'/page2': (BuildContext context) => Page2(),
},
),
));
}
class HomePage extends StatefulWidget {
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('image')),
body: Column(
children: [
GestureDetector(
onTap: () {
GlobalData.of(context).title = 'new Title';
Navigator.of(context).pushNamed('/page2');
},
child: Text('page2'),
)
],
));
}
}
class GlobalData extends InheritedModel {
GlobalData({
this.title,
Widget? child,
}) : super(child: child!);
String? title;
static GlobalData of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<GlobalData>()!;
}
bool updateShouldNotify(GlobalData old) => title != old.title;
bool updateShouldNotifyDependent(GlobalData oldWidget, Set dependencies) {
if (dependencies.contains('title') && oldWidget.title != title) {
return true;
}
return false;
}
}
//page2.dart
import 'package:flutter/material.dart';
import 'main.dart';
class Page2 extends StatelessWidget {
Page2();
Widget build(BuildContext context) {
GlobalData g = GlobalData.of(context);
return Scaffold(
appBar: AppBar(
title: Text('${g.title}'),
),
);
}
}
沒有留言:
張貼留言