import 'package:flutter/material.dart';
main(){
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("计数器"),
centerTitle: true,
),
body: const MyHomeContent(),
);
}
}
class MyHomeContent extends StatefulWidget {
final String parentValue = "这是父类传过来的值";
const MyHomeContent({Key? key}) : super(key: key);
@override
State<MyHomeContent> createState() => _MyHomeContentState();
}
class _MyHomeContentState extends State<MyHomeContent> {
int _count = 0;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
child: const Text("+",style:TextStyle(color: Colors.white,fontSize: 35)),
onPressed: (){
setState(() {
_count++;
});
}),
ElevatedButton(
child: const Text("-",style:TextStyle(color: Colors.white,fontSize: 35)),
onPressed: (){
setState(() {
_count--;
});
}),
],
),
Text("当前计数是:$_count",style: const TextStyle(fontSize: 30)),
Text(widget.parentValue,style: const TextStyle(fontSize: 20)),
],
),
);
}
}
代码:
import 'package:flutter/material.dart';
main(){
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("2222"),
centerTitle: true,
),
body: const MyHomeContent(),
);
}
}
class MyHomeContent extends StatefulWidget {
const MyHomeContent({Key? key}) : super(key: key);
@override
State<MyHomeContent> createState() => _MyHomeContentState();
}
class _MyHomeContentState extends State<MyHomeContent> {
final ButtonStyle style =
ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20));
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ButtonTheme(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
child: ElevatedButton(
style: style,
onPressed:null,
child: const Text("Disabled"),
),
),
//const SizedBox(height: 30),
ElevatedButton(
style: style,
onPressed: () {},
child: const Text('Enabled'),
),
],
),
);
}
}
文章评论