import 'package:demoflutter/main_base2.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart' show rootBundle ; import 'package:dio/dio.dart'; import 'dart:convert'; import 'dart:async'; import 'dart:io'; void main() { runApp(MaterialApp( title: "material", theme: ThemeData( backgroundColor: Colors.red, primaryColor: Colors.blue, splashColor: Colors.transparent ), home: Scaffold( appBar: AppBar( title: Text("CodeText"), ), // body: MyApp2()))); // body: MyGridCountDemo()))); // body: GridViewBuildDemo()))); // body: HomeContent()))); // body: HomeContent2()))); // body: MyHomePage3()))); // body: HYStartRating(rating:1)))); // body: showDashLine()))); // body: MyApp()))); // body: MovieListItem(MovieItem.fromMap({"":""}))))); body: HYHomePage()))); } class HYDataWidget extends InheritedWidget { final int counter; HYDataWidget( {this.counter, Widget child}): super(child: child); static HYDataWidget of(BuildContext context){ return context.dependOnInheritedWidgetOfExactType(); } @override bool updateShouldNotify(HYDataWidget oldWidget) { // TODO: implement updateShouldNotify return oldWidget.counter !=this.counter; } } class HYHomePage extends StatefulWidget { @override _HYHomePageState createState() => _HYHomePageState(); } class _HYHomePageState extends State<HYHomePage> { int data = 100; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text("inheritedWidget"),), body: HYDataWidget( counter: data, child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ HYSHowData01() ], ), ), ), floatingActionButton: FloatingActionButton( child: Icon(Icons.add), onPressed: (){ setState(() { data++; }); }, ), ); } } class ShowWidget extends StatefulWidget { @override _stateShowWidget createState() => _stateShowWidget(); } class _stateShowWidget extends State<ShowWidget>{ int count=0; @override Widget build(BuildContext context) { return Center( child: HYDataWidget( counter: count, child: FloatingActionButton( child:Text("sa"), onPressed: (){ setState(() { ++count; }); }, ), ) , ); } } // class HYSHowData01 extends StatelessWidget { // // @override // Widget build(BuildContext context){ // // return Container(); // int counter = HYDataWidget.of(context).counter; // return Card( // color: Colors.red, // child: Text('当前计数1111:${counter}'), // ); // } // // // } class HYSHowData01 extends StatefulWidget { @override _stateHYSHowData01 createState() => _stateHYSHowData01(); } class _stateHYSHowData01 extends State<HYSHowData01> { @override void didChangeDependencies() { // TODO: implement didChangeDependencies super.didChangeDependencies(); int counter = HYDataWidget.of(context).counter; print("改变啊 aa${counter}"); } @override Widget build(BuildContext context){ // return Container(); int counter = HYDataWidget.of(context).counter; return Card( color: Colors.red, child: Text('当前计数1111:${counter}'), ); } } class HYSHowData02 extends StatelessWidget { @override Widget build(BuildContext context){ // return Container(); return Card( color: Colors.red, child: Text('当前计数:100'), ); } }
FLutter中inheritedwidget的使用
最新推荐文章于 2024-04-30 11:10:47 发布