1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| void main() => runApp(App()); class App extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'loading 动画', debugShowCheckedModeBanner: false, theme: ThemeData( brightness: Brightness.dark, ), home: Scaffold( // SafeArea 异形屏适配 body: MultiProvider( providers: [ ChangeNotifierProvider( create: (_) => LoginProvider(), ) ], child: Consumer<LoginProvider>(builder: (context,loginProvider,_){ return BodyWidget(ifLoading: loginProvider.ifLoading,onPressed: loginProvider.onPressed,); },), ), ), ); } } class LoginProvider with ChangeNotifier { bool ifLoading = false; VoidCallback onPressed; Timer timer; LoginProvider() { onPressed = () { ifLoading = true; // notifyListeners 我的理解就是把它当做 setState 用 notifyListeners(); // 模拟发送网络请求异步获得请求结果 timer = new Timer(Duration(seconds: 3), () { ifLoading = false; notifyListeners(); }); }; } } class BodyWidget extends StatelessWidget { bool ifLoading = false; VoidCallback onPressed;
BodyWidget({@required this.ifLoading, @required this.onPressed});
@override Widget build(BuildContext context) { return SafeArea( child: Stack( children: <Widget>[ Container( width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).size.height, decoration: BoxDecoration(color: Colors.amberAccent), child: Column( mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[ RaisedButton( child: Text("登录"), onPressed: onPressed, ), ], ), ), ifLoading ? Positioned.fill( child: Container( color: Colors.blueAccent, child: SpinKitFadingCircle( // 可以画一个圈,分为 0 1 2 3 4 5 6 7 8 9 份 itemBuilder: (_, int index) { return DecoratedBox( decoration: BoxDecoration( // isEven 表示是否整数 color: index.isEven ? Colors.red : Colors.green, ), child: Text("$index"), ); }, size: 120.0, ), ), ) : Container(), ], ), ); } }
|