Flutter Go
https://github.com/alibaba/flutter-go
1_TextField(文本框)
main.dart
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Retrieve Text Input',
home: MyForm(),
);
}
}
class MyForm extends StatefulWidget {
const MyForm({Key? key}) : super(key: key);
@override
MyFormState createState() => MyFormState();
}
class MyFormState extends State<MyForm> {
String results = "";
final TextEditingController controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Using EditText"),
backgroundColor: Colors.red,
),
body: Center(
child: Column(
children: <Widget>[
TextField(
decoration: const InputDecoration(hintText: "Enter text here..."),
onSubmitted: (String str) {
setState(() {
results = results + "\n" + str;
});
},
),
Text(results)
],
),
),
);
}
}
1.1文本框获取值
main.dart
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Retrieve Text Input',
home: MyForm(),
);
}
}
// Define a Custom Form Widget
class MyForm extends StatefulWidget {
const MyForm({Key? key}) : super(key: key);
@override
_MyFormState createState() => _MyFormState();
}
// Define a corresponding State class. This class will hold the data related to
// our Form.
class _MyFormState extends State<MyForm> {
// Create a text controller. We will use it to retrieve the current value
// of the TextField!
final myController = TextEditingController();
@override
void dispose() {
// Clean up the controller when the Widget is disposed
myController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Retrieve Text Input'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
controller: myController,
),
),
floatingActionButton: FloatingActionButton(
// When the user presses the button, show an alert dialog with the
// text the user has typed into our text field.
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
// Retrieve the text the user has typed in using our
// TextEditingController
content: Text(myController.text),
);
},
);
},
tooltip: 'Show me the value!',
child: const Icon(Icons.text_fields),
),
);
}
}
1.2_复制粘贴
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void 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,
title: 'Kindacode.com',
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final TextEditingController _textController = TextEditingController();
// This function is triggered when the copy icon is pressed
Future<void> _copyToClipboard() async {
await Clipboard.setData(ClipboardData(text: _textController.text));
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Copied to clipboard'),
));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Kindacode.com'),
backgroundColor: Colors.deepOrange,
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(30),
child: TextField(
maxLines: null,//可以多行
controller: _textController,
decoration: InputDecoration(
icon: IconButton(
icon: const Icon(Icons.copy),
onPressed: _copyToClipboard,
),
),
),
)));
}
}
2_TabBar(选项卡)
main.dart
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
title: const Material(
color: Colors.blue,
child: TabBar(
tabs: [
Tab(icon: Icon(Icons.directions_car)),
Tab(icon: Icon(Icons.directions_transit)),
Tab(icon: Icon(Icons.directions_bike)),
],
),
),
),
body: const TabBarView(
children: [
Home1(),
Home2(),
Home3(),
],
),
),
),
);
}
}
class Home1 extends StatelessWidget{
const Home1({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(child:Text('HOME1') ,),
);
}
}
class Home2 extends StatelessWidget{
const Home2({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(child:Text('HOME2') ,),
);
}
}
class Home3 extends StatelessWidget{
const Home3({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(child:Text('HOME3') ,),
);
}
}
3_bottomNavigationBar(底部导航栏)
main.dart
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: const Color(0xFF2196f3),
canvasColor: const Color(0xFFfafafa),
colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.blue).copyWith(secondary: const Color(0xFF2196f3)),
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int index=0;
List<Widget> pages=[
const Home1(),
const Home2(),
const Home3(),
const Home4()
];
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: index,
fixedColor: Colors.blue,
onTap: (int idx){
setState(() {
index=idx;
});
},
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.access_alarm),
label: 'Title',
),
BottomNavigationBarItem(
icon: Icon(Icons.star),
label: 'Title',
),
BottomNavigationBarItem(
icon: Icon(Icons.pages),
label: 'Title',
),
BottomNavigationBarItem(
icon: Icon(Icons.adjust),
label: 'Title',
),
]
),
body:pages[index] ,
);
}
}
class Home1 extends StatelessWidget{
const Home1({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home1'),),
body: const Center(child:Text('HOME1') ,),
);
}
}
class Home2 extends StatelessWidget{
const Home2({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home2'),),
body: const Center(child:Text('HOME2') ,),
);
}
}
class Home3 extends StatelessWidget{
const Home3({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home3'),),
body: const Center(child:Text('HOME3') ,),
);
}
}
class Home4 extends StatelessWidget{
const Home4({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home4'),),
body: const Center(child:Text('HOME4') ,),
);
}
}