2021年12月15日 星期三

Flutter 簡易起手式

起始專案

import 'package:flutter/material.dart';

void main() {
runApp(
MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home:
Scaffold(
body:
_mainbody(),
),
);
}
}

class _mainbody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child:
RaisedButton(
child:
Text("Hello Button"),
onPressed: () {
// ...do something...
// ...
// ...go to page2...
//Navigator.push(context,
// MaterialPageRoute<void>(builder:(BuildContext context) {
// return page2() ;
//})

},
      ),
);
}
}


 StatelessWidget

import 'package:flutter/material.dart';

class page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: _page2()
);
}
}

class _page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body:Center(
child: RaisedButton(
child: Text("Home"),
onPressed: () {
// ...do someting...
// ...
// ...return homepage...
//Navigator.pop(context);
}),
),
);
}
}



沒有留言:

張貼留言